discord.py on_message 作者 return none 在私人频道

discord.py on_message author return none in private channel

所以我有这个代码

@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)

当我在每个角色都发送消息并查看频道权限的频道中测试它时它工作正常,但是当我在只有特定角色可以看到它的频道中测试它时引发此错误

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'

我已启用所有意图。

intents = discord.Intents.all()

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

我也试过 msg.author 而不是 member = msg.guild.get_member(msg.author.id) 但问题仍然存在

有什么办法可以解决这个问题?

私信里guild是None。因此,如果它是 None

,我建议您忽略它
if msg.guild is None:
    return