尝试更改 Discord 存在时出现属性错误

时间:2021-03-01 03:37:37

标签: python discord.py

我的代码是这样的:

import discord
from discord.ext import tasks 

client = discord.Client()

@tasks.loop(seconds=5)
async def update_info():
  the_name = 'Insert game name here'
  await client.change_presence(activity=discord.Game(name=the_name))
  print('Info Updated')

update_info.start()
client.run("TOKEN")

但是,当我尝试运行此代码时,我收到此错误消息:

Unhandled exception in internal background task 'update_info'.
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "main.py", line 223, in update_info
    await client.change_presence(activity=discord.Game(name=the_name))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 1059, in change_presence
    await self.ws.change_presence(activity=activity, status=status, afk=afk)
AttributeError: 'NoneType' object has no attribute 'change_presence'

然后程序继续每五秒打印一次“信息更新”,但我的机器人的状态没有得到更新。 我原以为在函数之外定义客户端就足以让任何函数从中读取数据。

基本上我想每 5 分钟更改一次状态,此过程在后台进行。

编辑: 没关系,我设法通过将更新信息开始输入来弄清楚 客户端事件就绪 像这样:

import discord
from discord.ext import tasks 

client = discord.Client()

@client.event
async def on_ready():
    update_info.start()

@tasks.loop(seconds=5)
async def update_info():
  the_name = 'Insert game name here'
  await client.change_presence(activity=discord.Game(name=the_name))
  print('Info Updated')

client.run("TOKEN")

仍然不知道为什么原来的不起作用,但代码现在每五秒更新一次状态。

0 个答案:

没有答案