var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
var rolledNumber = 0;
var norb = 'norb';
var thisPlace = 'this place';
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0];
args = args.splice(1);
switch(cmd) {
// !ping
case 'ping':
bot.sendMessage({
to: channelID,
message: 'Pong!'
});
break;
case 'commands':
bot.sendMessage({
to: channelID,
message: 'nope!'
});
break;
case 'sleepydave':
bot.sendMessage({
to: channelID,
message: 'this is dave'
});
break;
case 'roll':
rolledNumber = Math.floor(Math.random() * 12) + 1;
bot.sendMessage({
to: channelID,
message: 'You rolled two dice and get: ' + rolledNumber
});
break;
}
}
if (message.content[0,2000] == norb) {
bot.sendMessage({
to: channelID,
message: 'Praise be to the Overlord'
});
}
if (message.substring(0,2000) == thisPlace) {
bot.sendMessage({
to: channelID,
message: 'stop talking about here'
});
}
});
这是我的代码
如果说“ norb”是唯一的单词,或者说“这个地方”,它将起作用。但是,如果它位于“ hello norb hello”之类的词之间,则将无法正常工作。目前其他所有功能都正常。
我希望它能说“ norb”并说“赞美霸主”。而不只是当它是第一个单词时。我不太确定该怎么做。
谢谢!
答案 0 :(得分:1)
假设您正在尝试使漫游器响应特定命令。您正在寻找的是.includes()
函数
此函数检查字符串是否包含要查找的单词。例如:
const thisWord = "Something";
if(message.content.includes(thisWord))
{
bot.sendMessage({
to: channelID,
message: "Your reply."
})
}