最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Discord bot: Command Handler alias for command name - Stack Overflow

matteradmin8PV0评论

I am working on a Discord bot, and trying to improve my already functioning mand handler. I have a folder, and every file is an extra mand. I want to expand the system, so I have alias name for the same mand, e.g. I want my clearchat mand to function with /clearchat or with /cc, but I dont want to create just another file and copy the code. This is what I have:

// I left out the other imports etc.
clientmands = new Discord.Collection();

// Reading mands-folder
const mandFiles = fs.readdirSync("./mands/").filter(file => file.endsWith(".js"));
for (const file of mandFiles) {
    const mand = require(`./mands/${file}`);
    clientmands.set(mand.name, mand); 
}

client.on("message", msg => {

    if (msg.content.startsWith(config.prefix) && !msg.author.bot && msg.guild) {
        const args = msg.content.slice(config.prefix.length).split(" ");
        const mand = args.shift().toLowerCase();
        
        if (clientmands.find(f => f.name === mand)) {
            clientmands.get(mand).execute(client, msg, args);    
        }
    }
});

and then a mand file inside the mands-folder:

module.exports = {
    name: "clearchat",
    execute(client, msg, args) {
        if (msg.member.hasPermission("ADMINISTRATOR")) {
              msg.channel.messages.fetch({limit: 99}).then(messages => {
                  msg.channel.bulkDelete(messages);
              });
        }
    }
}

(I know it only deletes 100 messages max, I am fine with that)

I image something in changing a few lines in my client.on("message) function, and just having to write in the clearchat.js file a line like name: ["clearchat", "cc", ...] where I can go writing as much aliases as I want.

Thanks in advance!

I am working on a Discord bot, and trying to improve my already functioning mand handler. I have a folder, and every file is an extra mand. I want to expand the system, so I have alias name for the same mand, e.g. I want my clearchat mand to function with /clearchat or with /cc, but I dont want to create just another file and copy the code. This is what I have:

// I left out the other imports etc.
client.mands = new Discord.Collection();

// Reading mands-folder
const mandFiles = fs.readdirSync("./mands/").filter(file => file.endsWith(".js"));
for (const file of mandFiles) {
    const mand = require(`./mands/${file}`);
    client.mands.set(mand.name, mand); 
}

client.on("message", msg => {

    if (msg.content.startsWith(config.prefix) && !msg.author.bot && msg.guild) {
        const args = msg.content.slice(config.prefix.length).split(" ");
        const mand = args.shift().toLowerCase();
        
        if (client.mands.find(f => f.name === mand)) {
            client.mands.get(mand).execute(client, msg, args);    
        }
    }
});

and then a mand file inside the mands-folder:

module.exports = {
    name: "clearchat",
    execute(client, msg, args) {
        if (msg.member.hasPermission("ADMINISTRATOR")) {
              msg.channel.messages.fetch({limit: 99}).then(messages => {
                  msg.channel.bulkDelete(messages);
              });
        }
    }
}

(I know it only deletes 100 messages max, I am fine with that)

I image something in changing a few lines in my client.on("message) function, and just having to write in the clearchat.js file a line like name: ["clearchat", "cc", ...] where I can go writing as much aliases as I want.

Thanks in advance!

Share Improve this question edited Jul 28, 2020 at 17:44 pragmatrick asked Jul 28, 2020 at 17:34 pragmatrickpragmatrick 6452 gold badges11 silver badges25 bronze badges 1
  • I think we have to assume, that every alias/name is used only a single time – pragmatrick Commented Jul 28, 2020 at 17:37
Add a ment  | 

1 Answer 1

Reset to default 4

First, you'll have to create an array with the aliases in your mand.

module.exports = {
    name: "clearchat",
    aliases: ["cc"],
    execute(client, msg, args) {
        
    }
}

Then, the same you did with mands, create a Collection for the aliases.

client.aliases = new Discord.Collection()

And finally, bind the alias to the mand:

if (mand.aliases) {
    mand.aliases.forEach(alias => {
        client.aliases.set(alias, mand)
    })
}

Now, when you want to execute a mand, you'll have to check if it has an alias.

const mandName = "testmand" // This should be the user's input.
const mand = client.mands.get(mandName) || client.aliases.get(mandName); // This will return the mand and you can proceed by running the execute method.

fs.readdir(`./mands/`, (error, files) => {
    if (error) {return console.log("Error while trying to get the mmands.");};
    files.forEach(file => {
        const mand = require(`./mands/${file}`);
        const mandName = file.split(".")[0];

        client.mands.set(mandName, mand);

        if (mand.aliases) {
            mand.aliases.forEach(alias => {
                client.aliases.set(alias, mand);
            });
        };
    });
});
Post a comment

comment list (0)

  1. No comments so far