【发布时间】:2022-10-13 03:49:47
【问题描述】:
我正在尝试添加一个查看到我的斜杠命令,看看运行该命令的user.id 是否与我的.json 列表中的user.id 匹配。我可以使用下面的命令在此列表中添加和删除用户,但是,当我将 @commands.check(check_premium) 添加到代码的开头时,用户仍然能够运行代码!
这是其他人提出的一个问题,我将我的代码提交给How to add premium command in discord bot
这是我创建的检查函数,它从.json 文件中获取user.id(这也是我努力工作的原因)
def check_premium(interaction: Interaction):
with open("premium_users.json") as f:
premium_users_list = json.load(f)
if interaction.user.id not in premium_users_list:
return False
return True
例如,这是一个我想对其应用检查的命令,但是当我将它添加到命令的标题时,即使我的 user.id 不在列表中,它仍然允许我运行该命令。
@bot.slash_command(name="test",description="Just a Test Command")
@commands.check(check_premium) # <-- THIS IS WHATS NOT WORKING
async def test(ctx):
embed = nextcord.Embed(title="Test Command",
description="(random words)",
color=nextcord.Color.red(),
timestamp=datetime.datetime.now())
embed.add_field(name="This is a test Command",
value="*It does not matter what I put here*",
inline=False)
await ctx.send(embed=embed)
如果有人对我可能做错了什么有任何见解,我将不胜感激我能得到的任何帮助。谢谢!
【问题讨论】:
-
确保interaction.user.id 与json 的类型(整数、字符串)相同。确保您的 json 是一个仅包含相同类型(整数、字符串)的数组。
-
@TinNguyen 感谢您的意见。我已经在 if int(interaction.user.id) not in premium_users_list 上尝试了 str 和 int:但是它没有解决我的问题,我仍然能够运行该命令。
标签: discord discord.py bots nextcord