2017-05-30 56 views
1

我已经创建了自己的不和谐机器人为我的服务器,我想回答我,如果我说的是谁在阵列tabHello特定的单词:discord.js的Node.js - 机器人回复

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数组。 而且,如果说“你好@bot”,他不回答我。

有人可以帮助我吗?

对不起,我的英语:■

回答

0

您可以随时使用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]); 
    } 
} 
+0

噢,谢谢你! :) – SalakissODV

0

这应该做的伎俩

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

所以不是检查消息为世界你好,这种检查如果消息包含在数组中。

+0

嗨!我在来这里之前试过这个,但是没有反应。 – SalakissODV

+0

是否有反正你可以发布你的机器人的地方或给我一个github链接,所以我可以检查出来,看看有什么不对? –

+0

哟,可以在这里找到这个机器人的源https://github.com/SalakissODV/Jollia我删除了令牌 – SalakissODV

0

我去了,使这个对你来说,它的工作原理与厄里斯我试图将其转换为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的所有内容,这样以后你可以忽略用户的外壳为小写的版本,例如,如果约翰#1234是进入“@Bot你好”它仍然会工作,因为我们忽略套管。

0

我已经建立了这个小脚本,让您可以在此之上构建机器人:

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; 

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