Does .partial then .fetch fix .guild being undefined on a message?

I’m getting a runtime error where a message in a guild has .guild as undefined even though it should exist. From my understanding this is because the guild isn’t in the cache I’m looking for the best solution to this, currently my plan is to do: if(message.partial) { message = await message.fetch() } Does .fetch also fetch the guild and the channel, thread, etc that the message allows you to access on it? Thanks
6 Replies
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Rhys
Rhys16mo ago
From the source code: /** * Fetch this message. * @param {boolean} [force=true] Whether to skip the cache check and request the API * @returns {Promise<Message>} */ fetch(force = true) { if (!this.channel) return Promise.reject(new DiscordjsError(ErrorCodes.ChannelNotCached)); return this.channel.messages.fetch({ message: this.id, force }); } That’s a lil ugly but makes sense I guess, so I’d first need to see if the guild exist in the cache, if it doesn’t then fetch it, then if the channel exists in the cache, if not fetch it, and then if it’s a thread fetch the channel threads by the message id and if that doesn’t exist then it doesn’t have a child thread Is there any better approach to this?
monbrey
monbrey16mo ago
There's no reason guild should be undefined on an actual Message, ever If anything, it could be null
Rhys
Rhys16mo ago
Guild was a bad example sorry, the actual runtime error I got was that the channel was null
Rhys
Rhys16mo ago
It doesn’t make sense there for message.channel to be null since a message always has to be in a channel, so I’m guessing the message channel got ejected from the cache Relevant source code: https://github.com/AnswerOverflow/AnswerOverflow/blob/main/apps/discord-bot/src/utils/conversions.ts Also I probably shouldn’t use null and undefined interchangeably sorry about that
monbrey
monbrey16mo ago
A DM channel could be null if not cached, or if you do some other channel cache sweeping