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

discord.js node.js - bot reply

我已经为我的服务器创建了自己的 discord 机器人,如果我说数组中的特定单词,我想回答我 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

这应该可以解决问题

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

您总是可以只使用 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]);
    }
}

我去为你做了这个,它适用于 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”,它仍然有效,因为我们忽略了大小写。

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

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;

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