如何在Discord JavaScript中正确使用别名

问题描述 投票:1回答:1

需要别名的帮助。我在Google周围搜索,但仍然找不到有用的东西

module.exports.config = {
    name: "av",
    aliases: ["icon", "pfp"]
};

索引主文件:

bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
fs.readdir("./commands/general", (err, files) => {

    if (err) console.log(err);

    let jsfile = files.filter(f => f.split(".").pop() === "js");
    if (jsfile.length <= 0) {
        console.log("Couldn't find the general commands.");
        return;
    }
    jsfile.forEach((f, i) => {
        let props = require(`./commands/general/${f}`);
        console.log(`${f} loaded!`);
        bot.commands.set(props.config.name, props);
        bot.aliases.set(props.config.name);
    });
});

正在留言

const command = bot.commands.get(commandName) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

注:所有命令都工作得很好,但是尝试使用不同的方法多次修复别名,但我真的放弃了。希望大家帮助我,谢谢。

javascript alias discord.js aliases jscript-10
1个回答
0
投票

[您有两个集合,您也没有为bot.aliases设置任何值,并且关键仍然是props.config.name,您可以坚持使用两个集合,但是没有实际用途,只会使代码变得更糟]]

因此摆脱别名集合和bot.aliases.set(props.config.name)

接下来,实际的功能属性是什么?如果当前在config对象上,它就没用了。

所以假设您的布局是现在

module.exports.run = () => { /* function, doesnt have to be run */ }
module.exports.config = {
};

主文件中的代码

jsfile.forEach(f => {
    let props = require(`./commands/general/${f}`);
    console.log(`${f} loaded!`);
    bot.commands.set(props.config.name, { 
        run: props.run,
        ...props.config
    });
});

支票上的代码

const command = bot.commands.get(commandName) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

//to actually run it you would need to do
command.run();
© www.soinside.com 2019 - 2024. All rights reserved.