我正在执行一个PURGE命令,但我不知道如何将其使用限制为具有MANAGE_MESSAGES权限的用户.

我提供了客户端的源代码.on("Ready")部分以及整个命令.

对这件事真的一无所知,谢谢你的帮助.

client.on('ready', async () => {
  try {
    const command = await client.application.commands.create({
      name: 'purge',
      PermissionFlagsBits: 'MANAGE_MESSAGES',
      description: 'Deletes messages',
      options: [
        {
          name: 'amount',
          description: 'The amount of messages to delete',
          type: 4,
          required: true,
        },
      ],
      

      defaultPermission: false,
    });
    console.log(`Registered ${command.name} command`);
  } catch (error) {
    console.error(error);
  }
});


  client.on('interactionCreate', async (interaction) => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === 'purge') {
      const amount = interaction.options.getInteger('amount');
      if (amount > 100) {
        return interaction.reply({ content: 'You can only delete 100 messages at a time!', ephemeral: true });
      }
      if (amount < 1) {
        return interaction.reply({ content: 'You must delete at least 1 message!', ephemeral: true });
      }
      await interaction.channel.bulkDelete(amount, true).catch(error => { 
        console.error(error);
        interaction.reply({ content: 'There was an error trying to delete messages in this channel!', ephemeral: true });
      });
      interaction.reply({ content: `Successfully deleted ${amount} messages!`, ephemeral: true });
    }
  });
  

推荐答案

在Discord.js v14中,这对我很有效.使用PermissionFlagsBits.ManageMessages将返回管理邮件的正确权限.此外,这处房产是default_member_permissions,而不是PermissionFlagsBits.

import { PermissionFlagsBits } from 'discord.js';

client.on('ready', async () => {
    try {
        const command = await this.application.commands.create({
            name: 'purge',
            default_member_permissions: PermissionFlagsBits.ManageMessages,
            description: 'Deletes messages',
            options: [
                  {
                    name: 'amount',
                    description: 'The amount of messages to delete',
                    type: 4,
                    required: true,
                  },
                ],
             defaultPermission: false,
       });

        console.log(`Registered ${command.name} command`);
    } catch (error) {
        console.error(error);
    }
});

Javascript相关问答推荐

功能和普通对象之间的原型污染

如何在react + react路由域名中使用DeliverBrowserRouter?

从mat—country—select获取整个Country数组

将自定义排序应用于角形数字数组

将本机导航路由react 到导航栏中未列出的屏幕?

如何从html元素创建树 struct ?

使用LocaleCompare()进行排序时,首先使用大写字母

如何避免页面第一次加载时由于CSS样式通过JavaScript更改而出现闪烁

我创建了一个创建对象的函数,我希望从该函数创建的对象具有唯一的键.我怎么能做到这一点?

我可以使用使用node.js创建的本地主机来存储我网站上提交的所有数据吗?没有SQL或任何数据库.只有HTML语言

不能将空字符串传递给cy.containes()

闭包是将值复制到内存的另一个位置吗?

在Matter.js中添加从点A到另一个约束的约束

使用getBorbingClientRect()更改绝对元素位置

DOM不自动更新,尽管运行倒计时TS,JS

TypeError:无法读取未定义的属性(正在读取';宽度';)

有没有办法更改Chart.js 3.x.x中主要刻度的字体?

未找到用于 Select 器的元素:in( puppeteer 师错误)

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

如何在移动设备中使用JAVASSCRIPT移除点击时的焦点/悬停状态