2017-05-30 9 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こんにちは"と言うと、彼はtabAnsw配列の1つの値に答えます。しかし、もし私がtabHelloの配列の値を言うなら、私は私に答えたいと思う。 "Hello @bot"と言うと、彼は私に答えません。

誰かが私を助けることができますか?

私の英語のため申し訳ありません

:代わりにHello Worldのためのメッセージを確認するメッセージがアレイ内に含まれている場合の

答えて

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

私たちは無視しているので、それはまだ動作しますジョン#1234「@Botこんにちは」を入力した場合ので、後であなたは、たとえば、ユーザーのケースを無視することができます小文字バージョンにtabHelloのすべてのコンテンツを行って、変換ケーシング。

index.js:あなたはこの上にあなたのボットを構築することができますので、私はこの小さなスクリプトを設定している

0

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; 

まだ改善とエラー処理の余地がありますが、私はあなたに任せます。がんばろう!

関連する問題