Discord.js UnhandledPromiseRejectionWarning: TypeError: message.guild.channels.cache.array.forEach is not a function

Viewed 305

I'm trying to create a ticket function for my discord.js bot, but when I click on an emoji, it throws an error. Here is the code:

let messageEmbed = await message.channel.send(ticketEmbed);
messageEmbed.react("1️⃣");
messageEmbed.react("2️⃣");
messageEmbed.react("3️⃣");
messageEmbed.react("4️⃣");

client.on('messageReactionAdd', async (reaction, user) => {
  if (reaction.message.partial) await reaction.message.fetch();
  if (reaction.partial) await reaction.fetch();
  if (user.bot) return;
  if (!reaction.message.guild) return;


  if (reaction.emoji.name === firstreaction) {
    const categoryId = "822577084223586364"
    var username =  reaction.message.guild.members.cache.get(user.id)
    var userDiscriminator =  reaction.message.guild.members.cache.get(user.id).discriminator
    var ticketExists = false

    message.guild.channels.cache.array.forEach(channel => {
      if(channel.name == userName.toLowerCase() + "-" + userDiscriminator){
        var ticketExists = true
        message.username.send("You already have an open ticket!")

        return
      }
    })
    if (ticketExists) return

    message.guild.channels.create("Playerreport-" +username.toLowerCase + userDiscriminator, {type: "text"})
1 Answers

First of all Collection.array is a method not a property, so you need to call it like Collection.array().

Second, Collections' have a forEach method that is inherited from Maps, you could just go about directly calling it

message.guild.channels.cache.forEach(...)
Related