Need Discord bot to respond with random messages upon command

Hello all,

I’m working on a Discord bot with basic coding that fits what I need, only thing it’s lacking is that the bot needs to respond with random messages that will be written into the code, rather than just a single response each time the command is sent.

So, something like “reply 1”, “reply 2”, “reply 3”, where the bot will randomly choose a response to send into the chat each time it hears the command.

Here is the template I’m using, it’s very basic and anyone that is even remotely more experienced with this stuff than I could quickly implement what I need here, I’m sure, then I can go into the code and start crafting the various responses:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// 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) {
            // /Hi
            case 'Hi':
                bot.sendMessage({
                    to: channelID,
                    message: 'Hello'
				});
            break;
            // Just add any case commands if you want to..
         }
     }
});

Thank you in advance to anyone that chimes in with a simple solution here, I know it’s probably a really minor snippet of coding that needs to be added in.

1 Like

Hey, welcome to the GitHub Support Community! I see that you’re using the discord.io library for Node.JS. A simple way to send random replies to a Discord channel would be to put those replies into an array and then randomize an index number for that array. Here’s an example:

// Array list of replies
let replies = ["reply 1", "reply 2", "reply 3"];
// Random number from 0 to 2 for the array index
let random = Math.floor(Math.random() * 3);

// Send a random reply to a channel
bot.sendMessage({
    to: channelID,
    message: replies[random]
});

I hope this helps fix your issue! If you have any questions or if something isn’t working correctly, please reply back!

1 Like

Thanks for the quick response. Is there a specific area of the template that I paste this new code into so that it works properly?

1 Like

No problem! You can place the let replies = ["reply 1", "reply 2", "reply 3"]; at the top of your file (e.g. where you require discord.io) and you can put the entire bot.sendMessage function whenever and wherever you need to send one of those random replies! Make sure you mark my above reply as a solution once it all works out!

1 Like

Can you paste me back the entire template with the new snippets of code in where it will work properly? If I can get it to upload successfully then I’ll be happy to mark your solution as approved. Thanks a bunch!

1 Like

Of course, I would be perfectly fine pasting back the entire template for your issue. However, could you reply back with all the code from that code file and put it within a code-block? It will make it way easier for me to format all of your code and send it back to you (just make sure you leave out any private access tokens).

My knowledge with coding is very limited, which is why I’m using a template for this, the bot is very simplistic, it’s only function is to reply to command with a few random phrases. Here is the entire “bot” code, pasted directly from the file:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// 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) {
            // /Hi
            case 'hi':
                bot.sendMessage({
                    to: channelID,
                    message: 'Hi'
                });
            break;
            // Just add any case commands if you want to..
         }
     }
});
1 Like
var Discord = require(‘discord.io’);
var logger = require(‘winston’);
var auth = require(’./auth.json’);

// Configure the array used for random replies
let replies = ["reply 1", "reply 2", "reply 3"];

// Configure the randomizer that will pick a random integer from 0 to the length of the array; used for array index
let random = Math.floor(Math.random() * replies.length);

// 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) {
            // /Hi
            case 'hi':
                bot.sendMessage({
                    to: channelID,
                    message: replies[random]
                });
        break;
        // Just add any case commands if you want to..
         }
     }
});

^ There’s the entire code file. Your bot should pick a random reply from the replies array whenever someone types “/hi” in a channel. You can replace those replies in the replies array with your own as well! I made sure to correct a syntax issue I made earlier within this template as well. If you have anymore questions or concerns, let me know (also mark the reply as a solution if all goes well).

1 Like

Worked perfectly, thanks so much! Marking your solution as approved right now! Great work!

1 Like

Awesome! Glad it worked out well and I appreciate the fast replies!

1 Like

Where is the option to mark your reply as a “solution”, I don’t see it.

1 Like

Should be next to the Heart icon near the Reply button.

1 Like

Hmm, I don’t see it - I see the heart icon, link and edit, in that order. Maybe it’s because my account here is brand new? Either way, I think people reading will see that you solved this for me. I really appreciate the help. If the “mark as solved” button appears in the future I’ll definitely check that for you.

1 Like

Weird, don’t know why it isn’t showing up. Either way, glad I could help resolve this issue for you!

Hey, once last issue, which I’m sure is minor - the bot isn’t selecting at random, it’s choosing #3 each time. So, if I type in three different phrases into the code, it’s selecting #3 each time from the batch, it’s not randomizing. Any ideas?

1 Like

In @gisgar3’s defense:

But seriously, it looks like the problem is that the random number is generated once and not on each call. Moving the generation of the random number to the on('message') function should fix it?

var Discord = require(‘discord.io’);
var logger = require(‘winston’);
var auth = require(’./auth.json’);

// Configure the array used for random replies
let replies = ["reply 1", "reply 2", "reply 3"];

// 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);
        let random = Math.floor(Math.random() * replies.length);

        switch(cmd) {
            // /Hi
            case 'hi':
                bot.sendMessage({
                    to: channelID,
                    message: replies[random]
                });
        break;
        // Just add any case commands if you want to..
         }
     }
});
2 Likes

Yes, that quick edit worked, thanks so much for the contribution to this issue!

2 Likes

Ahhh, that’s my bad. That’s what happens when I haven’t had coffee all day haha

1 Like

Hey guys, I just wanted to let you know the “solution” button finally populated and I credited both of you with “solutions” for your contributions to this issue. Thanks again!

1 Like

Hey everyone, sorry to tack on to an old thread, but I am trying to do the exact same thing as the OP with only one major difference. I’ve been looking for a solution for a long time now, and this is 95% of the way there… My question is, instead of using

let replies = ["reply 1", "reply 2", "reply 3"];

Is there a way the bot could read from a much longer list? Preferably a text file.

I am trying to create a bot that can randomly output lines from a list that is easy for my mods to update. Any help would be greatly appreciated. Thank you

1 Like