discord.js message.content 一个词发送一条消息
discord.js message.content for a word to send a message
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" 或说 "this place" 时,它会起作用。但是如果它在像 "hello norb hello" 这样的单词之间,它将不起作用。目前其他一切正常。
我希望它查找 "norb" 并在它说时说 "praise be to the overlord"。而不仅仅是当它是第一个词时。我不太确定该怎么做。
谢谢!
假设您正在尝试让机器人响应特定命令。你要找的是.includes()
函数
此函数检查字符串是否包含您要查找的单词。例如:
const thisWord = "Something";
if(message.content.includes(thisWord))
{
bot.sendMessage({
to: channelID,
message: "Your reply."
})
}
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" 或说 "this place" 时,它会起作用。但是如果它在像 "hello norb hello" 这样的单词之间,它将不起作用。目前其他一切正常。
我希望它查找 "norb" 并在它说时说 "praise be to the overlord"。而不仅仅是当它是第一个词时。我不太确定该怎么做。
谢谢!
假设您正在尝试让机器人响应特定命令。你要找的是.includes()
函数
此函数检查字符串是否包含您要查找的单词。例如:
const thisWord = "Something";
if(message.content.includes(thisWord))
{
bot.sendMessage({
to: channelID,
message: "Your reply."
})
}