Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

python - Discord.py | add role to someone

I have trouble with making an "add role" command in discord.py. I don't know what is wrong; it just doesn't work.

@client.command()
@commands.has_role("Admin")
async def addrole(ctx):
    user = ctx.message.author
    role = discord.utils.get(user.server.roles, name="Test")
    await client.add_roles(user, role)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
from discord.ext import commands
from discord.utils import get

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

@bot.command(pass_context=True)
@commands.has_role("Admin") # This must be exactly the name of the appropriate role
async def addrole(ctx):
    member = ctx.message.author
    role = get(member.server.roles, name="Test")
    await bot.add_roles(member, role)

I think the only real mistake in your code is the lack of pass_context=True in the @bot.command decorator. You may have seen some code without this, but that likely belongs to the experimental "rewrite" branch of


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...