42 lines
2.1 KiB
TypeScript
42 lines
2.1 KiB
TypeScript
import { GuildScheduledEvent, GuildScheduledEventStatus, Collection, Snowflake, GuildScheduledEventUser } from "discord.js";
|
|
import { logger } from "../logger";
|
|
import { jellyfinHandler } from "../.."
|
|
import { v4 as uuid } from "uuid";
|
|
import { getGuildSpecificTriggerRoleId } from "../helper/roleFilter";
|
|
|
|
|
|
export const name = 'guildScheduledEventUpdate'
|
|
|
|
export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildScheduledEvent) {
|
|
try {
|
|
logger.info(JSON.stringify(newEvent, null, 2))
|
|
const requestId = uuid()
|
|
|
|
if (newEvent.description?.includes("!WP") && [GuildScheduledEventStatus.Active, GuildScheduledEventStatus.Completed].includes(newEvent.status)) {
|
|
const roles = getGuildSpecificTriggerRoleId(newEvent.guildId).map((key, value)=> value)
|
|
const members = (await newEvent.fetchSubscribers({ withMember: true })).filter(member => !member.member.roles.cache.hasAny(...roles))
|
|
if (newEvent.status === GuildScheduledEventStatus.Active)
|
|
createJFUsers(members, newEvent.name, requestId)
|
|
else {
|
|
members.forEach(member => {
|
|
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: Collection<Snowflake, GuildScheduledEventUser<true>>, movieName: string, requestId?: string) {
|
|
logger.info(`Creating users for: \n ${JSON.stringify(members, null, 2)}`)
|
|
members.forEach(member => {
|
|
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.member, "TEMPORARY", requestId)
|
|
})
|
|
}
|
|
|
|
async function deleteJFUsers(guildId: string, requestId?: string) {
|
|
logger.info(`Watchparty ended, deleting tmp users`)
|
|
jellyfinHandler.purge(guildId, requestId)
|
|
} |