【问题标题】:discord.js node.js - bot replydiscord.js node.js - 机器人回复
【发布时间】:2017-10-31 06:28:50
【问题描述】:

我已经为我的服务器创建了自己的不和谐机器人,如果我说出数组 tabHello 中的特定单词,我想回答我:

var tabHello = ['Bonjour','Salut','Hello', 'Guten tag', 'Buenos Dias'];
    var tabAnsw= ['Bonjour votre majesté.','Salutations jeune Douzien !','Ouais, ouais. T\'es qui déjà ?', 'Bonjour ' + message.author + ', comment vas-tu aujourd\'hui ?'];

    if (message.content.indexOf('Hello') > 0  && message.isMentioned(client.user)){
        var row = Math.floor(Math.random() * tabAnsw.length);
        message.channel.sendMessage(tabAnsw[row]);
    } 

使用此代码,如果我说“@bot Hello”,他会回答 tabAnsw 数组的一个值。但是如果我说 tabHello 数组的一个值,我想回答我。 而且,如果说“Hello @bot”,他不会回答我。

有人可以帮助我吗?

对不起我的英语:s

【问题讨论】:

    标签: javascript node.js bots discord reply


    【解决方案1】:

    我已经设置了这个小脚本,因此您可以在此基础上构建您的机器人:

    index.js:

    const Discord = require('discord.js');
    const client = new Discord.Client();
    const config = require('./config.json');
    const commands = require('./commands');
    const prefix = config.prefix;
    
    const commandExecuter = new commands();
    
    client.on("ready", () => {
        client.user.setGame('Minecraft');
        var servers = client.guilds.array().map(g => g.name).join('.');
        console.log('Bot started');
    });
    
    client.on('message', message => {
        //Check if its a command
        isBotCommand(message.content, (command) => {
            //If it is, lets execute it if we can
            if ( command ) {
                commandExecuter.execute(message, client, command);
            }
        });
    });
    
    
    
    const isBotCommand = (message, callback) => {
        //Get the first char of the message
        let firstChar = message.charAt(0);
        //If it does not equal our prefix answer that it's not a bot command
        if (firstChar !== prefix) return callback(false)
        //We got here, so it seems to be a command
        return callback(message.substring(1));
    }
    
    client.login(config.token);
    

    将文件“commands.js”添加到您的根目录并粘贴以下内容:

    const botCommandExecuter = function() {}
    
    const findCommandFromStack = (command, callback) => {
        //Find the command in the commands array
        commands.some((iteratedCommand) => {
            //If our keyword is inside the currently iterated command object we have a match
            if ( iteratedCommand.keywords.indexOf(command) > -1 ) {
                //Call the callback and break the loop
                callback(iteratedCommand.action);
                return true;
            }
        });
    }
    
    botCommandExecuter.prototype.execute = (messageInstance, client, command) => {
        //Find the command
        findCommandFromStack(command, (commandToExecute) => {
            //Execute the command we found
            commandToExecute(messageInstance, client);
        });
    }
    
    //List of commands
    const commands = [
        {
            keywords: ['Bonjour','Salut','Hello', 'Guten tag', 'Buenos Dias'],
            action: (message, client) => {
                var tabAnsw = ['Bonjour votre majesté.','Salutations jeune Douzien !','Ouais, ouais. T\'es qui déjà ?', 'Bonjour ' + message.author + ', comment vas-tu aujourd\'hui ?'];
                var row = Math.floor(Math.random() * tabAnsw.length);
                message.channel.sendMessage(tabAnsw[row]);
            }
        }
    ];
    
    module.exports = botCommandExecuter;
    

    还有很多改进和错误处理的空间,但我会留给你。祝你好运!

    【讨论】:

      【解决方案2】:

      我去为你做了这个,它适用于 Eris 我试图将它转换为 discord.js,它应该可以工作,但不能 100% 确定它会。

      var tabHello = ['bonjour', 'salut', 'hello', 'guten tag', 'buenos dias'];
      var tabAnsw = ['Bonjour votre majesté.', 'Salutations jeune Douzien !', 'Ouais, ouais. T\'es qui déjà ?', 'Bonjour ' + message.author.username + ', comment vas-tu aujourd\'hui ?'];
      
      for (i = 0; i < tabAnsw.length; i++) {
          if (message.content.startsWith(client.user.mention) && message.content.toLowerCase().indexOf(tabHello[i])) {
              var row = Math.floor(Math.random() * tabAnsw.length);
              message.channel.sendMessage(tabAnsw[row]);
              break;
          }
      }
      

      我将 tabHello 的所有内容转换为小写版本,以便稍后您可以忽略用户的大小写,例如,如果 John#1234 输入“@Bot HeLlO”,它仍然可以工作,因为我们忽略了大小写。

      【讨论】:

        【解决方案3】:

        您总是可以只使用for 循环。

        var tabHello = ['Bonjour','Salut','Hello', 'Guten tag', 'Buenos Dias'];
        var tabAnsw = ['Bonjour votre majesté.','Salutations jeune Douzien !','Ouais, ouais. T\'es qui déjà ?', 'Bonjour ' + message.author + ', comment vas-tu aujourd\'hui ?'];
        var content = message.content.split(' ');
        
        for(var x = 0; x < content.length; x++){
            if(tabHello.includes(content[x]) && message.isMentioned(client.user)){
                var row = Math.floor(Math.random() * tabAnsw.length);
                message.channel.send(tabAnsw[row]);
            }
        }
        

        【讨论】:

          【解决方案4】:

          这应该可以解决问题

          var tabHello = ['Bonjour','Salut','Hello', 'Guten tag', 'Buenos Dias'];
          var tabAnsw= ['Bonjour votre majesté.','Salutations jeune Douzien !','Ouais, ouais. T\'es qui déjà ?', 'Bonjour ' + message.author + ', comment vas-tu aujourd\'hui ?'];
          
          if (tabHello.indexOf(message.content) > -1  && message.isMentioned(client.user)){
              var row = Math.floor(Math.random() * tabAnsw.length);
              message.channel.sendMessage(tabAnsw[row]);
          } 
          

          因此,它不是检查消息是否为 world hello,而是检查消息是否包含在数组中。

          【讨论】:

          • 嗨!我来这里之前试过这个,但机器人没有反应。
          • 您是否可以在某处发布您的机器人的源代码或将 github 链接发送给我,以便我检查一下,看看有什么问题?
          • 你可以在这里找到这个机器人的来源github.com/SalakissODV/Jollia我删除了令牌
          • 发布了一个带有改进版本的新答案,如果您需要任何帮助,请告诉我。
          猜你喜欢
          • 2018-02-19
          • 2021-08-09
          • 2020-05-24
          • 2019-02-16
          • 1970-01-01
          • 2021-10-14
          • 2020-09-16
          • 2021-06-26
          • 2021-11-10
          相关资源
          最近更新 更多