【发布时间】:2020-11-16 04:13:20
【问题描述】:
我正在编写一个小型消息记录程序,我希望机器人只记录来自特定公会的消息,为此,我检查了message.guild.id。然而,当在 DM 频道中发送消息时,这会引发问题。我希望机器人完全忽略 Dm 频道,但我运气不佳
代码:
@commands.Cog.listener()
async def on_message(self, message):
if message.guild.id == Guild ID HERE:
print(f"{message.author} said --- {message.clean_content} --- in #{message.channel.name}")
elif message.channel.type is discord.ChannelType.private:
pass
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "d:\Documents\Bots\DS BOT\cog\Listener.py", line 13, in on_message
if message.guild.id == Guild ID HERE:
AttributeError: 'NoneType' object has no attribute 'id'
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "d:\Documents\Bots\DS BOT\cog\Logger.py", line 12, in on_message
if message.guild.id == Guild ID HERE:
AttributeError: 'NoneType' object has no attribute 'id'
【问题讨论】:
-
if message.guild is None: return
标签: python discord discord.py discord.py-rewrite nonetype