【问题标题】:Disable mentions in say command (discord.js)禁用say命令中的提及(discord.js)
【发布时间】:2021-04-24 20:24:57
【问题描述】:

我写了一个简单的“说”命令,但我试图禁止提及任何角色和/或用户。 例如,我输入“!say @everyone”,机器人应该回复“你没有权限!”而不是回复“@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();
  },
};

有人可以帮助我吗?提前谢谢你。
附:最后抱歉英语不好。

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    或者,如果您想从消息中删除提及,那么您可以使用 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

    【讨论】:

      【解决方案2】:

      发送消息时只需使用messageOptions 中的allowedMentions

      // In discord.js V13, but this works on V12
      
      message.channel.send({ content: "Hello @everyone", allowedMentions: { parse: [] }});
      

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

      【讨论】:

        【解决方案3】:

        您可以使用简单的#includes 语句。

        if (message.content.includes('@')) {
           return message.channel.send(':x: You do not have permission'); // if you want to deny completely
        
           let cleanedContent = message.content.replace('@', ''); // if you want to replace the @
        };
        

        注意:我不知道你的参数在你的处理程序中是如何构成的,所以你可能需要稍微修改我的代码,但这会满足你的要求。

        function myFunction() {
          var str = document.getElementById("demo").innerHTML; 
          var res = str.replace("@", "");
          document.getElementById("demo").innerHTML = res;
        }
        <!DOCTYPE html>
        <html>
        <body>
        
        <p id="demo">!say @everyone</p>
        
        <button onclick="myFunction()">Try it</button>
        
        </body>
        </html>

        【讨论】:

          【解决方案4】:

          或者.. 只需在客户端选项中添加这个:(我将展示完整示例)

            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
          });
          

          【讨论】:

            猜你喜欢
            • 2021-11-07
            • 1970-01-01
            • 2020-09-11
            • 2020-03-05
            • 1970-01-01
            • 2021-12-07
            • 2021-04-18
            • 2021-07-27
            • 2021-01-18
            相关资源
            最近更新 更多