Home > Software design >  TS2339 Error: "Property 'forEach' does not exist on type 'Collection<string,
TS2339 Error: "Property 'forEach' does not exist on type 'Collection<string,

Time:04-18

When trying to use the following code i get the error messages Property 'forEach' does not exist on type 'Collection<string, GuildMember> and Property 'size' does not exist on type 'Collection<string, GuildMember>

import { Message } from "discord.js";

export default {
    callback: async (message: Message, ...args: string[]) => {
        const channel2pullFrom = message.guild.channels.cache.get('964675235314020442')
        const sendersChannel = message.member.voice.channel.id
        const totalCount = channel2pullFrom.members.size
    
        if (!message.member.permissions.has('MOVE_MEMBERS')) return
        if (!message.member.voice.channel) return message.reply("Error: Executor of command is not in a Voice Channel.")
       
        channel2pullFrom.members.forEach((member) => {
            member.voice.setChannel(sendersChannel)
        })
       
        message.reply(`Moved ${totalCount} members.`)
    }
}

I have no idea why that's happening and couldn't find anything abour this error when working with DiscordJS.

Here's the full error log:

    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
commands/warp.ts:7:53 - error TS2339: Property 'size' does not exist on type 'Collection<string, GuildMember> | ThreadMemberManager'.
  Property 'size' does not exist on type 'ThreadMemberManager'.

7         const totalCount = channel2pullFrom.members.size
                                                      ~~~~
commands/warp.ts:12:34 - error TS2339: Property 'forEach' does not exist on type 'Collection<string, GuildMember> | ThreadMemberManager'.
  Property 'forEach' does not exist on type 'ThreadMemberManager'.

12         channel2pullFrom.members.forEach((member) => {
                                    ~~~~~~~

When console logging 'message.member.voice' this get's logged:

VoiceState {
  guild: <ref *1> Guild {
    id: '897642715368534027',
    name: 'Free Depression Store',
    icon: '55bbdc9124e504f062e5377a2fc4066e',
    features: [ 'COMMUNITY', 'NEWS' ],
    commands: GuildApplicationCommandManager {
      permissions: [ApplicationCommandPermissionsManager],
      guild: [Circular *1]
    },
    members: GuildMemberManager { guild: [Circular *1] },
    channels: GuildChannelManager { guild: [Circular *1] },
    bans: GuildBanManager { guild: [Circular *1] },
    roles: RoleManager { guild: [Circular *1] },
    presences: PresenceManager {},
    voiceStates: VoiceStateManager { guild: [Circular *1] },
    stageInstances: StageInstanceManager { guild: [Circular *1] },
    invites: GuildInviteManager { guild: [Circular *1] },
    scheduledEvents: GuildScheduledEventManager { guild: [Circular *1] },
    available: true,
    shardId: 0,
    splash: null,
    banner: null,
    description: null,
    verificationLevel: 'LOW',
    vanityURLCode: null,
    nsfwLevel: 'DEFAULT',
    discoverySplash: null,
    memberCount: 55,
    large: true,
    premiumProgressBarEnabled: false,
    applicationId: null,
    afkTimeout: 300,
    afkChannelId: null,
    systemChannelId: null,
    premiumTier: 'NONE',
    premiumSubscriptionCount: 0,
    explicitContentFilter: 'ALL_MEMBERS',
    mfaLevel: 'NONE',
    joinedTimestamp: 1648975385274,
    defaultMessageNotifications: 'ONLY_MENTIONS',
    systemChannelFlags: SystemChannelFlags { bitfield: 0 },
    maximumMembers: 500000,
    maximumPresences: null,
    approximateMemberCount: null,
    approximatePresenceCount: null,
    vanityURLUses: null,
    rulesChannelId: '964671035213488128',
    publicUpdatesChannelId: '954169682619957318',
    preferredLocale: 'en-US',
    ownerId: '672835870080106509',
    emojis: GuildEmojiManager { guild: [Circular *1] },
    stickers: GuildStickerManager { guild: [Circular *1] }
  },
  id: '672835870080106509',
  serverDeaf: null,
  serverMute: null,
  selfDeaf: null,
  selfMute: null,
  selfVideo: null,
  sessionId: null,
  streaming: null,
  channelId: null,
  requestToSpeakTimestamp: null
}
C:\Users\nobyp\OneDrive\Dokumente\.main\code\bypassbot\commands\warp.ts:4
    callback: async (message: Message, ...args: string[]) => {
           ^
TypeError: Cannot read properties of null (reading 'id')

CodePudding user response:

This will check that the channel being pulled from is a voice channel and return if it isn’t. If you run this and nothing happens and nothing errors then the channel if you set for channel2pullFrom is not a VC

The issue was that the correct intents were not specified. Change intents to intents: 32767,

import { Message } from "discord.js";

export default {
    callback: async (message: Message, ...args: string[]) => {
        const channel2pullFrom = message.guild.channels.cache.get('964675235314020442')
    // you could also dynamically set this by making it one of the args
    // const channel2pullFrom = message.guild.channels.cache.get(args[0])
    // above line won’t work without knowing more on how your args are handled (are they shifted, etc)
        if (channel2pullFrom.type != 'GUILD_VOICE') {
            return
        } else {
            const sendersChannel = message.member.voice.channelId
            const totalCount = channel2pullFrom.members.size
    
            if (!message.member.permissions.has('MOVE_MEMBERS')) return
            if (!message.member.voice.channel) return message.reply("Error: Executor of command is not in a Voice Channel.")
       
            channel2pullFrom.members.forEach((member) => {
            member.voice.setChannel(sendersChannel)
            })
       
            message.reply(`Moved ${totalCount} members.`)
        }
    }
}
  • Related