Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在discord.py最新版本中分配角色?_Python_Python 3.x_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python 如何在discord.py最新版本中分配角色?

Python 如何在discord.py最新版本中分配角色?,python,python-3.x,discord.py,discord.py-rewrite,Python,Python 3.x,Discord.py,Discord.py Rewrite,我已经找了一整天了,我似乎找不到一个合适的方式来分配角色给成员。我尝试了几种分配角色的方法: @client.command(pass_context=True) async def claimrank(ctx, role: discord.Role): user = ctx.message.author await user.add_roles(role='Rookie') 以及: 更糟糕的是,通过这两次尝试,我没有得到任何错误,但代码没有做任何事情。 请帮忙! 提前谢谢。我

我已经找了一整天了,我似乎找不到一个合适的方式来分配角色给成员。我尝试了几种分配角色的方法:

@client.command(pass_context=True)
async def claimrank(ctx, role: discord.Role):
    user = ctx.message.author
    await user.add_roles(role='Rookie')
以及:

更糟糕的是,通过这两次尝试,我没有得到任何错误,但代码没有做任何事情。 请帮忙!
提前谢谢。

我今天从一篇帖子上得到了这个。我将编写代码并查找原始帖子

@client.command(pass_context=True)
async def addrole(ctx):
    user = ctx.message.author 
    role = 'role' #change the role here
    try:
        await user.add_roles(discord.utils.get(user.guild.roles, name=role)) 
    except Exception as e:
        await ctx.send('Cannot assign role. Error: ' + str(e))
找到@Patrick Haugh的原始帖子:

@client.command(pass_context=True)
async def addrole(ctx):
    user = ctx.message.author 
    role = 'role' #change the role here
    try:
        await user.add_roles(discord.utils.get(user.guild.roles, name=role)) 
    except Exception as e:
        await ctx.send('Cannot assign role. Error: ' + str(e))