discord.py - 为什么我不能限制我的机器人使用命令 - discord.py
问题描述
'''
@client.event
@commands.has_role('xrole')
async def on_message(message):
#Commands
if message.content.startswith("!set"):
credits_message = message.content.split(" ", 2)
user_name = credits_message[1]
credit_add_amount = credits_message[2]
db[user_name] = int(credit_add_amount)
#DB Stuff
await message.channel.send(user_name + "has been set with " + credit_add_amount + ", For further credit adgustment use !add")
'''
出于某种原因,无论有没有 xrole 的用户都可以访问机器人,即使有 @commands 限制
没有 xrole 的用户^
具有 xrole 的用户 ^
解决方案
首先不要on_message
用于命令,最好使用@bot.command
第二:这段代码应该适合你
@bot.command(pass_context=True)
@commands.has_role("xrole")
async def role_check(ctx):
await ctx.channel.send('''you have the role "xrole"''')
@role_check.error
async def info_error(ctx, error):
if isinstance(error, (commands.MissingRole, commands.MissingAnyRole)):
await ctx.channel.send('''you do not have the role "xrole"''')
推荐阅读
- java - 数组索引越界 - Java 递归方法
- python - 无法在 Mendel LINux 上安装 scipy 模块
- django - Django 1.11 中间件如何正确重定向
- node.js - 错误:升级到 Gulp v4 后没有为“]a(r)”注册解析器
- environment-variables - 如何让 Netlify 解析我放入环境变量中的很长的私钥?
- excel - Excel VBA命令按钮完成查找后转到单元格
- javascript - ChartJS 和 DiscordJS 实现问题
- arrays - 我需要帮助编写 AWK 脚本以按行分组并查找值的最小值/最大值/平均值
- python - 谷歌日历事件持续时间 Python
- vue.js - Eslint vue lint html元素换行符