Skip to main content
 首页 » 编程设计

python之不和谐给某人添加角色

2025年05月04日23shangdawei

我在 discord.py 中创建“添加角色”命令时遇到了问题。我不知道出了什么问题;它就是行不通。

@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) 

请您参考如下方法:

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) 

我认为您的代码中唯一真正的错误是 @bot.command 装饰器中缺少 pass_context=True。您可能已经看到一些没有这个的代码,但这可能属于 的实验性“重写”分支。