首页 > 解决方案 > 动态帮助命令 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功能。如何解决这个问题?我已经阅读了多篇关于此的帖子。感谢大家阅读!

标签: javascriptnode.jsasynchronousdiscorddiscord.js

解决方案


After alot of coding i reached my goal; Making a dynamic help menu with pagination. This is the code:

https://pastebin.com/QGVfutti

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:

enter image description here

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!


推荐阅读