javascript - discord.js message.content for a word to send a message - Stack Overflow

var Discord = require('discord.io');var logger = require('winston');var auth = re

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 mand
    // 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 'mands':
                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'
         });
     }
});

This is my code

When "norb" is said as the only word, or "this place" is said, it'll work. But if it's between words like "hello norb hello" it won't work. Everything else is working fine for now.

I'd like it to look for "norb" and say "praise be to the overlord" when it's said. Rather than just when it's the first word. I'm not really sure how to do that.

Thanks!

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 mand
    // 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 'mands':
                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'
         });
     }
});

This is my code

When "norb" is said as the only word, or "this place" is said, it'll work. But if it's between words like "hello norb hello" it won't work. Everything else is working fine for now.

I'd like it to look for "norb" and say "praise be to the overlord" when it's said. Rather than just when it's the first word. I'm not really sure how to do that.

Thanks!

Share Improve this question edited Jul 25, 2018 at 18:30 airhoodz asked Jul 25, 2018 at 18:20 airhoodzairhoodz 11 gold badge1 silver badge4 bronze badges 1
  • 1 Please provide some sample data like what thisWord is when this is working and what it is when it isn't as well as the values of message. – zero298 Commented Jul 25, 2018 at 18:23
Add a ment  | 

1 Answer 1

Reset to default 3

Assuming you're trying to make the bot respond to a specific mand. What you are looking for is the .includes() function

This function checks if a string includes the word you are looking for. For example:

const thisWord = "Something";
if(message.content.includes(thisWord))
{
    bot.sendMessage({
        to: channelID,
        message: "Your reply."
    })
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742254015a4409685.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信