Kevin Powell - CommunityKP-C
Kevin Powell - Community12mo ago
11 replies
vince

TypeScript type error

Hi guys! I have a type error where I'm at a loss on how to proceed.

I'm writing a discord bot that fetches messages in a channel, filters them, and then transforms the data for my backend models.

Please see an example of the filter function:
/**
 * Filters an array of messages
 * @param messages An array of `Message`
 * @param pivot
 * @returns An array of `Message` that are written by a user, contain text, and hav a specific reaction
 */
const getQualifyingMessages = async (
  channel: TextChannel,
  pivot?: string,
): Promise<Message[]> => {
  // there's more but unimportant...

  return messages.filter((message) => {
    return (
      isTextMessage(message) &&
      !isBotMessage(message) &&
      !!getMessageReaction(message) // If message contains specified reaction
    );
  });
};


I'm filtering for 3 things:
1) if the message is a text message,
2) if the message is written by a user,
3) if the message contains a specific reaction

Here is the getMessageReaction() method:
/**
 * Get specific reaction on a message
 * @param message A text message
 * @param filter The name of the reaction or emoji to filter for
 * @returns A specific reaction or null if one isn't found
 */
const getMessageReaction = (
  message: Message,
  filter: string = "💹",
): MessageReaction | null => {
  const [...all] = message.reactions.cache.values();

  const find = all.filter((reaction) => {
    const name = reaction.emoji.name ?? "";
    return filter.includes(name);
  });

  return find.pop() ?? null;
};


As you can see, I'm checking to see if the message contains a specific reaction via filter and returning null if not.

The issue is when I go to mutate my data.
Was this page helpful?