【问题标题】:Discord.js. Cannot add permissions to slash commands不和谐.js。无法为斜杠命令添加权限
【发布时间】:2021-10-14 06:59:42
【问题描述】:

我试图以这种方式为命令添加权限,只有具有特定角色的用户才能使用它。起初我正在创建命令,就像它在文档中所说的那样。

然后我尝试获取每个命令并向它们添加新权限:

await commands.forEach(command => {
    const permissions2 = [
        {
            id: guild.roles.everyone.id,
            type: 'ROLE',
            permission: false,
        }
    ];
    const permissions1 = [
        {
            id: botRole.id,
            type: 'ROLE',
            permission: true,
        },
    ];

    console.log(`Changing command ${command.id}`);
    command.permissions.add(permissions2);
    command.permissions.add(permissions1);
});

但无论我做什么我都会得到这个错误:TypeError [INVALID_TYPE]: Supplied permissions is not an Array of ApplicationCommandPermissionData. 我也尝试过运行此代码,如文档中所示,但得到了相同的结果:

await commands.forEach(command => {
    ...
    console.log(`Changing command ${command.id}`);
    command.permissions.add({permissions2});
    command.permissions.add({permissions1});
});

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    将代码更改为此有帮助:

    const permissions2 = {
            id: guild.roles.everyone.id,
            type: 'ROLE',
            permission: false,
        };
        const permissions1 =  {
            id: botRole.id,
            type: 'ROLE',
            permission: true,
        };
        let commandsList = await guild.commands.fetch();
        await commandsList.forEach(slashCommand => {
            console.log(`Changing command ${slashCommand.id}`);
            //set the permissions for each slashCommand
            guild.commands.permissions.add({
                command: slashCommand.id,
                permissions: [permissions1, permissions2]
            });
        });
    

    【讨论】:

      【解决方案2】:

      我认为你不能像以前那样使用 forEach 循环。

      commands.permissions#set 由一个带有您要编辑的命令的 id 的对象和一个带有权限的数组组成。

      因此您必须将代码重写为:

      //create the permissions objects
      const permissions2 = {
        id: guild.roles.everyone.id,
        type: 'ROLE',
        permission: false,
      };
      const permissions1 =  {
        id: botRole.id,
        type: 'ROLE',
        permission: true,
      };
      
      
      //loop through all the slashCommands
      await commands.forEach(slashCommand => {
          console.log(`Changing command ${slashCommand.id}`);
      
          //set the permissions for each slashCommand
          client.application.commands.permissions.set({command: slashCommand.id, permissions: [permissions1, permissions2]});
      });
      

      阅读更多关于为 slashCommands 设置权限here

      【讨论】:

      • guild.commands 是 GuildApplicationCommandManager,它不允许使用 forEach。但是换括号有帮助,谢谢
      • @AlexanderShashkov 我改变了我的答案。它现在应该可以像我在这里展示的那样工作了。
      • 由于某种原因,它仍然给我 TypeError:client.application.commands.forEach is not a function。文档说 application.commands 是 ApplicationCommandManager
      • @AlexanderShashkov 因为我自己无法测试代码,所以我认为commands.forEach 像你一样应该可以正常工作。很抱歉给您带来不便
      猜你喜欢
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2021-09-09
      • 2021-04-06
      • 1970-01-01
      • 2021-06-30
      • 2020-10-16
      • 2022-01-15
      相关资源
      最近更新 更多