繁体   English   中英

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

[英]discord.js node.js - bot reply

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

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”,他没有回答我。

有人可以帮帮我吗?

对不起我的英文: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的消息,而是检查消息是否包含在Array中。

你总是可以使用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;

仍有很大的改进和错误处理的余地,但我会把它留给你。 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM