TS2339 错误: "Property 'forEach' does not exist on type 'Collection<string, GuildMember>"不和谐



当尝试使用以下代码时,我得到错误消息Property 'forEach' does not exist on type 'Collection<string, GuildMember>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.`)
    }
}

我不知道为什么会发生这种情况,在使用DiscordJS时找不到有关此错误的任何信息。

下面是完整的错误日志:

    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) => {
                                    ~~~~~~~

当控制台记录'message.member. '时语音' 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:UsersnobypOneDriveDokumente.maincodebypassbotcommandswarp.ts:4
    callback: async (message: Message, ...args: string[]) => {
           ^
TypeError: Cannot read properties of null (reading 'id')

这将检查被拉出的通道是否为语音通道,如果不是return通道。如果您运行此操作并且没有发生任何事情并且没有任何错误,则如果您为channel2pullFrom设置的通道不是VC

问题是没有指定正确的意图。修改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.`)
        }
    }
}

相关内容

最新更新