javascript - 动态帮助命令 discord.js
问题描述
我目前正在制作一个不和谐的机器人,但我有一个问题。我对 discord.js 和 node.js 还是很陌生。这是我的代码:
module.exports.run = async (bot, message, args) => {
function getMenu() {
var hpmenu = {};
return new Promise(function(resolve, reject) {
dashboard.readServerBoundValue(
`${message.guild.id}`,
'PREFIX',
async function(output) {
if (output) {
var prefixxx = output;
} else {
var prefixxx = config.botPrefix;
}
//=====================================================================================================
fs.readdir('./src/commands/', (err2, files2) => {
files2.forEach((f2, i2) => {
hpmenu[f2] = new discord.MessageEmbed();
hpmenu[f2].setTitle(`${f2}`);
console.log('Added catagory ' + f2);
//=========================================================================================
fs.readdir(`./src/commands/${f2}`, (err, files) => {
files.forEach((f, i) => {
const cmd = f.replace('.js', '');
hpmenu[f2].addField(cmd, 'test');
console.log('Added command ' + cmd);
//=====================================================================================================
});
resolve(hpmenu);
});
});
});
}
);
});
}
async function main() {
var output = await getMenu();
message.channel.send(output['developer']);
}
main();
};
此代码没有任何错误,但这是我遇到的问题:
问题是它在完成功能之前运行主要getMenu
功能。如何解决这个问题?我已经阅读了多篇关于此的帖子。感谢大家阅读!
解决方案
After alot of coding i reached my goal; Making a dynamic help menu with pagination. This is the code:
It functions good. The only thing you need is to make a file named config.json in the root directory of the bot where index.js is located and add a key named "embedColor" put your commands in
./src/commands/CATAGORY/COMMAND.js
and this is the result:
sorry for bad english. Do note if you use it you still have to implement the prefix and description yourself this depends per command handler and database engine. You can change the description code and the command name at line 19 in the pastebin. if you want a image you can add a new line after line 13:
hmu[i].setImage("IMAGE");
Its just a discord embed and it will work fine, add thumbnails, authors, and change however you like. You do not have to credit me if you're using it
Sorry for my bad english and a thank you to Lioness100 for editing the text. English is not my first language :) Have a nice day everyone bye!
推荐阅读
- python - 在 tkinter 中进行多项选择
- python-3.x - Python在字符串元素列表中搜索模式
- html - 如何在css中分别为同一元素添加动画?
- python - 如何在不删除括号的情况下删除括号内的内容
- c++ - 错误:“selectROI”不是“cv”的成员
- android - PayPal Android Native Checkout SDK - 无效的 client_id 或 redirect_uri 和 CHECKOUT_ERROR
- java - java11中不存在Javadoc问题包
- go - 使用 goftp 列出 FTP 文件
- java - 不断收到此错误“无法初始化检测器”。我没有正确加载 tflite 模型吗?
- javascript - 使用 tsc 时从 p5.play.js 生成 d.ts 文件的问题