GreatShark
GreatShark

Reputation: 25

Discord Application Bot - Discord.js

I am creating a discord.js bot, but I want to add more commands for applications. For example, I want a command !apply1 and command !apply2. When I copy the code, paste it again, change the name of the command, and type the command, it sends two or three questions instead of one. What should I change?

If you didn't understand and want to check what I am talking about join this fake discord server: https://discord.gg/fVsQaa6 (It sends 2 questions per time, I want to send 1 by 1. Also, it sends the answers to the same channel for both of them, I checked if the channel id is correct and it is.)

The Code:

// Requires \\
const botSettings = require("./botsettings.json");
const Discord = require("discord.js");
const prefix = botSettings.prefix;

const bot = new Discord.Client({disableEveryone: true});

// Bot On Ready Console \\
bot.on('ready', () => {
    console.log("Bot Is Ready"); 
    bot.user.setActivity("Great", {type: "WATCHING"})
})




// Staff \\
let userApplications = {}

bot.on("message", function(message) {
    if (message.author.equals(bot.user)) return;
  
    let authorId = message.author.id;
  
    if (message.content === "!apply staff") {
        console.log(`Apply begin for authorId ${authorId}`);   
        if (!(authorId in userApplications)) {
            userApplications[authorId] = { "step" : 1}
            
            message.author.send("‏‏‎ ‎")
            message.author.send(">>> Staff Application")
            message.author.send(">>> Question 1:");
        }
  
    } else {
  
        if (message.channel.type === "dm" && authorId in userApplications) {
            let authorApplication = userApplications[authorId];
  
            if (authorApplication.step == 1 ) {
                authorApplication.answer1 = message.content;
                message.author.send(">>> Question 2:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 2) {
                authorApplication.answer2 = message.content;
                message.author.send(">>> Question 3:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 3) {
                authorApplication.answer3 = message.content;
                message.author.send(">>> Question 4:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 4) {
                authorApplication.answer4 = message.content;
                message.author.send(">>> Question 5:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 5) {
                authorApplication.answer5 = message.content;
                message.author.send(">>> Question 6:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 6) {
                authorApplication.answer6 = message.content;
                message.author.send(">>> Question 7:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 7) {
                authorApplication.answer7 = message.content;
                message.author.send(">>> Question 8:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 8) {
                authorApplication.answer8 = message.content;
                message.author.send(">>> Thank You!");
                bot.channels.cache.get("743550883589259287")
                  .send(`${message.author}\n >>> Question 1: | ${authorApplication.answer1}\n Question 2: | ${authorApplication.answer2}\n Question 3: | ${authorApplication.answer3}\n Question 4: | ${authorApplication.answer4}\n Question 5: | ${authorApplication.answer5}\n Question 6: | ${authorApplication.answer6}\n Question 7: | ${authorApplication.answer7}\n Question 8: | ${authorApplication.answer8}`);
                delete userApplications[authorId];
            }
        }
    }
});


// Police \\
bot.on("message", function(message) {
    if (message.author.equals(bot.user)) return;
  
    let authorId = message.author.id;
  
    if (message.content === "!apply police") {
        console.log(`Apply begin for authorId ${authorId}`);   
        if (!(authorId in userApplications)) {
            userApplications[authorId] = { "step" : 1}
            
            message.author.send("‏‏‎ ‎")
            message.author.send(">>> Police Application")
            message.author.send(">>> Question 1:");
        }
  
    } else {
  
        if (message.channel.type === "dm" && authorId in userApplications) {
            let authorApplication = userApplications[authorId];
  
            if (authorApplication.step == 1 ) {
                authorApplication.answer1 = message.content;
                message.author.send(">>> Question 2:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 2) {
                authorApplication.answer2 = message.content;
                message.author.send(">>> Question 3:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 3) {
                authorApplication.answer3 = message.content;
                message.author.send(">>> Question 4:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 4) {
                authorApplication.answer4 = message.content;
                message.author.send(">>> Question 5:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 5) {
                authorApplication.answer5 = message.content;
                message.author.send(">>> Question 6:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 6) {
                authorApplication.answer6 = message.content;
                message.author.send(">>> Question 7:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 7) {
                authorApplication.answer7 = message.content;
                message.author.send(">>> Question 8:");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 8) {
                authorApplication.answer8 = message.content;
                message.author.send(">>> Thank You!");
                bot.channels.cache.get("743558960677912636")
                  .send(`${message.author}\n >>> Question 1: | ${authorApplication.answer1}\n Question 2: | ${authorApplication.answer2}\n Question 3: | ${authorApplication.answer3}\n Question 4: | ${authorApplication.answer4}\n Question 5: | ${authorApplication.answer5}\n Question 6: | ${authorApplication.answer6}\n Question 7: | ${authorApplication.answer7}\n Question 8: | ${authorApplication.answer8}`);
                delete userApplications[authorId];
            }
        }
    }
});



bot.login(botSettings.token);

Upvotes: 1

Views: 2746

Answers (2)

user14144949
user14144949

Reputation:

I'm pretty sure that it's sending two messages at a time because you might have your code editor and command prompt running the code at the same time. It's a common thing. Your code looks normal, so I'm pretty sure this is what happened.

Upvotes: 0

Dorian349
Dorian349

Reputation: 1589

You have an else statement in both of your client.on("message") execution.

If you decide to dm the bot, it will check the first condition: if (message.content === "!apply staff") and the second one: if (message.content === "!apply police").

Since both are false, the bot will execute both else statement. That's why you get two questions each time.

If you want to fix this, it can be interesting to store the application value and then check it inside the else statement or merge the two client.on("message") into one and add a check inside the else statement.

Upvotes: 1

Related Questions