I am writing a small message logging program, I want the bot to only log messages from a specific Guild and for that, I check for the message.guild.id
. This, however, raises a problem when there is a message sent in a DM Channel. I want the bot to ignore the Dm channel completely but I did not have much luck in that
The Code:
JavaScript
x
7
1
@commands.Cog.listener()
2
async def on_message(self, message):
3
if message.guild.id == Guild ID HERE:
4
print(f"{message.author} said --- {message.clean_content} --- in #{message.channel.name}")
5
elif message.channel.type is discord.ChannelType.private:
6
pass
7
JavaScript
1
15
15
1
Ignoring exception in on_message
2
Traceback (most recent call last):
3
File "C:Python38libsite-packagesdiscordclient.py", line 312, in _run_event
4
await coro(*args, **kwargs)
5
File "d:DocumentsBotsDS BOTcogListener.py", line 13, in on_message
6
if message.guild.id == Guild ID HERE:
7
AttributeError: 'NoneType' object has no attribute 'id'
8
Ignoring exception in on_message
9
Traceback (most recent call last):
10
File "C:Python38libsite-packagesdiscordclient.py", line 312, in _run_event
11
await coro(*args, **kwargs)
12
File "d:DocumentsBotsDS BOTcogLogger.py", line 12, in on_message
13
if message.guild.id == Guild ID HERE:
14
AttributeError: 'NoneType' object has no attribute 'id'
15
Advertisement
Answer
You can just do
JavaScript
1
5
1
if message.guild:
2
# message is from a server
3
else:
4
# message is from a dm.
5
That’s it. There’s no need of checking for types.