【问题标题】:How would I give someone a role using discord.py?我如何使用 discord.py 给某人一个角色?
【发布时间】:2020-09-26 17:51:07
【问题描述】:

我知道以前有人问过这个问题,但我的问题有点不同,我将如何创建一个命令来赋予其他人一个角色?例如

[p]giverole @user

会给他们一个角色吗?我知道如何使用 discord.py 为作者赋予角色,但无法弄清楚如何将其赋予所提到的人。任何帮助表示赞赏

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    您可以在这样的命令中使用参数:

    @bot.command()
    async def giverole(ctx, member: discord.Member=None):
     # if no member is provided, the bot will give the role to the cmd author
        if not member:
            member = ctx.author
        role = ctx.guild.get_role(112233445566778899) # role ID goes there
        await member.add_roles(role)
        await ctx.send(f"Successfully gave {member} the {role.name} role!")
    

    这比使用on_message() 事件更可取,因为使用装饰器注册的命令时处理参数会容易得多。


    参考资料:

    【讨论】:

    • 谢谢!我可能遗漏了一些小东西,但我收到了错误 name 'discord' is not defined 你介意帮忙吗?
    • 感谢您的快速回复?‍♀️并且不...修复了它,我很愚蠢哈哈
    • 哈哈不用担心!很高兴你现在可以正常工作了!快乐编码:^)
    猜你喜欢
    • 2021-10-28
    • 2019-10-28
    • 2021-10-19
    • 2018-09-16
    • 1970-01-01
    • 2021-04-16
    • 2021-08-13
    • 2020-04-09
    • 2021-05-05
    相关资源
    最近更新 更多