Search code examples
javascriptnode.jsdiscorddiscord.jsnode-modules

TypeError: message.guild.roles.cache.array is not a function


Actually, I don't know how to fix my code anymore, I tried a lot, but I can't solve the problem yet

const Discord = require("discord.js");
const client = new Discord.Client({ intents: 32767 });
const AsciiTable = require("ascii-data-table").default;
const prefix = "#";

client.on("messageCreate", async(message) => {
    if (!message.guild || !message.channel || message.author.bot) return;
    if(message.content.startsWith(prefix + "roles")) {
        var
            ros = message.guild.roles.cache.size,
            data = [["Rank", "RoleName"]]
            for(let i = 0; i < ros; i++) {
                if(message.guild.roles.cache.array()[i].id !== message.guild.id) {
                    data.push([i,`${message.guild.roles.cache.filter(r => r.position == ros-i).map(r=>r.name)}`])
        }
            }
        let res = AsciiTable.table(data)
        message.channel.send({ content: `**\`\`\`xl\n${res}\`\`\`**` });
    }
});

client.login(process.env.TOKEN);

Solution

  • You can change from message.guild.roles.cache.array() to [...message.guild.roles.cache.values()]

    Here is what the docs said:

    These methods existed to provide access to a cached array of Collection values and keys respectively, which other Collection methods relied on internally. Those other methods have been refactored to no longer rely on cache, so those arrays and these methods have been removed.
    You should instead construct an array by spreading the iterators returned by the base Map class methods