discord.js icon indicating copy to clipboard operation
discord.js copied to clipboard

MessageManager#fetch has signatures, but none of those signatures are compatible with each other.

Open ImRodry opened this issue 2 years ago • 10 comments

Which package is this bug report for?

discord.js

Issue description

On v14.3.0, when you fetch the messages of a channel through message.channel.messages.fetch() without narrowing down the type of message.channel you get a Collection of Message or Message. Upon trying to call .filter on this collection, you will get the following error: This expression is not callable. Each member of the union type '{ <K2 extends string>(fn: (value: Message, key: string, collection: Collection<string, Message>) => key is K2): Collection<K2, Message>; <V2 extends Message<...>>(fn: (value: Message<...>, key: string, collection: Collection<...>) => value is V2): Collection<...>; (fn: (value: Message<...>, key: st...' has signatures, but none of those signatures are compatible with each other. ts(2349)

This is because, since the message manager can come from multiple types of channels, the boolean type parameter can be true or false, and those two are not compatible with each other, thus TS cannot infer the type of the resulting filtered Collection

Code sample

client.on("message", async message => {
    (await message.channel.messages.fetch()).filter(m => m.author.id === message.author.id)
})

Package version

14.3.0

Node.js version

18.7.0

Operating system

Windows 11

Priority this issue should have

High (immediate attention needed)

Which partials do you have configured?

Not applicable (subpackage bug)

Which gateway intents are you subscribing to?

Not applicable (subpackage bug)

I have tested this issue on a development release

No response

ImRodry avatar Aug 22 '22 17:08 ImRodry

I haven’t been able to update since v14.3.0 due to this issue. Can this be fixed asap please?

ImRodry avatar Sep 21 '22 17:09 ImRodry

I was looking in to this. I initially thought to change TextBasedChannel's definitions, but it's just an exclusion of Channel, so modifying that wouldn't fix it. There's no BaseTextChannel class I can modify. Each channel extends BaseChannel which isn't always a TextChannel so I can't change that.

Could anyone help point me in the right direction with this? Thanks!

cobaltt7 avatar Sep 21 '22 21:09 cobaltt7

I am also having the same issue with this. I am currently trying to make a purge command that clears messages from a user, and whenever I try to filter the messages to push them to an array and it filter out the author ID, it gives this error. Even on discord.js v14.7.1. Update: from what it seems, it's probably effecting versions newer than 14.3.0. Downgrading to 14.2.0 fixed it for me, so idk what the hell they did.

ararouu avatar Jan 18 '23 19:01 ararouu

So looks like its effected by #8538

jaw0r3k avatar Jan 21 '23 15:01 jaw0r3k

prob

ararouu avatar Jan 21 '23 17:01 ararouu

So, I have found the issue. In order to use messages.filter, the channel you are fetching messages from needs to be as TextChannel. Doing const messages = await (<Channel> as TextChannel).messages.fetch(); and then messages.filter() should work.

ararouu avatar Feb 10 '23 15:02 ararouu

That is not relevant to this issue. If you only care about text channels, you should check the channel's type.

Jiralite avatar Feb 10 '23 15:02 Jiralite

That is not relevant to this issue. If you only care about text channels, you should check the channel's type.

Either way works, if you check the channels type is GuildText or casting it.

ararouu avatar Feb 10 '23 15:02 ararouu

Except that is not a valid solution because this issue happens when you want to check the messages from any text based channel

ImRodry avatar Feb 10 '23 16:02 ImRodry

I'm running into the same problem. Gotta @ts-ignore around this thing.

nukeop avatar Mar 21 '23 23:03 nukeop

This issue has now also been resolved on TS as of v5.2 so for people in previous versions the fix will be to update TS. There is a caveat though that is explained in https://devblogs.microsoft.com/typescript/announcing-typescript-5-2-rc/#easier-method-usage-for-unions-of-arrays

ImRodry avatar Aug 10 '23 15:08 ImRodry