2023-06-23 15:51:07 +02:00
|
|
|
import { Collection, GuildScheduledEvent, GuildScheduledEventStatus, Message } from "discord.js";
|
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
import { client } from "../..";
|
|
|
|
import { logger } from "../logger";
|
|
|
|
|
|
|
|
|
|
|
|
export const name = 'guildScheduledEventUpdate'
|
|
|
|
|
|
|
|
export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildScheduledEvent) {
|
2023-06-24 20:56:58 +02:00
|
|
|
const requestId = uuid()
|
|
|
|
try {
|
|
|
|
if (!newEvent.guild) {
|
|
|
|
logger.error("Event has no guild, aborting.", { guildId: newEvent.guildId, requestId })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const guildId = newEvent.guildId
|
2023-06-23 15:51:07 +02:00
|
|
|
|
2023-06-24 20:56:58 +02:00
|
|
|
if (newEvent.description?.toLowerCase().includes("!wp") && newEvent.status === GuildScheduledEventStatus.Completed) {
|
|
|
|
logger.info("A watchparty ended. Cleaning up announcements!", { guildId, requestId })
|
|
|
|
const announcementChannel = client.getAnnouncementChannelForGuild(newEvent.guild.id)
|
|
|
|
if (!announcementChannel) {
|
|
|
|
logger.error("Could not find announcement channel. Aborting", { guildId: newEvent.guild.id, requestId })
|
|
|
|
return
|
|
|
|
}
|
2023-06-23 15:51:07 +02:00
|
|
|
|
2023-06-24 20:56:58 +02:00
|
|
|
const events = await newEvent.guild.scheduledEvents.fetch()
|
2023-06-23 15:51:07 +02:00
|
|
|
|
2023-06-24 20:56:58 +02:00
|
|
|
const wpAnnouncements = (await announcementChannel.messages.fetch()).filter(message => !message.cleanContent.includes("[initial]"))
|
|
|
|
const announcementsWithoutEvent = filterAnnouncementsByPendingWPs(wpAnnouncements, events)
|
|
|
|
logger.info(`Deleting ${announcementsWithoutEvent.length} announcements.`, { guildId, requestId })
|
|
|
|
announcementsWithoutEvent.forEach(message => message.delete())
|
2023-06-23 15:51:07 +02:00
|
|
|
}
|
2023-06-24 20:56:58 +02:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(<string>error, { guildId: newEvent.guildId, requestId })
|
|
|
|
}
|
2023-06-23 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function filterAnnouncementsByPendingWPs(messages: Collection<string, Message<true>>, events: Collection<string, GuildScheduledEvent<GuildScheduledEventStatus>>): Message<true>[] {
|
2023-06-24 20:56:58 +02:00
|
|
|
const filteredMessages: Message<true>[] = []
|
|
|
|
for (const message of messages.values()) {
|
|
|
|
let foundEventForMessage = false
|
|
|
|
for (const event of events.values()) {
|
|
|
|
if (message.cleanContent.includes(event.id)) { //announcement always has eventid because of eventbox
|
|
|
|
foundEventForMessage = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundEventForMessage) {
|
|
|
|
filteredMessages.push(message)
|
2023-06-23 15:51:07 +02:00
|
|
|
}
|
2023-06-24 20:56:58 +02:00
|
|
|
}
|
|
|
|
return filteredMessages
|
2023-06-23 15:51:07 +02:00
|
|
|
}
|