hard lander
hard lander

Reputation: 112

discord.py on_message author return none in private channel

So I have this code

@client.event
async def on_message(msg):
  bprole = msg.guild.get_role(773370896773021716)
  filter = [#a list of filtered word, in actual code this list have items]
  member = msg.guild.get_member(msg.author.id)

  for word in filter:
    if bprole not in member.roles:
      if msg.channel.id != 715097440369508484:
        if msg.content.count(word) > 0:
          print("%s Has said a bad word" % (msg.author.id))
          await msg.delete()
          await msg.channel.send("%s Mind your language. It is a blacklisted word" % (msg.author.mention))
  mention = msg.mentions #you may ignore this this is an afk system and its work just fine but incase this thing is the cause of the problem I included it
  for m in mention:
    try:
      val = db[m.id]
      await msg.channel.send(f"{m.name} is AFK: {val}")
    except:
      print()
  await client.process_commands(msg)

When I test it in a channel where everyone role have send a message and see channel permission its work fine, but when I tested it in a channel that only a certain role can see its raise this error

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 54, in on_message
    if bprole not in member.roles:
AttributeError: 'NoneType' object has no attribute 'roles'

I have all intents enabled.

intents = discord.Intents.all()

client = commands.Bot(command_prefix=get_prefix,case_insensitive=True, intents=intents, help_command=None)

I also tried msg.author instead of member = msg.guild.get_member(msg.author.id) too but the problem still presist

Any way I can fix this?

Upvotes: 0

Views: 460

Answers (1)

Peach
Peach

Reputation: 84

In private messages guild is None. So i would reccommend just igonring it if it's None

if msg.guild is None:
    return

Upvotes: 1

Related Questions