discord.py - 只允许特定角色使用命令
问题描述
我一直在发出命令来更改用户的角色,但目前,每个人都可以使用它。我已经尝试过做一些事情。这是我的代码:
async def addrole(ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"I gave {user.name} the role {role.name}")
这是我唯一能想到的:
allowed = ["Role 1"]
if message.author.role in allowed:
async def addrole(ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"I gave {user.name} the role {role.name}")
提前致谢!
解决方案
只需添加@commands.has_any_roles()
:
@bot.command()
@commands.has_any_roles("Role 1", "Role 2")
async def addrole(ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"I gave {user.name} the role {role.name}")
如果不允许用户使用该命令,则会引发commands.MissingAnyRole
错误。
推荐阅读
- c# - Qlik Desktop - 自定义连接器 - ExtractQuery 和其他问题上的错误
- sql - 集团历史数据
- android - 任务:app:bundleReleaseJsAndAssets 失败 - React Native - Android
- multinomial - 多项 logit 模型的 dimnames(x) 错误
- c# - EF 6.4 单元测试对框架运行正常,但 dotnet 5.0 找不到我的连接字符串
- powershell - 将文件夹/子中的所有最新文件复制到powershell中的同名文件夹/子中
- excel-formula - Excel动态数组公式创建列的运行积
- java - 是否可以手动对 Firebase 自动生成的键进行排序?
- javascript - React.js 状态过滤错误。(无法读取未定义的属性“toLowerCase”)
- numpy - 将所有元素左侧的索引设置为 1,将索引右侧的索引设置为零以获取索引列表