Random message discord bot

So i want to create a discord bot in discord.js that sends random message everytime i type in the command. I’ve looked online and i found a recent thread but the bot selects the random message everytime it gets restarted.
I would be very grateful if someone helped me. I’m really new to all of this so i dont have any experience

`const Discord = require(“discord.js”);
const client = new Discord.Client();

client.on(“ready”, () => {

console.log(“I am ready!”);

});

let replies = [“reply 1”, “reply 2”, “reply 3”];
let random = Math.floor(Math.random() * 3);

client.on(“message”, (message) => {
if (message.content.startsWith(“command”)) {
message.channel.send(replies[random])
}
});

client.login(“token”);`

Hey @LEoNNd and welcome! That looks like a very good start already!

What is your question? Is something not working? If not, do you see any error messages? Any odd behaviour? Let us know!

1 Like

hey, i was asking how to make a bot that sends random messages when you type a command

if(message.content.startsWith('!random')) {
        number = 10;
        var roll = Math.floor (Math.random() * (number - 1 + 1 )) + 1;
        if (roll == 1){
            message.reply('1')
        }
        if (roll == 2){
            message.reply('2')
        }
        if (roll == 3){
            message.reply('3')
        }
        if (roll == 4){
            message.reply('4')
        }
        if (roll == 5){
            message.reply(5')
        }
        if (roll == 6){
            message.reply('7')
        }
        if (roll == 7){
            message.reply('7')
        }   
        if (roll == 8){
            message.reply('8')
        }
        if (roll == 9){
            message.reply('9')
        }
        if (roll == 10){
            message.reply(10')
        }

i found this piece of code on a random immmage thread and i adapted it to work with text. Took me a long time to figure out.

My problem with the old code was that the answer was random but only when you restart the bot. So if i start the bot and type the command once it chooses the the answer it doesnt change till i restart the bot. For example i turn on the bot, type “message” and the bot will answer “reply2” everytime. After i restart the it may choose reply1, 2, or 3 and if it chooses 3 for example it will keep answering 3 until i restart the bot again.

And no i dont get any error message but still thx for trying to help me. Now i also put all the code in a command/event handler so i dont get any package loss or any lag

In order not to get the same number each time, it’s very important the line I quoted above is indeed in some form of event handler, so it gets executed every time.

Glad putting all code in the event handler worked out for you!