输入命令时如何发出角色?

How do I issue a role when entering a command?

我需要在输入命令时,为输入命令的人赋予特定角色。我有一个代码,但如果你输入一个命令和一个昵称(.command @nickname),它就会起作用,我需要让它在你输入命令(.command)时起作用。我怎样才能重做代码以使其工作?

@client.command(pass_context = True )
@commands.has_role("role1234")
async def command (ctx, author: discord.Member):
    role0 = ctx.guild.get_role(981971100898582539) 
    role1 = ctx.guild.get_role(981594840879988807)
    role3 = ctx.guild.get_role(981971134893395990) 
    role2 = discord.utils.get(ctx.guild.roles, id = 981971065486057492)
    if role2 in author.roles:
        await ctx.channel.purge( limit = 1000)
        await ctx.author.add_roles(role3)
        await ctx.author.remove_roles(role1)
        await ctx.author.remove_roles(role2)
        await ctx.author.remove_roles(role0)

[...] but it works if you enter a command and a nickname (.command @nickname), I need to make it work just when you enter a command

我完全理解。那么,您知道如何获取 author 作为 discord.Member 对象而不将其作为参数传递给命令吗?

async def command(ctx):
    author = ctx.author # Then you can use this variable in the whole scope of the function

因此,在您的情况下,除了函数原型外,唯一需要更改的是这一行:

if role2 in author.roles:

应该变成:

if role2 in ctx.author.roles: