0
votes

This is what my discord bot.js looks like.

Basically, I thought that when I type in "ten bot je fakt sračka" it gives one of the responses in the array.

If I type in "ahoj", it gives that response with a few hearts.

And also when I type in "test", it should reply with "test".

Now, when I run the bot and type in any of the three messages, if only replies "test" to "test". Nothing else gets a response. What do I do?

const Discord = require("discord.js");
const client = new Discord.Client();
client.login("TOKEN");

client.on("ready", readyDiscord);

function readyDiscord() {
    console.log("123");
}

const replies = ["Stop it!", "Bot lives matter! <:ragey:776017536973013003>", "no", "<:jakjestvbiblipsno:776002959665266709>"];

client.on("message", gotMessage);

function gotMessage(msg) {
    console.log(msg.content);
    if (msg.content === "ten bot je fakt sračka") {
        const index = Math.floor(Math.random() * replies.length);
        msg.channel.send(replies[index]);
    }
}

function gotMessage(msg) {
    console.log(msg.content);
    if (msg.content === "ahoj") {
        msg.reply("Ahoj <3<3<3");
    }
}

function gotMessage(msg) {
    console.log(msg.content);
    if (msg.content === "test") {
        msg.reply("test");
    }
}
1

1 Answers

0
votes

This is because you keep overriding your function 'gotMessage' and the last declaration of it is where you reply with test.

client.on('message', gotMessage);

function gotMessage(msg) {
    console.log(msg.content);
    if (msg.content === 'ten bot je fakt sračka') {
      const index = Math.floor(Math.random() * replies.length);
      msg.channel.send(replies[index]); 
    } else if (msg.content === 'ahoj') {
      msg.reply('Ahoj <3<3<3');
    } else if (msg.content === 'test') {
      msg.reply('test');
    }
}

Here you have all the if statements in one declaration of gotMessage. Since it isn't overridden, each if will be accounted for. It is also better to use else if in this situation since you're checking if a value is equal to one thing or another.

P.S. Never share your bot's login token online, otherwise other people can mess with it