【问题标题】:Discord.js | How to get user's input and make a message for it?不和谐.js |如何获取用户的输入并为其发送消息?
【发布时间】:2021-01-10 07:39:34
【问题描述】:

所以我正在尝试使用 javascript 制作一个 Discord 机器人,但我正在尝试制作它以便它可以获取用户的输入,并且它必须是一个人或类似提及,并将其作为消息。

所以如果我输入!say @Dobie,机器人会这样说@Dobie is a son of a cookie,请帮助我,这将非常有帮助。

【问题讨论】:

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


    【解决方案1】:

    这很容易实现。您必须检查消息中是否有任何提及(Message.mentions),如果是这种情况,您可以将回复发送回频道。

    这是一个例子:


    client.on('message', (message) => {
     // Making sure that the author of the message is not a bot.
     if (message.author.bot) return false;
    
     // Checking if the message's content starts with "!say"
     if (message.content.toLowerCase().startsWith('!say')) {
      // Making sure that there is at least one GuildMember mention within the message.
      if (!message.mentions.members.size) return false;
    
      // Sending the message.
      message.channel.send(
       `${message.mentions.members.first()} is a son of a cookie`
      );
     }
    });
    

    【讨论】:

    • 它实际上效果很好,但是如果我这样做“/say batejibj aebjrotbiajert ajeoitbjaeotjb @person”它仍然有效,那么有什么办法可以解决它吗?我的意思是..如果你不知道如何解决它也没关系。
    • 您必须使用 RegEx 将消息的内容拆分为一个数组,其中包括命令名称和命令的参数。然后,您可以检查该命令是否等于!say。我的只是一个例子。
    猜你喜欢
    • 2021-05-08
    • 2021-05-04
    • 2020-12-12
    • 2020-07-28
    • 2021-08-24
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多