Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript discord.js node.js-机器人回复_Javascript_Node.js_Bots_Discord_Reply - Fatal编程技术网

Javascript discord.js node.js-机器人回复

Javascript discord.js node.js-机器人回复,javascript,node.js,bots,discord,reply,Javascript,Node.js,Bots,Discord,Reply,我已经为我的服务器创建了自己的discord bot,如果我说的是数组中的特定单词,我想回答: 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.au

我已经为我的服务器创建了自己的discord bot,如果我说的是数组中的特定单词,我想回答:

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”,他不会回答我

有人能帮我吗


对不起,我的英语是:这应该可以

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,而是检查消息是否包含在数组中。

这应该可以解决问题

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

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

您可以始终使用
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]);
    }
}
var tabHello=['Bonjour'、'salt'、'Hello'、'Guten tag'、'Buenos Dias'];
var tabAnsw=['Bonjour votre majesté','Hallaments jeune Douzien!','Ouais,Ouais.T's qui déjá','Bonjour'+message.author+',评论vas tu aujourd'hui?';
var content=message.content.split(“”);
对于(var x=0;x
您始终可以使用
来执行
循环

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]);
    }
}
var tabHello=['Bonjour'、'salt'、'Hello'、'Guten tag'、'Buenos Dias'];
var tabAnsw=['Bonjour votre majesté','Hallaments jeune Douzien!','Ouais,Ouais.T's qui déjá','Bonjour'+message.author+',评论vas tu aujourd'hui?';
var content=message.content.split(“”);
对于(var x=0;x
我为您制作了这个,它与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;
    }
}
var tabHello=['bonjour','salt','hello','guten tag','buenos dias'];
var tabAnsw=['Bonjour votre majesté','问候jeune Douzien!','Ouais,Ouais.T's qui déjá','Bonjour'+message.author.username+',评论vas tu aujourd'hui?';
对于(i=0;i

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

我为您制作了这个,它与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;
    }
}
var tabHello=['bonjour','salt','hello','guten tag','buenos dias'];
var tabAnsw=['Bonjour votre majesté','问候jeune Douzien!','Ouais,Ouais.T's qui déjá','Bonjour'+message.author.username+',评论vas tu aujourd'hui?';
对于(i=0;i

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

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

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;

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

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

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;

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

嗨!我在来这里之前尝试过这个,但是机器人没有反应。你是否可以将你的机器人的来源发布到某个地方,或者给我发送一个github链接,这样我就可以查看它,看看出了什么问题?你可以在这里找到这个机器人的来源我删除了令牌发布了一个新的答案和一个改进的版本,如果你需要任何帮助,请告诉我。嗨!我在来这里之前尝试过这个,但是机器人没有反应。你是否可以将你的机器人的来源发布到某个地方,或者给我发送一个github链接,这样我就可以查看它,看看出了什么问题?你可以在这里找到这个机器人的来源我删除了令牌发布了一个新的答案和一个改进的版本,如果你需要任何帮助,请告诉我。