【问题标题】:Discord.js: how can I make paticular permissons for each reaction?Discord.js:如何为每个反应设置特定权限?
【发布时间】:2021-08-18 16:38:24
【问题描述】:

我正在编写 !ticket 命令,无法处理允许没有任何权限的成员做出反应 ⛔。

代码

module.exports = {
  name: "ticket",
  slash: true,
  aliases: [],
  permissions: [],
  description: "open a ticket!",
  async execute(client, message, args) {
    let chanel = message.guild.channels.cache.find(c => c.name === `ticket-${(message.author.username).toLowerCase()}`);
    if (chanel) return message.channel.send('You already have a ticket open.');
    const channel = await message.guild.channels.create(`ticket-${message.author.username}`)
    
    channel.setParent("837065612546539531");

    channel.updateOverwrite(message.guild.id, {
      SEND_MESSAGE: false,
      VIEW_CHANNEL: false,
    });
    channel.updateOverwrite(message.author, {
      SEND_MESSAGE: true,
      VIEW_CHANNEL: true,
    });

    const reactionMessage = await channel.send(`${message.author}, welcome to your ticket!\nHere you can:\n:one: Report an issue or bug of the server.\n:two: Suggest any idea for the server.\n:three: Report a staff member of the server.\n\nMake sure to be patient, support will be with you shortly.\n<@&837064899322052628>`)

    try {
      await reactionMessage.react("????");
      await reactionMessage.react("⛔");
    } catch (err) {
      channel.send("Error sending emojis!");
      throw err;
    }

    const collector = reactionMessage.createReactionCollector(
      (reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission("ADMINISTRATOR"),
      { dispose: true }
    );

    collector.on("collect", (reaction, user) => {
      switch (reaction.emoji.name) {
        case "????":
          channel.updateOverwrite(message.author, { SEND_MESSAGES: false });
          break;
        case "⛔":
          channel.send("Deleting this ticket in 5 seconds...");
          setTimeout(() => channel.delete(), 5000);
          break;
      }
    });

    message.channel
      .send(`We will be right with you! ${channel}`)
      .then((msg) => {
        setTimeout(() => msg.delete(), 7000);
        setTimeout(() => message.delete(), 3000);
      })
      .catch((err) => {
        throw err;
      });
  },
};

与下面部分代码有关。

const collector = reactionMessage.createReactionCollector(
      (reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission("ADMINISTRATOR"),
      { dispose: true }
    );

我希望它允许为管理员锁定票证,并允许为所有人关闭。

【问题讨论】:

  • 所以您希望⛔ 反应仅由具有特定权限级别的用户使用?编辑刚刚看到帖子底部
  • @FAXES 好吧,哈哈

标签: javascript node.js discord discord.js bots


【解决方案1】:

不确定我是否理解正确,但您似乎有两种反应,只希望管理员使用?,而管理员和原作者都使用

您当前的代码仅收集拥有ADMINISTRATOR 权限的成员的反应。您应该更改过滤器以收集创建工单的成员的反应。

下面的过滤器正是这样做的。

const filter = (reaction, user) => {
  const isOriginalAuthor = message.author.id === user.id;
  const isAdmin = message.guild.members.cache
    .find((member) => member.id === user.id)
    .hasPermission('ADMINISTRATOR');

  return isOriginalAuthor || isAdmin;
}

您的代码中还有其他错误,例如没有SEND_MESSAGE 标志,只有SEND_MESSAGES。您还应该使用更多的 try-catch 块来捕获任何错误。

明确允许机器人在新创建的频道中发送消息也是一个好主意。我使用overwritePermissions 而不是updateOverwrite。它允许您使用覆盖数组,因此您可以使用单个方法对其进行更新。

为了解决锁表情符号的问题...我检查了使用? 做出反应的成员的权限,如果没有ADMINISTRATOR,我只需使用reaction.users.remove(user) 删除他们的反应。

查看下面的工作代码:

module.exports = {
  name: 'ticket',
  slash: true,
  aliases: [],
  permissions: [],
  description: 'open a ticket!',
  async execute(client, message, args) {
    const username = message.author.username.toLowerCase();
    const parentChannel = '837065612546539531';
    const ticketChannel = message.guild.channels.cache.find((ch) => ch.name === `ticket-${username}`);
    if (ticketChannel)
      return message.channel.send(`You already have a ticket open: ${ticketChannel}`);

    let channel = null;
    try {
      channel = await message.guild.channels.create(`ticket-${username}`);

      await channel.setParent(parentChannel);
      await channel.overwritePermissions([
        // disable access to everyone
        {
          id: message.guild.id,
          deny: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
        },
        // allow access for the one opening the ticket
        {
          id: message.author.id,
          allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
        },
        // make sure the bot can also send messages
        {
          id: client.user.id,
          allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
        },
      ]);
    } catch (error) {
      console.log(error);
      return message.channel.send('⚠️ Error creating ticket channel!');
    }

    let reactionMessage = null;
    try {
      reactionMessage = await channel.send(
        `${message.author}, welcome to your ticket!\nHere you can:\n:one: Report an issue or bug of the server.\n:two: Suggest any idea for the server.\n:three: Report a staff member of the server.\n\nMake sure to be patient, support will be with you shortly.\n<@&837064899322052628>`,
      );
    } catch (error) {
      console.log(error);
      return message.channel.send(
        '⚠️ Error sending message in ticket channel!',
      );
    }

    try {
      await reactionMessage.react('?');
      await reactionMessage.react('⛔');
    } catch (err) {
      console.log(err);
      return channel.send('⚠️ Error sending emojis!');
    }

    const collector = reactionMessage.createReactionCollector(
      (reaction, user) => {
        // collect only reactions from the original
        // author and users w/ admin permissions
        const isOriginalAuthor = message.author.id === user.id;
        const isAdmin = message.guild.members.cache
          .find((member) => member.id === user.id)
          .hasPermission('ADMINISTRATOR');

        return isOriginalAuthor || isAdmin;
      },
      { dispose: true },
    );

    collector.on('collect', (reaction, user) => {
      switch (reaction.emoji.name) {
        // lock: admins only
        case '?':
          const isAdmin = message.guild.members.cache
            .find((member) => member.id === user.id)
            .hasPermission('ADMINISTRATOR');

          if (isAdmin) {
            channel.updateOverwrite(message.author, {
              SEND_MESSAGES: false,
            });
          } else {
            // if not an admin, just remove the reaction
            // like nothing's happened
            reaction.users.remove(user);
          }
          break;
        // close: anyone i.e. any admin and the member
        // created the ticket
        case '⛔':
          channel.send('Deleting this ticket in 5 seconds...');
          setTimeout(() => channel.delete(), 5000);
          break;
      }
    });

    try {
      const msg = await message.channel.send(`We will be right with you! ${channel}`);
      setTimeout(() => msg.delete(), 7000);
      setTimeout(() => message.delete(), 3000);
    } catch (error) {
      console.log(error);
    }
  },
};

【讨论】:

  • 感谢您提供如此详细的回答,即使有工作示例,您也很棒!
猜你喜欢
  • 2018-03-01
  • 2021-01-02
  • 2022-01-09
  • 2016-10-18
  • 2019-04-04
  • 1970-01-01
  • 2021-07-17
  • 2016-02-22
  • 1970-01-01
相关资源
最近更新 更多