简体   繁体   English

Discord.js 我的“帮助”命令不起作用

[英]Discord.js my command for 'help' is not working

So basically i've made a help command for my discord.js bot and I tried implementing a page system into the bot but I have two issues.所以基本上我已经为我的 discord.js 机器人创建了一个帮助命令,我尝试在机器人中实现一个页面系统,但我有两个问题。 1: Whenever I react to an emoji the bot lags and sometimes it won't change pages, and 2: The commands won't split into two halves so I'm not able to display two halves of the commands onto seperate pages so that the first embed isn't so long. 1:每当我对表情符号做出反应时,机器人都会滞后,有时它不会更改页面,以及 2:命令不会分成两半,因此我无法将命令的两半显示到单独的页面上,这样第一个嵌入不是那么长。 Here's my code这是我的代码

const {
    MessageEmbed
} = require("discord.js");
const {
    stripIndents
} = require("common-tags");
const {
    getMember,
    formatDate
} = require("../../functions.js");
const viewPerms = require("../../cmdviewperms.json");

module.exports = {
    config: {
        name: "help",
        aliases: ["?"],
        category: "Info",
        description: "Returns help information",
        usage: "!help",
    },
    run: (client, message, args) => {
        let cmds = client.commands;
        let allUsers = {
            Everyone: [],
            ADMINISTRATOR: []
        }

        for (let value of cmds) {
            let category = value[1].config.category;
            viewPerms.forEach(e => {
                if (e.cmdGroup.toLowerCase() == category.toLowerCase()) {
                    //console.log(`CATEGORY OF ${value[1].config.name} IS ${e.cmdGroup}`)
                    if (value[1].config.bypassUsers) {
                        value[1].config.bypassUsers.forEach(x => {
                            //console.log(`${value[1].config.name} ADDED TO ${x}; CMDGROUP: ${e.cmdGroup}`)
                            allUsers[x].push(value[1].config);
                        })
                    } else {
                        e.AllowedUsers.forEach(x => {
                            //console.log(`${value[1].config.name} ADDED TO ${x}; CMDGROUP: ${e.cmdGroup}`)
                            allUsers[x].push(value[1].config);
                        })
                    }
                }
            })
        }
        let adminEmbedText = "";
        let userEmbedText = "";
        allUsers.ADMINISTRATOR.forEach(e => {
            let name = e.name.charAt(0).toUpperCase() + e.name.slice(1, e.name.length);
            adminEmbedText = `${adminEmbedText} ${name}\n`;
        })

        allUsers.Everyone.forEach(e => {
            let name = e.name.charAt(0).toUpperCase() + e.name.slice(1, e.name.length);
            userEmbedText = `${userEmbedText} ${name}\n`;
        })

        //const embedHalf = Math.ceil(userEmbedText.length / 2);
        // var firsthalf = userEmbedText.lastIndexOf(' ', embedHalf);
        // var secondhalf = userEmbedText.indexOf(' ', embedHalf + 1);

        const embedhalf = Math.ceil(userEmbedText.split(" "))
        var firsthalf = userEmbedText.lastIndexOf(' ', embedhalf);
        var secondhalf = userEmbedText.indexOf(' ', embedhalf + 1);

        let page = 1 
        let pages = [firsthalf, secondhalf]


        let adminEmbed = new MessageEmbed()
            .setTitle("Commands")
            .setColor("RANDOM")
            .setDescription(adminEmbedText)
            .setFooter(`Page ${page} of ${pages.length}`)
        let userEmbed = new MessageEmbed()
            .setTitle("Commands")
            .setColor("RANDOM")
            .setDescription(userEmbedText)
            .setFooter(`Page ${page} of ${pages.length}`)


                //console.log(sh)
                //console.log(fh)
        if (args.length == 0) {
            if (message.member.hasPermission("ADMINISTRATOR")) return message.author.send(adminEmbed);
            return message.channel.send(userEmbed).then(msg => {

                msg.react('◀️').then(r => {
                    msg.react('▶️')

                    const filter = (reaction, user) => {
                        return ['◀️', '▶️'].includes(reaction.emoji.name) && user.id === message.author.id;
                    };

                    const back = msg.createReactionCollector(filter, {
                        timeout: 60000
                    });
                    const forw = msg.createReactionCollector(filter, {
                        timeout: 60000
                    })

                    back.on('collect', (r, u) => {
                        if (page === 1) return r.users.remove(r.users.cache.filter(u => u === message.author).first())
                        page--
                        userEmbed.setDescription(userEmbedText)
                        userEmbed.setFooter(`Page ${page} of ${pages.length}`)
                        msg.edit(userEmbed)
                        r.users.remove(r.users.cache.filter(u => u === message.author).first())
                    })
                
                    forw.on('collect', (r, u) => {
                        if (page === pages.length) return r.users.remove(r.users.cache.filter(u => u === message.author).first())
                        page++
                        userEmbed.setDescription(userEmbedText)
                        userEmbed.setFooter(`Page ${page} of ${pages.length}`)
                        msg.edit(userEmbed)
                        r.users.remove(r.users.cache.filter(u => u === message.author).first())
                    })
                  })
                })
    
        } else {
            let cmd = args.join(" ");
            let cmdFound = false;
            let embed = "No command found!";
            if (message.member.hasPermission("ADMINISTRATOR")) {
                allUsers.ADMINISTRATOR.forEach(e => {
                    if (cmd.toLowerCase() == e.name.toLowerCase()) {
                        let name = e.name.charAt(0).toUpperCase() + e.name.slice(1, e.name.length);
                        embed = new MessageEmbed()
                            .setTitle(`Command: ${name}`)
                            .setColor("RANDOM")
                            .setDescription(`Name: ${name} \n\n > Description: ${e.description} \n\n> Category: ${e.category} \n\n > Usage: ${e.usage}`);
                    }
                })
            }
            allUsers.Everyone.forEach(e => {
                if (cmd.toLowerCase() == e.name.toLowerCase()) {
                    let name = e.name.charAt(0).toUpperCase() + e.name.slice(1, e.name.length);
                    embed = new MessageEmbed()
                        .setTitle(`Command: ${e.name}`)
                        .setColor("RANDOM")
                        .setDescription(`Name: ${name} \n\n > Description: ${e.description} \n\n> Category: ${e.category} \n\n > Usage: ${e.usage}`);
                }
            })
            return message.channel.send(embed);
        }
    }
}```

The first issue I see skimming over this code is the use of Math.ceil() on the array you get from a split() , which doesn't make any sense as Math.ceil() only takes numbers.我看到浏览此代码的第一个问题是在从split()获得的数组上使用Math.ceil() ) ,这没有任何意义,因为Math.ceil()只接受数字。 This is what's causing your help text to not split into two pages.这就是导致您的帮助文本没有分成两页的原因。 Further down, your two message reaction collectors forw and back are both looking for the same reactions, because you created them using the same filter.再往下,您的两个消息反应收集器forwback都在寻找相同的反应,因为您使用相同的过滤器创建了它们。 Both of the callbacks execute every time either reaction is hit, because both reactions pass the filter.每次点击任一反应时都会执行这两个回调,因为这两个反应都通过了过滤器。 That makes it a tossup whether the page actually switches or not.这使得页面是否实际切换成为一个问题。 Make the collectors use two different filters, each one only looking for the correct reaction.让收集器使用两个不同的过滤器,每个过滤器只寻找正确的反应。 I haven't looked over the code line by line, so there might be more issues, but those are the two main issues I see.我没有逐行查看代码,因此可能存在更多问题,但这是我看到的两个主要问题。

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

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