Discord.py 使机器人不与 DM 频道互动
Discord.py Making the bot not interact with DM channels
我正在编写一个小的消息记录程序,我希望机器人只记录来自特定公会的消息,为此,我检查 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'
在进行公会检查之前,您必须检查频道是否为私人频道,否则会引发错误。这应该可以完成工作:
@commands.Cog.listener()
async def on_message(self, message):
if isinstance(message.channel, discord.DMChannel):
return
if message.guild.id == Guild ID HERE:
print(f"{message.author} said --- {message.clean_content} --- in #
在你的 if
声明中传递这个:
if message.channel.type is discord.ChannelType.private:
return
这应该适合你!
你可以做到
if not message.guild:
# message is from a server
else:
# message is from a dm.
就是这样。无需检查类型。
我解决这个问题的方法很简单。当时我设置了一个自定义前缀系统,该系统将为机器人提供其自己的默认前缀的每个服务器,该前缀可以在以后更改。 Dm 的问题是它不是服务器,因此不在导致错误的前缀数据库中。
解决方法是:
def get_prefix(bot, message):
if (message.guild is None):
return '.'
else:
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
简单地说,如果收到的消息或命令不是从服务器发送的,它会简单地使用默认前缀
我正在编写一个小的消息记录程序,我希望机器人只记录来自特定公会的消息,为此,我检查 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'
在进行公会检查之前,您必须检查频道是否为私人频道,否则会引发错误。这应该可以完成工作:
@commands.Cog.listener()
async def on_message(self, message):
if isinstance(message.channel, discord.DMChannel):
return
if message.guild.id == Guild ID HERE:
print(f"{message.author} said --- {message.clean_content} --- in #
在你的 if
声明中传递这个:
if message.channel.type is discord.ChannelType.private:
return
这应该适合你!
你可以做到
if not message.guild:
# message is from a server
else:
# message is from a dm.
就是这样。无需检查类型。
我解决这个问题的方法很简单。当时我设置了一个自定义前缀系统,该系统将为机器人提供其自己的默认前缀的每个服务器,该前缀可以在以后更改。 Dm 的问题是它不是服务器,因此不在导致错误的前缀数据库中。
解决方法是:
def get_prefix(bot, message):
if (message.guild is None):
return '.'
else:
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
简单地说,如果收到的消息或命令不是从服务器发送的,它会简单地使用默认前缀