Creating a module for multiple ‘chat’ features for a discord bot


I’m revisiting one of my first discord bots that I want to test an upgrade on. This bot essentially gives users a lot of information on characters (it’s a fandom bot) so they have a lot of lines that respond to specific quotes.

My prior experience is C# and with Unity. In that scenario, if I wanted to constantly listen out for any prompts in a separate script, I would do the following:

public Script chat;

public void Update()
{
chat();
}

So anytime someone did a command that was in the script, it would know to always listen and respond to those commands. The problem is, this bot I coded in JS(Java Script) and I’m not familiar enough with it to know if there’s a similar function for discord bot API.

I’ve followed tutorials about modules but I can’t seem to get them working properly, so I think I’m doing something wrong. Below is a copy of the index.js and the chat.js scripts but I think I’m overlooking something basic here.

index.js:

const Discord = require('discord.js');
const chat = require('./chat.js');
const Greeting = require('Greetings');
const bot = new Discord.Client();

const token = 'hidden'; 

const PREFIX = '!';

bot.on('ready', () =>
{
    console.log('Meepo has been freed!');
    bot.user.setActivity("Kameo: Elements of Power");
})

bot.on('message', async message =>
{
    let args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0])
    {
        case 'ping':
            message.reply('pong');
            break;
    }

    var role = message.guild.roles.find(role => role.name === "user");

    

});

bot.login(token);

chat.js:

function chat()
{
    //Character dialogue. 
    //This is to get more information about characters, or specific reactions to character names.
    if (message.content == "Kreepo") {
        message.reply("That legless guy? He's such a loser, he's got no legs and a BAD attitude!");
    }

    if (message.content == "elemental sprite") {
        message.reply("WHERE?!");
    }

    //Info about Kameo
    if (message.content == "Hey Meepo, can you tell me about Kameo?") {
        message.reply("Sure can! What do you wanna know?n Kameo Character Info.n Kameo's Skills.n Kameo's Origins.")
    }

    if (message.content == "Kameo's Skills.") {
        message.reply("Kameo is basically the jock of the family with twice the charm, she can dropkick a baby and people would still love her!n n She can fly pretty fast but can't really get off the ground. That said, large gaps are a pretty big problem for her too.n n When fighting on her own, Kameo can do this flip kick, like WHAPOW!n n and....that's pretty much it actually.")
    }

    //if the user makes a typo:
    if (message.content == "Kameo's kills.") {
        message.reply("Her WHAT?")
    }

    if (message.content == "Kameo's Origins.") {
        message.reply("Okay, lemme see here. Okay, here it says Kameo came from the Twycross Village, her Gramps, Terramus, is the Cheiftan and he...oh, n n -yeah okay he died after Thorn smacked him.n Solon got real mad and used The Ultimate Curse, then HE died and the Warriors and Wotnot were kaput.n n 5 Sprites got captured, 5 were pro at hide n seek and took baby Kameo back to the Royal Fameo, where Theena adopted her and started playing favourites.n  nWait, so that means Kameo's parents are still alive, right? n nHmmm...")
    }

    if (message.content == "Kameo Character Info.") {
        message.reply("Kameo is the youngest Elvin Princess of the Royal Sisters. She is very headstrong, stubborn and ain't afraid to get into a brawl.n n After a certain incident, Kameo changes Kalus' fate and inherits the Element of Power and the Wotnot.n n Obviously Kalus is mad about this, so she up and leaves with a plan in mind, ya see.n n A plan that would start a really nasty war.n n For more, ya gonna have to check out <#424906520883625984>.n   Or ya could just go talk to her, I think she's in <#695571436789825536>.")
        message.member.addRole('695568329015951413');
    }

    //Info about Kalus

    if (message.content == "Hey Meepo, what do you think about Kalus?") {
        message.reply("Kalus? She's way too serious all the time, and scary when she's angry too! But one time I saw her walking down the hallway by herself...n n and she looked so sad. I wonder why? I'm not brave enough to ask, but maybe you can?n n ALSO IF SHE ASKS I DIDN'T SEND YOU.");
        message.member.addRole('695568036123508821');
    }

    if (message.content == "What else can you tell me about Kalus?") {
        message.reply("I dunno, I'm not Kreepo.");
    }

    if (message.content == "Where's Ortho?") {
        message.reply("Takin' a nap. Old people sleep a lot.");
    }

    //Conversation
    //This is for general conversation/reactions to dialogue, and mostly for fun.
    if (message.content == "Hahaha" || message.content == "Ha ha ha") {
        message.reply("Hee hee hee....what're we laughing at?");

    }

    if (message.content == "thanks meepo") {
        message.reply("You're welcome!");
    }

    if (message.content == "Do I amuse you?") {
        message.reply("Yeah, you're supposed to be a joke, right?");

    }

    if (message.content == "How was it being trapped in stone?") {
        message.reply("Feeling really attacked right now.");
    }

    if (message.content == "Did you know that Meepo was a Diva?") {
        message.reply("I am not! They just said that cause I wanted a PlayStation 3 in my dressing room! ....and maybe I may have wanted the game renamed to Meepo: Elements of Power...*cough*");
    }

    if (message.content == "I feel sad.") {
        message.reply("Hey it's okay, I know what'll cheer you up! What's green and stinky? A TROLL!");
    }

    if (message.content == "lol") {
        message.reply("You look good with a smile!");
    }

    if (message.content == "XD") {
        message.reply("You look good with a smile!");
    }

    if (message.content == "How are you?") {
        message.reply("I'm fantastic, thanks for asking!");
    }

    if (message.content == "Not you Meepo.") {
        message.reply("....yes you Meepo.");
    }

    if (message.content == "Meepo no.") {
        message.reply("MEEPO YES!");
    }

    if (message.content == "fortnite") {
        message.reply("....I will ban you. Right here. Right now.");
    }

    if (message.content == "Meepo can't ban.") {
        message.reply("....In my dreams I can.");
    }

    if (message.content == "Kameo 2.") {
        message.reply("Here is my resume, I can be thrown to catch elemental sprites and really help out!!");
    }

    if (message.content == "I'm going to bed.") {
        message.reply("Want me to tuck you in?");
    }

    if (message.content == "Thanks Meepo") {
        message.reply("You're welcome!");
    }

    if (message.content == "I gotta go to bed.") {
        message.reply("Want me to tuck you in?");
    }

    if (message.content == "No Meepo.") {
        message.reply("Aww.");
    }

    if (message.content == "Hey Meepo, can you tell me about the lore of the game?.") {
        message.reply("Sorry, I can't do that, but there's something in the works for that!");
    }

    //Responds to insults:

    if (message.content == "you suck meepo") {
        message.reply("D:<");
    }

    if (message.content == "meepo you suck") {
        message.reply("D:<");
    }

    if (message.content == "shut up meepo") {
        message.reply("NEVER");
    }

    if (message.content == "you're a loser") {
        message.reply("Takes one to know one!");
    }

}
function Greetings()
{
    //Greetings and Goodbyes
    //This is for saying hello or goodbye to the user.
    if (message.content == "Hi!")
    {
        message.reply("Hey, my name's Meepo!");
    }

    if (message.content == "Hello!")
    {
        message.reply("Hey, my name's Meepo, great to meet ya!");
    }

    if (message.content == "Goodnight.")
    {
        message.reply("Goodnight, sleep tight! Don't let the trolls bite!");
    }

    if (message.content == "G'night.")
    {
        message.reply("Goodnight, sleep tight! Don't let the trolls bite!");
    }
}

module.exports = chat.js;
module.exports = Greetings;

As you can see, there’s a lot of information and responses for this fandom bot and prior to this, every single command was kept inside of the index script. This makes it incredibly tedious to update/add features and upgrade to the bot. I’m sure it’s something very simple I’ve overlooked here, I’m just not familiar enough to spot where I’ve gone wrong with the code.

Source: JavaSript – Stack Overflow

November 28, 2021
Category : News
Tags: bots | discord.js | javascript | module

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.