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');
}


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
Was this page helpful?