【问题标题】:Loop for display help command循环显示帮助命令
【发布时间】:2020-05-06 03:37:54
【问题描述】:

实际上,我尝试使用 discord.js 创建一个机器人并尝试执行帮助命令。

我不明白为什么我的循环会这样做(我知道这不是一个好方法)

let i = 0;
const embed = new RichEmbed();

if (args.length < 1) {
  embed.setColor('#5B4DCA');
  while (i < list_groups.length) {
    let x = 0;
    embed.setTitle(`${list_groups[i]}`)
    while (x < groups.length) {
      if (groups[x] === list_groups[i]) {
        embed.addField('test1', 'test2')
      }
      x++;
    }
    message.channel.send(embed)
    i++;
  }
}

“Modérations”应该显示一个命令,“level & rank”也是,“Outils”4 命令和“Sondage”也是

enter image description here

【问题讨论】:

  • 要回答你的问题,需要了解list_groups的结构
  • "list_groups" 包含所有组的列表,它由 this 设置(nb : bloc of code)和 "groups" 它是命令的属性,看起来像一个类别` if (!list_groups.includes (f.help.groups)) { list_groups.push(f.help.groups) } `
  • 请显示您的exports.help 块之一。

标签: javascript arrays loops discord.js


【解决方案1】:

我认为你的解决方法不对。如果您将有超过 10 个组,机器人将发送垃圾邮件命令列表。如果args.length===0,则显示所有类别列表的第一种方法,如果 args !==0 您尝试查找此类别中的所有命令。要嵌入不和谐,您只能添加 18 个字段,因此如果您在 categroy 中有超过 18 个命令,您将收到 api 错误。所以你需要将命令拼接到页面上。

如果 args.length === 0,或命令组未罚款,此代码将显示所有类别。 如果组被罚款机器人在组中发送带有命令的嵌入消息(最多 10 个),如果页面超过 1,则响应消息,因此用户可以通过响应更改页面。

   const {Discord,RichEmbed} = require('discord.js');
    const {prefix,token,classic_roles} = require('../config.json');
    const logs = require('../logs/logs');

    module.exports.run = async (bot, message, args) => {
        if(args.length === 0) {
            //Show user all allowed groups commands 
            let commandCategories = bot.commands.map(command => `!help ${command.help.groups}`).filter(onlyUnique).join('\n') //find all categories and get onlyUnique
            let helpMessage = `**The list of command groups:**\n\n ${commandCategories}`
            let Embed = new Discord.RichEmbed()
                .setAuthor(message.author.tag, message.author.displayAvatarUrl)
                .setDescription(helpMessage)
                .setColor('#e0c40b')
            message.channel.send(Embed)
        } else {
            //try find required group 
            let commandsInCategory = bot.commands.filter(command => command.help.groups === args.join(' ').toLowerCase())
            if(commandsInCategory.size === 0) {
                // if no group find, then display user list of groups 
                let commandCategories = bot.commands.map(command => `!help ${command.help.groups}`).filter(onlyUnique).join('\n')
                let helpMessage = `**For get command list use**\n\n ${commandCategories}`
                        let Embed = new Discord.RichEmbed()
                .setAuthor(message.author.tag, message.author.displayAvatarUrl)
                .setDescription(helpMessage)
                .setColor('#e0c40b')
            message.channel.send(Embed)
                return
            }
            let counter = 0
            let allCommands = []
            commandsInCategory.map(command => {
                allCommands.push({
                    name: command.help.name,
                    description: command.help.description
                })
            })

            allCommands = generateHelpArray(allCommands)
            //for better display, we will display only 10 in page
            let Embed = new Discord.RichEmbed()
                Embed.setAuthor(message.author.tag, message.author.displayAvatarUrl)
                Embed.setDescription(`The list command of group : **${args.join(' ')}**`)
                allCommands[counter].map(command => {
                    Embed.addField(`**${command.name}**`,`${command.description}`,false)
                })
                Embed.setColor('#E8DB0E')
                Embed.setFooter(`Page ${counter+1} of ${allCommands.length}`)
                message.channel.send(Embed).then(msg => {
                    if(allCommands.length < 2) return
                    // To change page we will use react emoji 
                    msg.react(`◀️`).then(() => msg.react('▶️'))
                    const filter = (reaction, user) => {
                        return [`◀️`, '▶️'].includes(reaction.emoji.name) && user.id === message.author.id;
                    };
                    const collector = msg.createReactionCollector(filter, { max:50, time: 60000 });
                    collector.on('collect', (reaction, reactionCollector) => {
                        if (reaction.emoji.name === `◀️`) {
                            //Change counter, remove user reaction and call change embed function 
                            reaction.remove(message.author.id)
                            counter-=1
                            if(counter < 0) counter = 0
                            editEmbed(message, msg, counter, args.join(' '), allCommands)
                        } else if (reaction.emoji.name === `▶️`) {
                            //Change counter, remove user reaction and call change embed function 
                            reaction.remove(message.author.id)
                            counter+=1
                            if(counter >= allCommands.length) counter = allCommands.length -1
                            editEmbed(message, msg, counter, args.join(' '), allCommands)
                        }
                   });
                   collector.on('end', (reaction, reactionCollector) => {
                    msg.clearReactions()
                   })
                })

        }
    }

    module.exports.help = {
        name: "help",
        description: "Vous permet d'obtenir toutes les commandes accessibles pour vos rôles.",
        access: "Public",
        groups: "Outils"
    }




    const onlyUnique = (value, index, self) => { 
        return self.indexOf(value) === index;
    }


    const editEmbed = (message, msg, counter, category, allCommands) => {
        let Embed  = new Discord.RichEmbed()
            Embed.setAuthor(message.author.tag, message.author.displayAvatarURL)
            Embed.setDescription(`The list command of group : **${category}**`)
            Embed.setColor('#E8DB0E')
            allCommands[counter].map(command => {
                Embed.addField(`**${command.name}**`,`${command.description}`,false)
            })
            Embed.setFooter(`Page ${counter+1} of ${allCommands.length}`)
            msg.edit(Embed)
    }

    const generateHelpArray = (arr) => {
        let newArray = [];
        for (let i = 0; i < arr.length; i+=10) {
        newArray.push(arr.slice(i,i+10))
        }
        return newArray
    }

【讨论】:

  • 感谢代码,实际上使用此代码,不会显示任何命令,但我会在您的代码的帮助下重新编码,我对嵌入有更好的理解。对不起我的英语,我对这种语言真的很差
  • 那是显示命令名称,但是你可以修改Array args列表,只需将其添加到allCommands.push({ name: command.help.name, description: command.help.description, })
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 2010-09-26
  • 2010-09-23
  • 1970-01-01
相关资源
最近更新 更多