diff --git a/server/events/guildScheduledEventUpdate.ts b/server/events/guildScheduledEventUpdate.ts new file mode 100644 index 0000000..84c51e2 --- /dev/null +++ b/server/events/guildScheduledEventUpdate.ts @@ -0,0 +1,40 @@ +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>, 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>) { + 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()) + }) +} \ No newline at end of file diff --git a/server/events/voiceStateUpdate.ts b/server/events/voiceStateUpdate.ts new file mode 100644 index 0000000..5a043cd --- /dev/null +++ b/server/events/voiceStateUpdate.ts @@ -0,0 +1,49 @@ +import { GuildScheduledEvent, GuildScheduledEventStatus, Collection, Snowflake, GuildScheduledEventUser, User, VoiceState } from "discord.js"; +import { logger } from "../logger"; +import { jellyfinHandler } from "../.." +import { v4 as uuid } from "uuid"; + + +export const name = 'voiceStateUpdate' + +export async function execute(oldState: VoiceState, newState: VoiceState) { + + try { + //ignore events like mute/unmute + if(newState.channel?.id === oldState.channel?.id) { + return + } + + const scheduledEvents = (await newState.guild.scheduledEvents.fetch()) + .filter((key, value) => key.description?.includes("!WP") && key.isActive()) + .map((key, value) => key) + + const scheduledEventUsers = (await Promise.all(scheduledEvents.map(event => event.fetchSubscribers({withMember: true})))) + + //Dont handle users, that are already subscribed to the event. We only want to handle unsubscribed users here + let userFound = false; + scheduledEventUsers.forEach(collection => { + collection.each(key => { + logger.info(JSON.stringify(key, null, 2)) + if(key.member.user.id === newState.member?.user.id) + userFound = true; + }) + }) + if(userFound) + return + + + if (scheduledEvents.find(event => event.channelId === newState.channelId)) { + if(newState.member){ + logger.info("YO! Da ist jemand dem Channel mit dem Event beigetreten, ich kümmer mich mal um nen Account!") + + newState.member.createDM().then(channel => channel.send(`Hey! Du bist unserer Watchparty beigetreten, ich leg dir mal nen Account an, damit du mitschauen kannst!`)) + jellyfinHandler.upsertUser(newState.member, "wird eh nicht genutzt", uuid()) + } else { + logger.error("WTF? Expected Member?? When doing things") + } + } + }catch(error){ + logger.error(error) + } +} \ No newline at end of file diff --git a/server/structures/client.ts b/server/structures/client.ts index 921b495..06e1f50 100644 --- a/server/structures/client.ts +++ b/server/structures/client.ts @@ -12,7 +12,7 @@ export class ExtendedClient extends Client { public commands: Collection = new Collection() public constructor(jf: JellyfinHandler) { const intents: IntentsBitField = new IntentsBitField() - intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages) + intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages, IntentsBitField.Flags.GuildScheduledEvents, IntentsBitField.Flags.GuildVoiceStates) const options: ClientOptions = { intents } super(options) this.jellyfin = jf