0

I'm building a bot, and I want it to assign someone a certain role in my server if they DM the bot a certain passphrase.

if message.guild is None:
  if message.guild is None:
       if passphrase in message.content:
         await message.channel.send("You've been verified!")

I've gotten the bot to recognize the event, but I have no idea as to how to assign the user the role I want.

How would I go about having the bot assign a certain role in my server in this if statement?

neliah
  • 47
  • 5

1 Answers1

0

Since you're expecting the user to send the message in a dm channel, you will need to define the server you'll be adding the role in, as well as what role will be added. Do view the code below, as well as any further explanations.

Edit: My apologies, I had forgotten that you have to get the user from the server itself, otherwise you would (usually) get the error AttributeError: 'User' object has no attribute 'add_roles'. Also, I had forgotten to await the add_roles, which would've given the error 'Member.add_roles' was never awaited. I would recommend checking or improving your error handler, as it seems to be 'eating up errors'. (Edited code remarks are indicated with ##, please review the new changes)

if message.guild is None:
    # you don't need to do it twice
    if passphrase in message.content:
        your_guild = bot.get_guild(000000) # put your server's id in here

        # check if author is in the guild, if not end the current interaction
        if message.author not in your_guild.members: return

        ## You also need to get the user within the guild
        user_from_guild = your_guild.get_member(message.author.id)
        verify_role = your_guild.get_role(000000) # put the role id in here

        ## remember to await add_roles as well!
        await user.add_roles(verify_role) # give the author the role
        await message.channel.send("You've been verified!")

Other links:

Bagle
  • 2,326
  • 3
  • 12
  • 36
  • This partially works. It prints "You've been verified", but the issue is that it's not adding the role to the user. Any idea as to why this might be? – neliah Jan 13 '22 at 18:31
  • I'll have another look at the code that I've posted, and edit it asap. Thanks for letting me know that it doesn't work as expected – Bagle Jan 14 '22 at 08:03
  • I've copy pasted the code, and it still does not work. No errors or anything, and the code still runs through. – neliah Jan 14 '22 at 23:11
  • 1
    Do you have [`member intents`](https://stackoverflow.com/questions/64831017/how-do-i-get-the-discord-py-intents-to-work) turned on @neliah ? – Bagle Jan 15 '22 at 04:33
  • 1
    I turned it on, and that was the issue, thanks! – neliah Jan 15 '22 at 22:15