55 lines
2.9 KiB
TypeScript
55 lines
2.9 KiB
TypeScript
import { GuildMember, GuildScheduledEvent, GuildScheduledEventStatus } from "discord.js";
|
|
import { v4 as uuid } from "uuid";
|
|
import { client, jellyfinHandler } from "../..";
|
|
import { getGuildSpecificTriggerRoleId } from "../helper/roleFilter";
|
|
import { logger } from "../logger";
|
|
import { manageAnnouncementRoles } from "../commands/announce";
|
|
|
|
|
|
export const name = 'guildScheduledEventUpdate'
|
|
|
|
export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildScheduledEvent) {
|
|
try {
|
|
const requestId = uuid()
|
|
logger.debug(`Got scheduledEvent update. New Event: ${JSON.stringify(newEvent, null, 2)}`,{guildId: newEvent.guildId, requestId})
|
|
|
|
if (newEvent.description?.toLowerCase().includes("!wp") && [GuildScheduledEventStatus.Active, GuildScheduledEventStatus.Completed].includes(newEvent.status)) {
|
|
const roles = getGuildSpecificTriggerRoleId(newEvent.guildId).map((key, value)=> value)
|
|
const eventMembers = (await newEvent.fetchSubscribers({ withMember: true })).filter(member => !member.member.roles.cache.hasAny(...roles)).map((value, _) => value.member)
|
|
const channelMembers = newEvent.channel?.members.filter(member => !member.roles.cache.hasAny(...roles)).map((value, _) => value )
|
|
const allMembers = eventMembers.concat(channelMembers ?? [])
|
|
|
|
const members: GuildMember[] = []
|
|
for(const member of allMembers){
|
|
if(!members.find(x => x.id == member.id))
|
|
members.push(member)
|
|
}
|
|
|
|
if (newEvent.status === GuildScheduledEventStatus.Active)
|
|
createJFUsers(members, newEvent.name, requestId)
|
|
else {
|
|
const announcements = (await client.getAnnouncementChannelForGuild(newEvent.guild!.id).messages.fetch()).filter(message => !message.pinned)
|
|
announcements.forEach(message => message.delete())
|
|
members.forEach(member => {
|
|
member.createDM().then(channel => channel.send(`Die Watchparty ist vorbei, dein Account wurde wieder gelöscht. Wenn du einen permanenten Account haben möchtest, melde dich bei Samantha oder Marukus.`))
|
|
})
|
|
deleteJFUsers(newEvent.guildId, requestId)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
logger.error(error)
|
|
}
|
|
}
|
|
|
|
async function createJFUsers(members: GuildMember[], movieName: string, requestId?: string) {
|
|
logger.info(`Creating users for: \n ${JSON.stringify(members, null, 2)}`)
|
|
members.forEach(member => {
|
|
member.createDM().then(channel => channel.send(`Hey! Du hast dich für die Watchparty von ${movieName} angemeldet! Es geht gleich los!`))
|
|
jellyfinHandler.upsertUser(member, "TEMPORARY", requestId)
|
|
})
|
|
}
|
|
|
|
async function deleteJFUsers(guildId: string, requestId?: string) {
|
|
logger.info(`Watchparty ended, deleting tmp users`, { guildId, requestId })
|
|
jellyfinHandler.purge(guildId, requestId)
|
|
} |