我为我的服务器创建了自己的不和谐机器人,如果我说阵列中的特定单词 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
答案 0 :(得分: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]);
}
因此,不检查世界问候消息,而是检查消息是否包含在数组中。
答案 1 :(得分: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]);
}
}
答案 2 :(得分:0)
我去为你做了这个,它与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要输入&#39; @Bot HeLlO&#39;它仍然有效,因为我们忽略了套管。
答案 3 :(得分: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;
仍然有很大的改进和错误处理的余地,但我会把它留给你。古德勒克!