admin管理员组文章数量:1435848
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
1 Answer
Reset to default 4First, 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);
});
};
});
});
本文标签: javascriptDiscord bot Command Handler alias for command nameStack Overflow
版权声明:本文标题:javascript - Discord bot: Command Handler alias for command name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745646833a2668184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论