momotax
momotax

Reputation: 1

Add role to user with user mention

I was trying to make a bot that is able to add a role to a user with a user mention. I also want to pass the role in the command. So the syntax should be:
!addrole ROLENAME @user
I tried this:

import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get

def read_token():
    with open("token.txt", "r") as f:
        lines = f.readlines()
        return lines[0].strip()

token = read_token()

bot = commands.Bot(command_prefix='!')


@bot.command(pass_context=True)
async def addrole(ctx, arg1, member: discord.Member):
    role = get(member.guild.roles, name={arg1})
    await member.add_roles(role)

bot.run(token)

but it doesn't work. I receive the error:
AttributeError: 'NoneType' object has no attribute 'id'

Upvotes: 0

Views: 63

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60974

You're already using a converter for member, use one for role too:

from discord import Member, Role

@bot.command()
async def addrole(ctx, member: Member, *, role: Role):
    await member.add_roles(role)

The , *, allows you to supply role names that have multiple words.

Upvotes: 1

Related Questions