Leonardo Boiardi
Leonardo Boiardi

Reputation: 1

Discord Bot: Why this bot can read DM but doesn't give the role in the server?

I'm creating a discord bot that can give a Example role in the MIAO server if the user type in BOT dm the right email with "@" before the email (like @[email protected])

async def on_message(message):
    #Check if the message is a DM
    if isinstance(message.channel, discord.DMChannel):
        #Check if the message starts with "@"
      if message.content.startswith("@"):
          email = message.content
           # Check if the email is in the database
          if email in email_database:
            # If the email is in the database, then give the user the Example role
            server = message.guild
            role = discord.utils.get(message.guild.roles, name='Example')
            await message.author.add_roles(role)
            await message.channel.send('Email found in the database! You have been given the Example role!')
          else:
            # If the email is not in the database, then tell the user that the email was not found
            await message.channel.send('Email not found in the database!')

I'm sure about the email database (I insert the code before these lines).

How can I solve this problem? Do i have to specify which server?

Upvotes: 0

Views: 62

Answers (1)

ESloman
ESloman

Reputation: 2102

If the message is sent in a DM then message.guild is going to be None. I'm assuming you're getting an error message. You'll have to get the guild and then the member in the guild and apply the role that way.

@client.event
async def on_message(message):
    #Check if the message is a DM
    if isinstance(message.channel, discord.DMChannel):
        #Check if the message starts with "@"
        if message.content.startswith("@"):
            email = message.content
            # Check if the email is in the database
            if email in email_database:
                # If the email is in the database, then give the user the Example role
                
                # change `client` to `bot` or whatever your client/bot object is called
                server = await client.fetch_guild(YOUR_GUILD_ID)
                role = discord.utils.get(server.roles, name='Example')
                # change this to `await server.fetch_member` if you keep getting None but you're _certain_ they exist
                # shouldn't be necessary though as we're fetching the guild
                server_member = server.get_member(message.author.id)
                if not server_member:
                    # user doesn't exist in guild
                    return
                await server_member.add_roles(role)
                await message.channel.send('Email found in the database! You have been given the Example role!')
            else:
                # If the email is not in the database, then tell the user that the email was not found
                await message.channel.send('Email not found in the database!')

Upvotes: 1

Related Questions