Python 3.x 我想动态更改discord bot状态

Python 3.x 我想动态更改discord bot状态,python-3.x,discord.py,Python 3.x,Discord.py,这是我使用的代码 async def on_ready(): print(f'bots up') await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='.help')) 就像每一分钟的状态变化一样, 我试过了,但它不允许我使用其他命令,我输入了这段代码,但它会给出一个错误,说我不能在\u ready()上的中输入消息参数。 要定期更改bot的状

这是我使用的代码

async def on_ready():
    print(f'bots up')
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='.help'))
就像每一分钟的状态变化一样, 我试过了,但它不允许我使用其他命令,我输入了这段代码,但它会给出一个错误,说我不能在\u ready()上的中输入消息参数。


要定期更改bot的状态,可以使用。基本蓝图可以如下所示:

导入不一致
从discord.ext导入命令
从discord.ext导入任务
bot=commands.bot(“.”)
@tasks.loop(秒=30)
异步def开关_presence():
#要在两者之间切换的所有活动的列表
活动=[
discord.Activity(type=discord.ActivityType.listing,name='First Activity'),
discord.Activity(type=discord.ActivityType.listing,name='other Activity')
]
当前活动=机器人活动
#如果未设置或无效,则默认为第一个活动
如果当前活动不在活动中:
等待bot.change_状态(activity=activities[0])
返回
#一旦列表用尽,使用模从开始开始
下一个活动索引=(活动.索引(当前活动)+1)%len(活动)
等待机器人。更改存在状态(活动=活动[下一个活动\u索引])
@机器人事件
_ready()上的异步定义:
开关_presence.start()

经过一些修改,您也可以将其放入Cog中。这将允许您保留一个静态活动列表,而不是动态创建它(只需在Cog的
\uuuu init\uuuu
中创建它并将其存储为类变量),还可以集中存储下一个活动的索引,无需每次调用循环时都计算它

这就是我更改机器人存在的方式

async def status_task():
while True:
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"listining status"))
    await asyncio.sleep(10)# 10 as in 10seconds
    await bot.change_presence(activity=discord.Game(name="playing status"))
    await asyncio.sleep(10)
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.competing, name=f"competing status"))
    await asyncio.sleep(10)
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.streaming, name=f"streaming status"))
    await asyncio.sleep(10)
然后你准备好了吗

@bot.event
async def on_ready():
    print(f"bots up")
    bot.loop.create_task(status_task())

希望这对您有效,(对于status_任务)将每个任务之间的10秒更改为60秒,然后每分钟更改一次

您发布的代码不会干扰您的机器人响应命令的能力。如果有的话,
等待bot.process\u命令(消息)
属于消息上的
末尾。要定期更改您的机器人的状态,您应该设置一个
discord.ext.tasks.loop
on_ready
内部的
可以发送tht的代码吗?我应该在上面添加什么异步定义状态任务()…什么都没有?什么都没有不,这不是一个机器人事件,也不是任何其他工作状态
@bot.event
async def on_ready():
    print(f"bots up")
    bot.loop.create_task(status_task())