A typeguard to check if the received message is from inside a guild.

I'm currently using the @discordjs/core package along with it's other subpackages and discord-api-types for my bot. Based off of the util functions from API Types. I wanted to check if the message is from a guild or not using a typeguard. Rather than checking if both guild_id and member properties exist using if functions everytime. After reading the docs. I realized the guild_id property only exists when the message is from a guild. (obviously) So I made this just like the isGuildInteraction typeguard.
export type APIGuildMessage = GatewayMessageCreateDispatchData &
Required<Pick<GatewayMessageCreateDispatchData, 'guild_id' | 'member'>>;

export function isGuildMessage(
message: GatewayMessageCreateDispatchData
): message is APIGuildMessage {
return Reflect.has(message, 'guild_id');
}
export type APIGuildMessage = GatewayMessageCreateDispatchData &
Required<Pick<GatewayMessageCreateDispatchData, 'guild_id' | 'member'>>;

export function isGuildMessage(
message: GatewayMessageCreateDispatchData
): message is APIGuildMessage {
return Reflect.has(message, 'guild_id');
}
Idk if this actually exists and I missed it or if this is redundant in usage. But I just thought it'd be nice. Any thoughts are welcome <:teri_smug:723908622790164500>
2 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
KAVI
KAVI11mo ago
Ooooh yeah true... My mind actually skipped that part entirely. Thanks for pointing that out <:teri_love:723908621515227146> Then I think the best approach is to have isGuildWebhookMessage and isGuildMemberMessage Omitting the members property for webhook type and renaming the APIGuildMessage to APIGuildMemberMessage Then creating a Guild message type which only has the guild id property available and others optional... True lmao I just realized that.... Atleast my mind tried <:teri_xd:723928477346037869> Thanks again.... I'll just close this down.