【问题标题】:Nextcord @commands.check() not working. Premium slash commandNextcord @commands.check() 不工作。高级斜杠命令
【发布时间】: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


【解决方案1】:

@commands.check() 用于discord.py,在nextcord 上您需要使用@application_checks.check()。为此,您需要执行以下导入:from nextcord.ext import application_checks

例子:

from nextcord.ext import application_checks

@bot.slash_command(name="test",description="Just a Test Command")
@application_checks.check(check_premium) # <-- Replcaced commands.check by application_checks.check 
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)

我认为更好的另一种选择是直接在您的不和谐服务器中设置命令权限,在Server Settings&gt;Integration中执行此操作

然后通过单击他旁边的manage 选择您的机器人。

一旦你在那里,你就可以为你选择的命令设置权限,就像你为频道所做的那样。

⚠️ 警告:这仅适用于斜杠命令

这是最好的方法,因为当您这样做时,无权访问该命令的用户甚至无法通过执行“/”在命令列表中看到它,而使用另一种方法,他们会看到它并返回他们使用命令时出错。

【讨论】:

    猜你喜欢
    • 2022-11-10
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    相关资源
    最近更新 更多