Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new to discord.js and I'm trying to make a really simple command with command handling.

The content of the bot.js file:

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

const fs = require("fs");
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('On!');})


client.on('message', async message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (!client.commands.has(command)) return;

    try {
    client.commands.get(command).execute(message, args);
    } catch (error) {
    console.error(error);
    message.reply("Error");
    }

The content of the lunedi1.js file:

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

module.exports = {
    name: "lunedi1",
    description: "Codici videolezioni, li puoi ottenere facendo '.<giorno> <ora>",
    execute(message, args) {
        if(message.content.startsWith(prefix + "lunedi1")){
           return message.channel.send("Diritto, 1630027401")
        }else if(message.content.startsWith(prefix + "lunedi2")){
            return message.channel.send("Italiano, 959924441")
        }else if(message.content.startsWith(prefix + "lunedi3")){
            return message.channel.send("Italiano, 959924441")
        }else if(message.content.startsWith(prefix + "lunedi4")){
            return message.channel.send("Fisica LAB, 954071647" )
        }else if(message.content.startsWith(prefix + "lunedi5")){
            return message.channel.send("Informatica, 1213333512")
        }else if(message.content.startsWith(prefix + "martedi1")){
            return message.channel.send("Informatica, 1213333512")
        }else if(message.content.startsWith(prefix + "martedi2")){
            return message.channel.send("Georgrafia, 951609580")
        }else if(message.content.startsWith(prefix + "martedi3")){
            return message.channel.send("Chimica, 952232255")
        }else if(message.content.startsWith(prefix + "martedi4")){
            return  message.channel.send("Chimica, 952232255")
        }else if(message.content.startsWith(prefix + "martedi5")){
            return message.channel.send("Inglese, 953894058")
        }else if(message.content.startsWith(prefix + "martedi6")){
            return message.channel.send("Storia, 959924441")
        }else if(message.content.startsWith(prefix + "mercoledi1")){
            return message.channel.send("Grafica, 954902455")
        }else if(message.content.startsWith(prefix + "mercoledi2")){
            return message.channel.send("Italiano, 959924441")
        }else if(message.content.startsWith(prefix + "mercoledi3")){
            return message.channel.send("Italiano, 959924441")
        }else if(message.content.startsWith(prefix + "mercoledi4")){
            return message.channel.send("Inglese, 953894058")
        }else if(message.content.startsWith(prefix + "mercoledi5")){
            return message.channel.send("Inglese, 953894058")
        }else if(message.content.startsWith(prefix + "mercoledi6")){
            return message.channel.send("Religione, 955444970")
        }else if(message.content.startsWith(prefix + "giovedi1")){
            return  message.channel.send("Matematica, 957066907")
        }else if(message.content.startsWith(prefix + "giovedi2")){
            return message.channel.send("Matematica, 957066907")
        }else if(message.content.startsWith(prefix + "giovedi3")){
            return  message.channel.send("Grafica, 954902455")
        }else if(message.content.startsWith(prefix + "giovedi4")){
            return message.channel.send("Grafica, 954902455")
        }else if(message.content.startsWith(prefix + "giovedi5")){
            return message.channel.send("Scienze della Terra, 1632151557")
        }else if(message.content.startsWith(prefix + "venerdi1")){
            return  message.channel.send("Fisica, 1752593012")
        }else if(message.content.startsWith(prefix + "venerdi2")){
            return message.channel.send("Fisica, 1752593012")
        }else if(message.content.startsWith(prefix + "venerdi3")){
            return message.channel.send("Chimica, 952232255")
        }else if(message.content.startsWith(prefix + "venerdi4")){
            return message.channel.send("Storia, 959924441")
        }else if(message.content.startsWith(prefix + "venerdi5")){
            return message.channel.send("Educazione fisica, 140876521")
        }else if(message.content.startsWith(prefix + "venerdi6")){
            return message.channel.send("Educazione fisica, 140876521")
        }else if(message.content.startsWith(prefix + "sabato1")){
            return message.channel.send("Informatica, 1213333512")
        }else if(message.content.startsWith(prefix + "sabato2")){
            return message.channel.send("Diritto, 1630027401")
        }else if(message.content.startsWith(prefix + "sabato3")){
            return message.channel.send("Scienze della Terra, 1632151557")
        }else if(message.content.startsWith(prefix + "sabato4")){
            return message.channel.send("Matematica, 957066907")
        }else if(message.content.startsWith(prefix + "sabato5")){
            return message.channel.send("Matematica, 957066907")
        }
    },
};

As you can see it is really simple but when I want that when I do like ".martedi2" I don't get "Georgrafia, 951609580" but it only works with ".lunedi1", others don't work and like do other 30 commands will be rlly confusing. If you need more info I can give that. Thx to everyone that will help me :)

question from:https://stackoverflow.com/questions/65952367/discord-js-command-handling-else-if-problem

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
697 views
Welcome To Ask or Share your Answers For Others

1 Answer

command name is lunedi1 thats why. other will not work because message content starting does not starts with other, you can use arguments to solve this problem

code:

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

module.exports = {
    name: "info",
    description: "Codici videolezioni, li puoi ottenere facendo '.<giorno> <ora>",
    execute(message, args) {
      if (!args[0]) return message.channel.send("please give name");
      if (args[0] === "lunedi1") {
         return message.channel.send("Diritto, 1630027401")
      } else if (args[0] === "lunedi2") {
         return message.channel.send("Italiano, 959924441")
      } else {
         return message.channel.send("?");
      };
    }
};

usage:
[Prefix]info
example:
!info lunedi1

you can do this for other.

Links:
Guide For Arguments


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...