使用 Discord.py 时,错误提示 change_presence 不是 NoneType 的属性
Errors says change_presence is not an attribute to NoneType when using Discord.py
我正在尝试将我的机器人状态更改为正在玩游戏。我用了 client.change_presence 但它似乎不是 NoneType 的属性。
这里是代码:
class Bot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=_prefix_callable,
help_attrs=dict(hidden=True))
self.token = credentials['token']
self.client = discord.Client()
for extension in INITIAL_EXTENSIONS:
try:
self.load_extension(extension)
except Exception as e:
print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))
async def on_ready(self):
print('Logged in as:')
print('Username: ' + self.user.name)
print('ID: ' + str(self.user.id))
print('------')
await self.client.change_presence(status=discord.Status.idle, activity=discord.Game(name="can help"))
async def close(self):
await super().close()
def run(self):
super().run(self.token, reconnect=True)
if __name__ == '__main__':
bot = Bot()
bot.run()
这是我不断收到的错误:
Traceback (most recent call last):
File "C:\Apps\Python\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "D:\Projects\Bot\main.py", line 84, in on_ready
await self.client.change_presence(status=discord.Status.idle, activity=discord.Game(name="can help"))
File "C:\Apps\Python\lib\site-packages\discord\client.py", line 1062, in change_presence
await self.ws.change_presence(activity=activity, status=status, afk=afk)
AttributeError: 'NoneType' object has no attribute 'change_presence'
谢谢。
Bot 是 Client
子类。你不需要它。试试看:
class Bot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=_prefix_callable,
help_attrs=dict(hidden=True))
self.token = credentials['token']
# self.client = discord.Client() <removed>
for extension in INITIAL_EXTENSIONS:
try:
self.load_extension(extension)
except Exception as e:
print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))
async def on_ready(self):
print('Logged in as:')
print('Username: ' + self.user.name)
print('ID: ' + str(self.user.id))
print('------')
# await self.client.change_presence(status=discord.Status.idle,
# activity=discord.Game(name="can help")) <old>
await self.change_presence(status=discord.Status.idle,
activity=discord.Game(name="can help"))
# async def close(self):
# await super().close() <what different hear?>
def run(self):
super().run(self.token, reconnect=True)
if __name__ == '__main__':
bot = Bot()
bot.run()
我正在尝试将我的机器人状态更改为正在玩游戏。我用了 client.change_presence 但它似乎不是 NoneType 的属性。
这里是代码:
class Bot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=_prefix_callable,
help_attrs=dict(hidden=True))
self.token = credentials['token']
self.client = discord.Client()
for extension in INITIAL_EXTENSIONS:
try:
self.load_extension(extension)
except Exception as e:
print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))
async def on_ready(self):
print('Logged in as:')
print('Username: ' + self.user.name)
print('ID: ' + str(self.user.id))
print('------')
await self.client.change_presence(status=discord.Status.idle, activity=discord.Game(name="can help"))
async def close(self):
await super().close()
def run(self):
super().run(self.token, reconnect=True)
if __name__ == '__main__':
bot = Bot()
bot.run()
这是我不断收到的错误:
Traceback (most recent call last):
File "C:\Apps\Python\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "D:\Projects\Bot\main.py", line 84, in on_ready
await self.client.change_presence(status=discord.Status.idle, activity=discord.Game(name="can help"))
File "C:\Apps\Python\lib\site-packages\discord\client.py", line 1062, in change_presence
await self.ws.change_presence(activity=activity, status=status, afk=afk)
AttributeError: 'NoneType' object has no attribute 'change_presence'
谢谢。
Bot 是 Client
子类。你不需要它。试试看:
class Bot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=_prefix_callable,
help_attrs=dict(hidden=True))
self.token = credentials['token']
# self.client = discord.Client() <removed>
for extension in INITIAL_EXTENSIONS:
try:
self.load_extension(extension)
except Exception as e:
print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))
async def on_ready(self):
print('Logged in as:')
print('Username: ' + self.user.name)
print('ID: ' + str(self.user.id))
print('------')
# await self.client.change_presence(status=discord.Status.idle,
# activity=discord.Game(name="can help")) <old>
await self.change_presence(status=discord.Status.idle,
activity=discord.Game(name="can help"))
# async def close(self):
# await super().close() <what different hear?>
def run(self):
super().run(self.token, reconnect=True)
if __name__ == '__main__':
bot = Bot()
bot.run()