From b01700df6c31ae67d8d465bfa13f24c461c06e88 Mon Sep 17 00:00:00 2001 From: mightypanders Date: Tue, 18 Apr 2023 23:46:26 +0200 Subject: [PATCH] WIP: use guildmember in jf handler, start user creation --- jellyfin/handler.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/jellyfin/handler.ts b/jellyfin/handler.ts index 0a9e32f..caa4c2b 100644 --- a/jellyfin/handler.ts +++ b/jellyfin/handler.ts @@ -1,4 +1,4 @@ -import { User } from "discord.js"; +import { GuildMember, User } from "discord.js"; import { UserDto } from "./model/userDto"; import { Config } from "../server/configuration"; import { UserApi } from "./api/userApi"; @@ -12,7 +12,7 @@ export class JellyfinHandler { private token: string private authHeader: { headers: { 'X-MediaBrowser-Token': string } } private config: Config - private serverName: string = ""; + private serverName = ""; public async ServerName(): Promise { if (this.serverName === "") { @@ -33,26 +33,28 @@ export class JellyfinHandler { } } - private generateJFUserName(discordUser: User): string { - return discordUser.username + discordUser.discriminator + private generateJFUserName(discordUser: GuildMember): string { + return discordUser.displayName } public async addPermissionsToUserAccount(jfUserAccount: UserDto, guildId: string, requestId: string): Promise { throw new Error("Method not implemented."); } - public async createUserAccountForDiscordUser(discordUser: User, guildId: string, requestId: string): Promise { + public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId: string, requestId: string): Promise { const newUserName = this.generateJFUserName(discordUser) - logger.info(`New Username for ${discordUser.username}: ${newUserName}`, { guildId, requestId }) + logger.info(`New Username for ${discordUser.displayName}: ${newUserName}`, { guildId, requestId }) throw new Error("Method not implemented."); } - public async isUserAlreadyPresent(discordUser: User, guildId: string, requestId: string): Promise { + public async isUserAlreadyPresent(discordUser: GuildMember, guildId: string, requestId?: string): Promise { const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser) const jfUsers = await this.getCurrentUsers(guildId, requestId) const currentJfUserNames = jfUsers.map(x => x.name) + logger.debug(`Current JF Usernames: ${JSON.stringify(currentJfUserNames)}`) const presence = currentJfUserNames.find(x => x === jfUsernameFromDiscordUsername) + logger.debug(`Presence for DiscordUser ${discordUser.id}:${presence}`) return presence !== undefined } - public async getCurrentUsers(guildId: string, requestId: string): Promise { + public async getCurrentUsers(guildId: string, requestId?: string): Promise { try { const result = await this.userApi.getUsers(undefined, undefined, this.authHeader) return result.body @@ -61,4 +63,16 @@ export class JellyfinHandler { } return [] } + + public async removeUser(newMember: GuildMember) { + logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`) + } + public async upsertUser(newMember: GuildMember, level: string, requestId?: string) { + logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level} but method is not implemented`) + const presence = await this.isUserAlreadyPresent(newMember, newMember.guild.id, requestId) + if (presence) { + logger.info(`User with name ${newMember.displayName} is already present`) + } + + } }