0
votes

I'm creating a bot for my discord server. And when a user says a specific command the bot will give them a role

I've already tried finding other codes in the other stackoverflow questions and looked at the the Discord.Py Rewrite Documentation. Also for videos, they are all not discord.py rewrite

@bot.command()
async def recruitme(ctx):
    user = ctx.message.author
    role = discord.utils.get(user.guild.roles, name="Recruit")
    await user.guild.roles(role)

I expect that I can learn how to code that a bot gives a role.

1
This answer covers how to create and add roles.Anu6is
@Anu6is what do you mean?TheBigBro122
@Anu6is But its not working though.....TheBigBro122
@Anu6is wait nvm. I've foind the solution because of you. Thank youTheBigBro122
lol, no problemAnu6is

1 Answers

2
votes
@client.command()
    async def role(ctx, member:discord.Member, role: discord.Role): #pass user and role
        if role in member.roles: #checks all roles the member has
            await member.remove_roles(role) #removes the role
        else:
            await member.add_roles(role) #adds the role

In this, the code first checks for the specified role in all roles the member has. It adds the role if it doesn't exist and removes it if it already exists.