【问题标题】:How to set Discord.js v13 slash commands permissions?如何设置 Discord.js v13 斜杠命令权限?
【发布时间】:2022-01-09 21:22:51
【问题描述】:

我正在按照 Discord.js 指南为我的机器人创建斜杠命令,但我被困在这一点上:
https://discordjs.guide/interactions/slash-command-permissions.html#user-permissions

有两件事我想不通

  • 我应该在哪里编写我上面链接的这段代码,
  • 我应该如何找到每个命令的 ID

我正在使用SlashCommandBuilderdeploy-commands.js 脚本创建我的命令,如下所述:
https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script
在这里:
https://discordjs.guide/interactions/registering-slash-commands.html#guild-commands

如果你能在这两件事中的任何一件上帮助我,那就太好了!
谢谢!

【问题讨论】:

  • 您能提供您当前的代码吗? (至少有一部分?)
  • 当然,这里:github.com/OIIOIIOI/rd72-bot
  • 我猜你的存储库是私有的,所以我无权访问它。如果需要,可以单击我的个人资料,转到我的 github 个人资料并将我添加到存储库。比我看看
  • 我的错,我改成公开了

标签: javascript discord discord.js


【解决方案1】:

我查看了@discordjs/builders@discordjs/rest 的代码,无法使用这些包设置自定义权限。您可以做的是使用 Discord.js 包创建斜杠命令。通过在Discord.js 包中创建它们,斜杠命令的ID 将在完整的Promise 中返回。使用此 ID,您可以设置命令的权限。这样做的唯一问题是斜杠命令可能需要一段时间才能再次工作。这是一个例子:

const { Client } = require('discord.js');

const client = new Client({intents: ['Your Intents here']});

client.once('ready', () => {
   client.application.commands.create({
      name: 'your_command',
      description: "Your command's description"
   }).then(id => {
      client.application.commands.set({command: id, permissions: [
           id: 'some_user_id',
           type: 'USER',
           permission: false // Can not use the slash command
      ]}).catch(console.log);
   });
});

client.login('Your token here');

我认为还有另一种方法可以做到这一点,但我不太确定。如果我是对的,您还可以在使用 @discordjs/builders@discordjs/rest 包刷新它们后获取所有命令。获取它们后,Promise 将在填满后返回Collection。在Collection 中将包含所有可用于设置权限的斜杠命令的 ID。所以如果这个理论有效,这将是一个例子:

const { Client } = require('discord.js');

const client = new Client({intents: ['Your Intents here']});

client.once('ready', () => {
    // Your refresh code here
    client.application.commands.fetch().then(collection => {
        collection.forEach(command => {
            if(command.name === `The specified command name`){
                 client.application.commands.permissions.set({command: command.id, permissions: [
                     {
                         id: 'some_user_id',
                         type: 'USER',
                         permission: false // Can not use the slash command
                     }
                 ]}).catch(console.log);
            }
        });
    }).catch(console.log);
});

client.login('Your token here');

【讨论】:

  • 在 ready 事件中执行 forEach 循环确实有效!我已经相应地更新了回购。谢谢!
猜你喜欢
  • 2022-01-07
  • 2021-11-09
  • 2022-01-03
  • 1970-01-01
  • 2021-12-15
  • 2021-10-09
  • 2022-07-05
  • 2021-09-09
  • 1970-01-01
相关资源
最近更新 更多