Python 如何在discord.py中创建自定义循环状态?
我有一个discord机器人,用来学习如何使用API和练习python知识。我正在尝试创建一个系统,随机选择一个状态并应用它,然后等待很短的时间,目前为10分钟,然后无限期地继续,直到bot关闭。目前,我的Python 如何在discord.py中创建自定义循环状态?,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我有一个discord机器人,用来学习如何使用API和练习python知识。我正在尝试创建一个系统,随机选择一个状态并应用它,然后等待很短的时间,目前为10分钟,然后无限期地继续,直到bot关闭。目前,我的on_ready()功能中有一个循环,但bot会在短时间后关闭(不到10分钟,大约2-3分钟),我不知道为什么。当前代码如下: @client.event async def on_ready(): print("We have logged in as {0.user}&quo
on_ready(
)功能中有一个循环,但bot会在短时间后关闭(不到10分钟,大约2-3分钟),我不知道为什么。当前代码如下:
@client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
infiniteTime = 1
while infiniteTime != 20:
statusType = random.randint(0, 1)
if statusType == 0:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
else:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
time.sleep(600)
watchingStatus
和listingstatus
是1D数组,其中包含11个字符串。感谢您的任何帮助/反馈,因为我是API新手。干杯
ETA:我应该澄清一下,我已经意识到,将代码放在
on_ready()
函数中意味着它永远不会结束,因此不能做任何其他事情。我这样问是因为我不知道为什么机器人会在不停止代码的情况下在Discord端关闭,我也不知道我还应该把这个循环放在哪里,因为我是这个异步/等待系统的新手,API的工作方式也是如此。您尝试更改机器人状态的地方是正确的,问题是,尝试创建此循环的方法会停止bot的工作,因为时间。sleep
不适用于discord.py等异步库。相反,您可以使用asyncio.sleep()
你可以这样做:
import asyncio
@client.event
async def on_ready():
# ...
# Here goes your code for on_ready()
# ...
while True:
statusType = random.randint(0, 1)
if statusType == 0:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
else:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
asyncio.sleep(600)
另一种方法是使用discord.ext.tasks
,这些函数被定义为运行后台任务,比如您想要实现的任务
from discord.ext import commands, tasks
import asyncio
# Your code here
@loop(seconds=600)
async def status_change():
# ...
# Some code to define playingStatus and watchingStatus arrays
# ...
statusType = random.randint(0, 1)
if statusType == 0:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
else:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
status_change.before_loop(client.wait_until_ready())
status_change.start()
client.run("TOKEN")
参考资料:
时间。sleep
不适用于异步库,如discord.py。相反,您可以使用asyncio.sleep()
你可以这样做:
import asyncio
@client.event
async def on_ready():
# ...
# Here goes your code for on_ready()
# ...
while True:
statusType = random.randint(0, 1)
if statusType == 0:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
else:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
asyncio.sleep(600)
另一种方法是使用discord.ext.tasks
,这些函数被定义为运行后台任务,比如您想要实现的任务
from discord.ext import commands, tasks
import asyncio
# Your code here
@loop(seconds=600)
async def status_change():
# ...
# Some code to define playingStatus and watchingStatus arrays
# ...
statusType = random.randint(0, 1)
if statusType == 0:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.playing, name=playingStatus[statusNum]))
else:
statusNum = random.randint(0, 10)
await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name=watchingStatus[statusNum]))
status_change.before_loop(client.wait_until_ready())
status_change.start()
client.run("TOKEN")
参考资料:
等待asyncio.sleep(600)
。对于第二种方法,您可能需要使用从tasks import loop
显式导入循环。这很有效,谢谢!很抱歉,这是如此难以复制,这是相当简陋,所以有点难以确定!两种方法都有错误。第一次正确引导并登录,但随后表示从未等待“睡眠”,以使tracemalloc获得回溯()。第二个表示没有定义名称“loop()。有什么想法吗?我的错,因为你的代码很难复制,我无法测试它是否完全工作。对于第一个,您需要等待睡眠:等待asyncio.sleep(600)
。对于第二种方法,您可能需要使用从tasks import loop
显式导入循环。这很有效,谢谢!很抱歉,这是如此难以复制,这是相当简陋,所以有点难以确定!