且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用机器人 Discord.py 授予和删除角色

更新时间:2023-11-19 11:33:58

我不确定你的 role.json 文件在哪里发挥作用,但我将如何实现这样的命令

I'm not sure where your role.json file comes into play, but here's how I would implement such a command

@bot.command(name="role")
async def _role(ctx, role: discord.Role):
    if role in ctx.author.roles:
        await ctx.author.remove_roles(role)
    else:
        await ctx.author.add_roles(role)

这使用 Role 转换器 自动从 role 对象的名称、id 或提及中解析.

This uses the Role converter to automatically resolve the role object from its name, id, or mention.