noel puig
noel puig

Reputation: 73

discord.py add role on message

Ive seen a lot of questions about this and none of them worked for me, I don't understand why something that sounds this simple is that complicated, alredy spent more than 4 hours on this, I want to make a basic bot to make new users accept the rules:

Not much to explain, just a basic bot that when you say accept in a special channel it should add you a role.

import discord
from discord.utils import get

client = discord.Client()
TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'



@client.event
async def on_ready():
    #creates a message for users to react to
    guild = client.guilds
    channel = client.get_channel(836583535981608530)
    Text= "Type accept below if you understand and `accept` ALL the rules in <#836600011484785843>, in order to gain access to the server:"
    await channel.send(Text)

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    
    channel = message.channel
    if channel.id == 836593532981608530 and message.content.lower() == ('accept'):
        await message.delete()
        user_role = discord.utils.get(message.guild.roles, name = "role name")
        new_member = message.author
        new_member.add_role(user_role, reason = "new member")
    elif channel.id == 836593532981608530:
        await message.delete()

Upvotes: 1

Views: 258

Answers (1)

Bhavyadeep Yadav
Bhavyadeep Yadav

Reputation: 831

The problem here in your command is that you have not awaited statement(s) that had to be awaited for library to work.

So, let's start.

The statement(s) that needs to be awaited:

new_member.add_role(user_role, reason = "new member")

How to fix this error?

You need to await it just like you have awaited other statements in your code.

Just change the line to:

await new_member.add_roles(user_role, reason="new member")

This would solve the problem you have facing.

Why do you need to await some statements?

Read the docs from the following link to see why some statements are needed to be awaited. This would help you to figure out what commands has to be awaited in future.


Hope it helps. If still you have any problem, feel free to ask me in the comments. :)

Thank You! :D

Upvotes: 1

Related Questions