简体   繁体   中英

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. 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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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