0

I would like the bot to recognize when the @Community Manager, @Admin, @Moderator roles are tagged, either single, or multiple roles tagged in same message, then send a message to the channel mentioning the user name.

I can get the bot to recognize when it's been tagged using this code:

if client.user.mentioned_in(message) and message.mention_everyone is False:
        await message.delete()

I cannot for the life of me figure out how to see if other roles were tagged.

I've tried

if message.role_mentions.name('Admin'):
#do stuff

but get this error: AttributeError: 'list' object has no attribute 'name'

Brandon Weeks
  • 69
  • 1
  • 2
  • 11

1 Answers1

1

message.role_mentions gives back a list of Role.

And then you can fetch roles from the guild using message.guild.get_role(id) in order to compare against the list of roles you gotten from the message.

Should result in something along the lines of this:

# Create a list of roles to check against
rolesToCheckAgainst [
    # Fetches the role from the guild which the message was sent from
    message.guild.get_role("The ID of the role"),
    message.guild.get_role("The ID of the second role")
    #etc...
]

# 'rolesWerePinged' will be true if any of the roles were pinged
rolesWerePinged = any(item in rolesToCheckAgainst for item in message.role_mentions)

if rolesWerePinged:
    # Do something, some of the roles was pinged.

Also, I used any() to check if any of the roles mentioned contained any of the roles that needed to be check against.
You can use a double-loop instead if you need different actions to be done depending on the type of roles mentioned.

WQYeo
  • 3,973
  • 2
  • 17
  • 26
  • So this is conflicting with my code that lets me know if someone sent the bot a DM. When someone DM's it used to post to the console what the message was and who from, now since I put the code above in, it throws this error: AttributeError: 'NoneType' object has no attribute 'get_role'. It's saying the line rolesToCheckAgainst = [message.guild.get_role(521374635712053249) is the issue – Brandon Weeks Aug 05 '19 at 04:47
  • @BrandonWeeks Its saying that `guild` is null, and you are trying to use `get_role` from null. Check if your `message` is actually sent in a guild first. – WQYeo Aug 05 '19 at 05:22
  • The Role mention messages are, yes, but this error is getting thrown when the bot receives a DM. Both are within the async def on_message, does my if isinstant(message.channel, discord.DMChannel) need to be somewhere else? – Brandon Weeks Aug 05 '19 at 05:25
  • You should check if the `message.guild` is null before invoking the whole code – WQYeo Aug 05 '19 at 05:32
  • How would I do that? Im sort of self teaching myself this with some experience in python – Brandon Weeks Aug 05 '19 at 05:37
  • @BrandonWeeks `if message.guild is None` was be the simplest way to check if the message was sent in a guild. https://stackoverflow.com/questions/43934304/how-to-test-a-variable-is-null-in-python?noredirect=1&lq=1 The Discordpy API might have another way. – WQYeo Aug 05 '19 at 06:21
  • Figured it out. set the DM instance above the roles.ToCheckAgainst, then put if message.guild is None: return, works fine. Thank you – Brandon Weeks Aug 05 '19 at 06:22
  • This was the reference I used [link](https://stackoverflow.com/questions/3289601/null-object-in-python) – Brandon Weeks Aug 05 '19 at 06:23