discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

TypeError: Cannot use 'in' operator to search for 'color' in 0 - discord.js\src\structures\MessageEmbed.js:81 #6543

Closed NekoSuneVR closed 3 years ago

NekoSuneVR commented 3 years ago

Please describe the problem you are having in as much detail as possible:

The MessageEmbed returning error on line 81 in this.color = 'color' in data ? Util.resolveColor(data.color) : null; by saying

TypeError: Cannot use 'in' operator to search for 'color' in 0

i have color code set by HEX with # and it just mainly crashes my bot over and over when command embed not working.

since im updating bot from v12 to v13, some codes fully break when exact info i found update code from v12 to v13 on @ https://discordjs.guide/additional-info/changes-in-v13.html

LIB i using for Music is discord-music-player@8.0.1

Include a reproducible code sample here, if possible:


const { MessageEmbed } = require("discord.js");
const { LOCALE } = require("../util/BotUtil");
const i18n = require("i18n");

i18n.setLocale('en');

module.exports = {
    name: "queue",
    cooldown: 5,
    aliases: ["q"],
    description: i18n.__("queue.description"),
    category: "Music",
    usage: "%queue",
    async execute(message, args) {

        var connection = message.client.sqlconn;

        function getGuildToggle(guildID) {
            return new Promise((resolve, reject) => {
                connection.query(`SELECT * FROM guild_command_toggle WHERE guild_id = '${guildID}'`, (err, rows) => {
                    if (err) return reject(err);

                    resolve(rows);
                });
            });
        }

        const [guildsettingstoggle] = await getGuildToggle(message.guild.id) // destructuring 'rows' array
            .catch(console.error);

        if (guildsettingstoggle.musicToggle != 0) {

            const permissions = message.channel.permissionsFor(message.client.user);
            if (!permissions.has(["MANAGE_MESSAGES", "ADD_REACTIONS"]))
                return message.reply(i18n.__("queue.missingPermissionMessage"));

            const queue = message.client.player.getQueue(message.guild.id)
            if (!queue) return message.channel.send(i18n.__("queue.errorNotQueue"));

            let currentPage = 0;
            const embeds = generateQueueEmbed(message, queue.songs);

            const queueEmbed = await message.channel.send({
                content: `**${i18n.__mf("queue.currentPage")} ${currentPage + 1}/${embeds.length}**`,
                embeds: [currentPage]
            });

            console.log(currentPage)

            try {
                await queueEmbed.react("⬅️");
                await queueEmbed.react("⏹");
                await queueEmbed.react("➡️");
            } catch (error) {
                console.error(error);
                message.channel.send(error.message).catch(console.error);
            }

            const filter = (reaction, user) => ["⬅️", "⏹", "➡️"].includes(reaction.emoji.name) && message.author.id === user.id;
            const collector = queueEmbed.createReactionCollector(filter, { time: 60000 });

            collector.on("collect", async(reaction, user) => {
                try {
                    if (reaction.emoji.name === "➡️") {
                        if (currentPage < embeds.length - 1) {
                            currentPage++;
                            queueEmbed.edit({
                                content: `**${i18n.__mf("queue.currentPage", { page: currentPage + 1, length: embeds.length })}**`,
                                embeds: [currentPage]
                            });
                        }
                    } else if (reaction.emoji.name === "⬅️") {
                        if (currentPage !== 0) {
                            --currentPage;
                            queueEmbed.edit({
                                content: `**${i18n.__mf("queue.currentPage", { page: currentPage + 1, length: embeds.length })}**`,
                                embeds: [currentPage]
                            });
                        }
                    } else {
                        collector.stop();
                        reaction.message.reactions.removeAll();
                    }
                    await reaction.users.remove(message.author.id);
                } catch (error) {
                    console.error(error);
                    return message.channel.send(error.message).catch(console.error);
                }
            });
        } else {

            message.channel.send("Server Adminstrator Disabled Music Intergration on this Server. If want Renable it, please go to our Dashboard and Re-enable it");

        }
    }

};

function generateQueueEmbed(message, queue) {
    let embeds = [];
    let k = 10;

    for (let i = 0; i < queue.length; i += 10) {
        const current = queue.slice(i, k);
        let j = i;
        k += 10;

        const info = current.map((track) => `${++j} - [${track.name}](${track.url})`).join("\n");

        const embed = new MessageEmbed()
            .setTitle(i18n.__("queue.embedTitle"))
            .setThumbnail(message.guild.iconURL())
            .setColor("#F8AA2A")
            .setDescription(
                i18n.__mf("queue.embedCurrentSong", { title: queue[0].name, url: queue[0].url, info: info })
            )
            .setTimestamp();
        embeds.push(embed);
    }

    return embeds;
}

Further details:

Relevant client options:

VoltrexKeyva commented 3 years ago

This is not a bug, you're passing the variable currentPage to the option of embeds which holds a primitive value of 0 aka a number, you're supposed to pass embeds[currentPage] to access the wanted embed data.

NekoSuneVR commented 3 years ago

ah ok, but everythign else same?

VoltrexKeyva commented 3 years ago

ah ok, but everythign else same?

Try it and see, for more questions like this checkout the offical discord.js support server.

NekoSuneVR commented 3 years ago

k thank you