Automatically create and disable users based on watch event

This commit is contained in:
Sammy 2023-06-04 15:02:03 +02:00
parent d3fff38e36
commit 892562cd0b
3 changed files with 90 additions and 1 deletions

View File

@ -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<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())
})
}

View File

@ -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)
}
}

View File

@ -12,7 +12,7 @@ export class ExtendedClient extends Client {
public commands: Collection<string, CommandType> = new Collection() public commands: Collection<string, CommandType> = new Collection()
public constructor(jf: JellyfinHandler) { public constructor(jf: JellyfinHandler) {
const intents: IntentsBitField = new IntentsBitField() 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 } const options: ClientOptions = { intents }
super(options) super(options)
this.jellyfin = jf this.jellyfin = jf