gpt4 book ai didi

python - Discord.py 使机器人不与 DM channel 交互

转载 作者:行者123 更新时间:2023-12-04 11:30:30 25 4
gpt4 key购买 nike

我正在编写一个小消息记录程序,我希望机器人只记录来自特定公会的消息,为此,我检查了 message.guild.id .但是,当在 DM channel 中发送消息时,这会引发问题。我希望机器人完全忽略 Dm channel ,但我没有太多运气
编码:

@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 not message.guild:
# message is from a server
else:
# message is from a dm.
就是这样。无需检查类型。

关于python - Discord.py 使机器人不与 DM channel 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63105018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com