简体   繁体   English

动态帮助命令 discord.js

[英]Dynamic help command discord.js

I'm currently making a discord bot but I have an issue.我目前正在制作一个不和谐的机器人,但我有一个问题。 I'm still quite new to discord.js and node.js.我对 discord.js 和 node.js 还是很陌生。 This is my code:这是我的代码:

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();
};

This code doesnt have any errors but this is the issue I'm having:此代码没有任何错误,但这是我遇到的问题:

问题

The issue is that it runs the main function before it finished the getMenu function.问题是它在完成getMenu函数之前运行了 main 函数。 How to solve this?如何解决这个问题? I have read multiple posts about this.我已经阅读了多篇关于这个的帖子。 Thanks for reading everyone!感谢大家阅读!

After alot of coding i reached my goal;经过大量编码后,我达到了目标; Making a dynamic help menu with pagination.使用分页制作动态帮助菜单。 This is the code:这是代码:

https://pastebin.com/QGVfutti 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您唯一需要做的就是在 index.js 所在的机器人的根目录中创建一个名为 config.json 的文件,并添加一个名为“embedColor”的键,将您的命令放入

./src/commands/CATAGORY/COMMAND.js ./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.您可以在 pastebin 的第 19 行更改描述代码和命令名称。 if you want a image you can add a new line after line 13:如果你想要一个图像,你可以在第 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.抱歉我的英语不好,感谢 Lioness100 编辑文本。 English is not my first language :) Have a nice day everyone bye!英语不是我的第一语言 :) 祝大家有个美好的一天,再见!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM