Search code examples
javascriptnode.jsdiscorddiscord.js

discord.js Running a command sends a duplicate text


Here is my code, whenever I run the $dm command it runs it but also runs the $help command. I know its probably a newbie question but if someone could help me out it will be appreciated

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
const { MessageEmbed } = require('discord.js');

client.once('ready', () => { client.user.setActivity('Use $help for a list of commands!', { type: "PLAYING" }); });
    console.log('Ready!');
    

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).trim().split(' ');
    const command = args.shift().toLowerCase();
    if (message.content.startsWith(`${prefix}ping`)) {
        message.channel.send('Pong.');
    } else if (message.content.startsWith(`${prefix}beep`)) {
        message.channel.send('Boop.');
    } else if (message.content === `${prefix}server`) {
        message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
    } else if (message.content === `${prefix}user-info`) {
        message.channel.send(`Your username: ${message.author.username}\nYour ID: ${message.author.id}`);
    console.log(message.content);
    } else if (message.content === (`${prefix}dm`)) {
        message.author.send("string");
    } else if (message.content === "$help") {}
        let embed = new MessageEmbed()
        .setTitle("Command List")
        .setDescription("$help, $ping, $beep, $server, $user-info, $dm")
        .setColor("RANDOM")
        message.channel.send(embed)

}

,
)
client.login(token)

Solution

  • else if (message.content === "$help") {} <- The issue

    You're closing the curly braces too early and so the execution of your code below isn't actually dependant on the if statement.

    It should look like this

    else if (message.content === "$help") {
            let embed = new MessageEmbed()
            .setTitle("Command List")
            .setDescription("$help, $ping, $beep, $server, $user-info, $dm")
            .setColor("RANDOM")
            message.channel.send(embed)
    }