40 lines
1.9 KiB
TypeScript
40 lines
1.9 KiB
TypeScript
import { GuildScheduledEvent, GuildScheduledEventStatus, Collection, Snowflake, GuildScheduledEventUser} from "discord.js";
|
|
import { logger } from "../logger";
|
|
import { jellyfinHandler } from "../.."
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
|
|
export const name = 'guildScheduledEventUpdate'
|
|
|
|
export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildScheduledEvent) {
|
|
try {
|
|
logger.info(JSON.stringify(newEvent, null, 2))
|
|
|
|
if (newEvent.description?.includes("!WP") && [GuildScheduledEventStatus.Active, GuildScheduledEventStatus.Completed].includes(newEvent.status)) {
|
|
const members = await newEvent.fetchSubscribers({withMember: true})
|
|
newEvent.guild?.members.fetch
|
|
if(newEvent.status === GuildScheduledEventStatus.Active)
|
|
createJFUsers(members, newEvent.name)
|
|
else
|
|
deleteJFUsers(members)
|
|
}
|
|
} catch(error) {
|
|
logger.error(error)
|
|
}
|
|
}
|
|
|
|
async function createJFUsers(members: Collection<Snowflake, GuildScheduledEventUser<true>>, movieName: 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, "wird eh nicht genutzt", uuid())
|
|
})
|
|
}
|
|
|
|
async function deleteJFUsers(members: Collection<Snowflake, GuildScheduledEventUser<true>>) {
|
|
logger.info(`Deleting users for: \n ${JSON.stringify(members, null, 2)}`)
|
|
members.forEach(member => {
|
|
member.member.createDM().then(channel => channel.send(`Hey! Da die Watchparty vorbei ist, hab ich deinen Account wieder entfernt. Falls du einen permanenten Account haben möchtest frag bitte Samantha oder Marukus :)`))
|
|
jellyfinHandler.removeUser(member.member, uuid())
|
|
})
|
|
} |