diff --git a/server/commands/announce.ts b/server/commands/announce.ts index e803177..80472d5 100644 --- a/server/commands/announce.ts +++ b/server/commands/announce.ts @@ -6,7 +6,7 @@ import { Maybe } from '../interfaces' import { logger } from '../logger' import { Command } from '../structures/command' import { RunOptions } from '../types/commandTypes' -import { messageIsInitialAnnouncement } from '../helper/messageIdentifiers' +import { isInitialAnnouncement } from '../helper/messageIdentifiers' export default new Command({ name: 'announce', @@ -62,7 +62,7 @@ async function sendInitialAnnouncement(guildId: string, requestId: string): Prom return } - const currentPinnedAnnouncementMessages = (await announcementChannel.messages.fetchPinned()).filter(message => messageIsInitialAnnouncement(message)) + const currentPinnedAnnouncementMessages = (await announcementChannel.messages.fetchPinned()).filter(message => isInitialAnnouncement(message)) currentPinnedAnnouncementMessages.forEach(async (message) => await message.unpin()) currentPinnedAnnouncementMessages.forEach(message => message.delete()) diff --git a/server/events/deleteAnnouncementsWhenWPEnds.ts b/server/events/deleteAnnouncementsWhenWPEnds.ts index 9b1a0b0..18c572d 100644 --- a/server/events/deleteAnnouncementsWhenWPEnds.ts +++ b/server/events/deleteAnnouncementsWhenWPEnds.ts @@ -2,7 +2,7 @@ import { Collection, GuildScheduledEvent, GuildScheduledEventStatus, Message } f import { v4 as uuid } from "uuid"; import { client } from "../.."; import { logger } from "../logger"; -import { messageIsInitialAnnouncement } from "../helper/messageIdentifiers"; +import { isInitialAnnouncement } from "../helper/messageIdentifiers"; export const name = 'guildScheduledEventUpdate' @@ -26,7 +26,7 @@ export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildSche const events = await newEvent.guild.scheduledEvents.fetch() - const wpAnnouncements = (await announcementChannel.messages.fetch()).filter(message => !messageIsInitialAnnouncement(message)) + const wpAnnouncements = (await announcementChannel.messages.fetch()).filter(message => !isInitialAnnouncement(message)) const announcementsWithoutEvent = filterAnnouncementsByPendingWPs(wpAnnouncements, events) logger.info(`Deleting ${announcementsWithoutEvent.length} announcements.`, { guildId, requestId }) announcementsWithoutEvent.forEach(message => message.delete()) diff --git a/server/helper/messageIdentifiers.ts b/server/helper/messageIdentifiers.ts index 1b33bde..76832af 100644 --- a/server/helper/messageIdentifiers.ts +++ b/server/helper/messageIdentifiers.ts @@ -1,11 +1,20 @@ import { Message } from "discord.js"; -export function messageIsVoteMessage(msg: Message): boolean { + +// branded types to differentiate objects of identical Type but different contents +export type VoteEndMessage = Message & { readonly __brand: 'vote' } +export type AnnouncementMessage = Message & { readonly __brand: 'announcement' } +export type VoteMessage = Message & { readonly __brand: 'voteend' } + +export type DiscordMessage = VoteMessage | VoteEndMessage | AnnouncementMessage + +export function isVoteMessage(msg: Message): msg is VoteMessage { return msg.cleanContent.includes('[Abstimmung]') } -export function messageIsInitialAnnouncement(msg: Message): boolean { +export function isInitialAnnouncement(msg: Message): msg is AnnouncementMessage { return msg.cleanContent.includes("[initial]") } -export function messageIsVoteEndedMessage(msg: Message): boolean { +export function isVoteEndedMessage(msg: Message): msg is VoteEndMessage { return msg.cleanContent.includes("[Abstimmung beendet]") } + diff --git a/server/structures/client.ts b/server/structures/client.ts index f445585..84fe333 100644 --- a/server/structures/client.ts +++ b/server/structures/client.ts @@ -9,7 +9,7 @@ import { JellyfinHandler } from "../jellyfin/handler"; import { logger } from "../logger"; import { CommandType } from "../types/commandTypes"; import { checkForPollsToClose } from "../commands/closepoll"; -import { messageIsInitialAnnouncement } from "../helper/messageIdentifiers"; +import { isInitialAnnouncement } from "../helper/messageIdentifiers"; import VoteController from "../helper/vote.controller"; @@ -155,7 +155,7 @@ export class ExtendedClient extends Client { } this.announcementRoleHandlerTask.set(guild.id, schedule("*/10 * * * * *", async () => { const requestId = uuid() - const messages = (await textChannel.messages.fetchPinned()).filter(message => messageIsInitialAnnouncement(message)) + const messages = (await textChannel.messages.fetchPinned()).filter(message => isInitialAnnouncement(message)) if (messages.size > 1) { logger.error("More than one pinned announcement Messages found. Unable to know which one people react to. Please fix!", { guildId: guild.id, requestId })