use branded types for messageType determination

This commit is contained in:
kenobi 2023-07-05 22:54:43 +02:00
parent 1a13638ed9
commit 8c3cf7829b
4 changed files with 18 additions and 9 deletions

View File

@ -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())

View File

@ -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())

View File

@ -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]")
}

View File

@ -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 })