Compare commits

..

No commits in common. "64c58742492e42743795df1898b1adba7edb9f07" and "d3fff38e369ff35a4d96b5b03b7cecbd5515f84f" have entirely different histories.

7 changed files with 31 additions and 144 deletions

View File

@ -1,5 +1,5 @@
import { Collection, GuildMember } from "discord.js"
import { filterRolesFromMemberUpdate, getGuildSpecificTriggerRoleId } from "../helper/roleFilter"
import { filterRolesFromMemberUpdate } from "../helper/roleFilter"
import { ChangedRoles, PermissionLevel } from "../interfaces"
import { jellyfinHandler } from "../.."
import { v4 as uuid } from "uuid"
@ -18,7 +18,7 @@ export async function execute(oldMember: GuildMember, newMember: GuildMember) {
}
const removedRoleMatches = changedRoles.removedRoles.find(rRole => rRole.id === key)
if (removedRoleMatches) {
jellyfinHandler.removeUser(newMember, level, requestId)
jellyfinHandler.removeUser(newMember, requestId)
}
})
} catch (error) {
@ -26,5 +26,10 @@ export async function execute(oldMember: GuildMember, newMember: GuildMember) {
}
}
function getGuildSpecificTriggerRoleId(id: string): Collection<string, PermissionLevel> {
const outVal = new Collection<string, PermissionLevel>()
outVal.set('1096819983889215659', "VIEWER")
outVal.set('1097990848613986526', "ADMIN")
return outVal
}

View File

@ -1,42 +0,0 @@
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)
}

View File

@ -1,49 +0,0 @@
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, "TEMPORARY", uuid())
} else {
logger.error("WTF? Expected Member?? When doing things")
}
}
}catch(error){
logger.error(error)
}
}

View File

@ -1,5 +1,5 @@
import { Collection, GuildMember } from "discord.js"
import { ChangedRoles, PermissionLevel } from "../interfaces"
import { GuildMember } from "discord.js"
import { ChangedRoles } from "../interfaces"
import { logger } from "../logger"
export function filterRolesFromMemberUpdate(oldMember: GuildMember, newMember: GuildMember): ChangedRoles {
@ -14,10 +14,3 @@ export function filterRolesFromMemberUpdate(oldMember: GuildMember, newMember: G
return { addedRoles, removedRoles }
}
export function getGuildSpecificTriggerRoleId(guildId: string): Collection<string, PermissionLevel> {
const outVal = new Collection<string, PermissionLevel>()
outVal.set('1096819983889215659', "VIEWER")
outVal.set('1097990848613986526', "ADMIN")
return outVal
}

View File

@ -32,4 +32,4 @@ export interface ChangedRoles {
addedRoles: Collection<string, Role>
removedRoles: Collection<string, Role>
}
export type PermissionLevel = "VIEWER" | "ADMIN" | "TEMPORARY"
export type PermissionLevel = "VIEWER" | "ADMIN"

View File

@ -1,11 +1,11 @@
import { GuildMember } from "discord.js";
import { CreateUserByNameOperationRequest, DeleteUserRequest, SystemApi, UpdateUserPasswordOperationRequest, UpdateUserPolicyOperationRequest, UserApi } from "./apis";
import { CreateUserByNameOperationRequest, SystemApi, UpdateUserPasswordOperationRequest, UpdateUserPolicyOperationRequest, UserApi } from "./apis";
import { UserDto } from "./models/UserDto";
import { Configuration, ConfigurationParameters } from "./runtime";
import { CreateUserByNameRequest, UpdateUserEasyPasswordRequest, UpdateUserPasswordRequest, UpdateUserPolicyRequest } from "./models";
import { Config } from "../configuration";
import { logger } from "../logger";
import { Maybe, PermissionLevel } from "../interfaces";
import { Maybe } from "../interfaces";
import { v4 as uuid } from "uuid";
@ -46,8 +46,8 @@ export class JellyfinHandler {
logger.info(`Initialized Jellyfin handler`, { requestId: 'Init' })
}
private generateJFUserName(discordUser: GuildMember, level: PermissionLevel): string {
return `${discordUser.displayName}${level == "TEMPORARY" ? "_tmp" : ""}`
private generateJFUserName(discordUser: GuildMember): string {
return discordUser.displayName
}
public async addPermissionsToUserAccount(jfUserAccount: UserDto, guildId: string, requestId: string): Promise<UserDto> {
@ -58,8 +58,8 @@ export class JellyfinHandler {
return (Math.random() * 10000 + 10000).toFixed(0)
}
public async createUserAccountForDiscordUser(discordUser: GuildMember, level: PermissionLevel, guildId?: string, requestId?: string): Promise<UserDto> {
const newUserName = this.generateJFUserName(discordUser, level)
public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId?: string, requestId?: string): Promise<UserDto> {
const newUserName = this.generateJFUserName(discordUser)
logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId })
const req: CreateUserByNameOperationRequest = {
createUserByNameRequest: {
@ -95,40 +95,20 @@ export class JellyfinHandler {
public async getUser(discordUser: GuildMember, requestId?: string): Promise<Maybe<UserDto>> {
logger.info(`Getting user for discord member ${discordUser.displayName}`, {requestId})
const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser)
const jfUsers = await this.getCurrentUsers(discordUser.guild.id, requestId)
const foundUser = jfUsers.find(x => x.name?.includes(discordUser.displayName))
const foundUser = jfUsers.find(x => x.name === jfUsernameFromDiscordUsername)
return foundUser
}
public async removeUser(newMember: GuildMember, level: PermissionLevel, requestId?: string) {
logger.info(`${level == "TEMPORARY" ? "Deleting" : "Disabling" } user ${newMember.displayName}, but method is not implemented`, { requestId })
public async removeUser(newMember: GuildMember, requestId?: string) {
logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`, {requestId})
const jfuser = await this.getUser(newMember, requestId)
if (jfuser && jfuser.id) {
if (level === "TEMPORARY") {
const r: DeleteUserRequest = {
userId: jfuser.id
}
this.userApi.deleteUser(r)
}
else
if (jfuser) {
await this.disableUser(jfuser, newMember.guild.id, requestId)
}
}
public async purge(guildId: string, requestId?: string) {
logger.info("Deleting tmp users")
const users = (await this.userApi.getUsers()).filter(user => user.name?.endsWith("_tmp"))
users.forEach(user => {
if(user.id) {
const r: DeleteUserRequest = {
userId: user.id
}
this.userApi.deleteUser(r)
}
})
}
public async resetUserPasswort(member: GuildMember, requestId?: string) {
logger.info(`Resetting password for user ${member.displayName}`, {requestId})
const jfUser = await this.getUser(member, requestId)
@ -210,14 +190,14 @@ export class JellyfinHandler {
}
}
public async upsertUser(newMember: GuildMember, level: PermissionLevel, requestId?: string) {
public async upsertUser(newMember: GuildMember, level: string, requestId?: string) {
logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level}`)
const jfuser = await this.getUser(newMember, requestId)
if (jfuser) {
logger.info(`User with name ${newMember.displayName} is already present`)
await this.enableUser(jfuser, newMember.guild.id, requestId)
} else {
this.createUserAccountForDiscordUser(newMember, level, newMember.guild.id, requestId)
this.createUserAccountForDiscordUser(newMember, newMember.guild.id, requestId)
}
}
}

View File

@ -12,7 +12,7 @@ export class ExtendedClient extends Client {
public commands: Collection<string, CommandType> = 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, IntentsBitField.Flags.GuildScheduledEvents, IntentsBitField.Flags.GuildVoiceStates)
intents.add(IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.DirectMessages)
const options: ClientOptions = { intents }
super(options)
this.jellyfin = jf