我的目标是将我的机器人分为模块,但不幸的是,我失败了。 我已经尝试了很长时间,在实现目标方面确实需要一些帮助。
This is my bot.js
:
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
fs.readdir('./commands/', (err, files) => {
if (err) console.error(err);
console.log(`${files.length} commands will load.`);
files.forEach(f => {
let props = require(`./commands/${f}`);
console.log(`Loaded commands: ${props.help.name}.`);
client.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name);
});
});
});
client.reload = command => {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`./commands/${command}`)];
let cmd = require(`./commands/${command}`);
client.commands.delete(command);
client.aliases.forEach((cmd, alias) => {
if (cmd === command) client.aliases.delete(alias);
});
client.commands.set(command, cmd);
cmd.conf.aliases.forEach(alias => {
client.aliases.set(alias, cmd.help.name);
});
resolve();
} catch (e) {
reject(e);
}
});
};
client.load = command => {
return new Promise((resolve, reject) => {
try {
let cmd = require(`./commands/${command}`);
client.commands.set(command, cmd);
cmd.conf.aliases.forEach(alias => {
client.aliases.set(alias, cmd.help.name);
});
resolve();
} catch (e) {
reject(e);
}
});
};
client.unload = command => {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`./commands/${command}`)];
let cmd = require(`./commands/${command}`);
client.commands.delete(command);
client.aliases.forEach((cmd, alias) => {
if (cmd === command) client.aliases.delete(alias);
});
resolve();
} catch (e) {
reject(e);
}
});
};
This is my commands/command1.js
:
const Discord = require('discord.js');
exports.run = (client, message, args) => {
msg.channel.send('hello')
}
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: ['try'],
permLevel: 0,
};
exports.help = {
name: 'try',
description: 'try',
usage: 'try'
};
当我使用命令时,什么也没有发生,并显示“将加载1条命令。”。
控制台中的“已加载命令:尝试”。
每次使用命令都会得到相同的输出。
答案 0 :(得分:0)
我以前遇到过此问题,我不知道是什么原因引起的或如何解决的,但我有一个“解决方案”。 尝试更改命令名称,出于某些原因,当我遇到此问题时可以使用。