NodeJS 禁用say命令中的提及(discord.js)

0g0grzrc  于 2023-01-16  发布在  Node.js
关注(0)|答案(3)|浏览(148)

我写了一个简单的"say"命令,但是我试图**禁止提及任何角色和/或用户。例如,如果有人输入"! say@everyone",机器人不会回复"@everyone "并标记每个人,而是回复" You don't have permission!"或者只是执行用户的命令,但过滤掉" everyone "前面的@。
下面是我的代码:

module.exports = {
  name: "say",
  description: "Say command",
  usage: "<msg>",
  run: async (bot, message, args) => {
    if (!message.member.permissions.has("MANAGE_MESSAGES")) return;
    let MSG = message.content.split(`${bot.prefix}say `).join("");
    if (!MSG)
      return message.channel.send(`Non hai specificato il messaggio da inviare!`);
    message.channel.send(MSG);
    message.delete();
  },
};

有人能帮我吗?先谢谢你。

9udxz4iz

9udxz4iz1#

发送消息时只使用messageOptionsallowedMentions

// In discord.js V13, but this works on V12

message.channel.send({ content: "Hello @everyone", allowedMentions: { parse: [] }});

这将使消息输出干净,提及仍将被视为提及**,但不会提及任何人**。

1wnzp6jl

1wnzp6jl2#

或者,如果你想从消息中删除提及,那么你可以使用RegExp:

// If Message Content Includes Mentions
if (message.content.includes(/<@.?[0-9]*?>/g)) {
  //Replace All Message Mentions Into Nothing
  message = message.replace(/<@.?[0-9]*?>/g, "");
};

解释:
我们检查消息内容是否包括提及,如果是,则将所有提及替换为空
链接:
Learn About RegExp
RegExp Source

jtjikinw

jtjikinw3#

或者..只是添加这个客户端选项:(我将给出完整示例)

const Discord = require('discord.js')
  const client = new Discord.Client({
  disableMentions: "everyone", //THIS 
  intents: [
    "GUILDS",
    "GUILD_MESSAGES",
    "GUILD_INTEGRATIONS",
    "GUILD_VOICE_STATES",
    "GUILD_MESSAGE_REACTIONS",
    "DIRECT_MESSAGES"
  ] //optional
});

相关问题