From efd4d69c748b8cf49d69c14a23e79556c1e4bb1f Mon Sep 17 00:00:00 2001 From: mightypanders Date: Tue, 6 Jun 2023 23:27:07 +0200 Subject: [PATCH 1/2] add ids to logging --- server/jellyfin/handler.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/server/jellyfin/handler.ts b/server/jellyfin/handler.ts index 2c6bdbc..1617a44 100644 --- a/server/jellyfin/handler.ts +++ b/server/jellyfin/handler.ts @@ -67,7 +67,7 @@ export class JellyfinHandler { password: this.generatePasswordForUser(), } } - logger.debug(JSON.stringify(req)) + logger.debug(JSON.stringify(req), { requestId, guildId }) const createResult = await this.userApi.createUserByName(req) if (createResult) { (await discordUser.createDM()).send(`Ich hab dir mal nen Account angelegt :)\nDein Username ist ${createResult.name}, dein Password ist "${req.createUserByNameRequest.password}"!`) @@ -78,7 +78,7 @@ export class JellyfinHandler { public async isUserAlreadyPresent(discordUser: GuildMember, requestId?: string): Promise { const jfuser = await this.getUser(discordUser, requestId) - logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`) + logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`, { guildId: discordUser.guild.id, requestId }) return jfuser !== undefined } @@ -94,14 +94,14 @@ export class JellyfinHandler { } public async getUser(discordUser: GuildMember, requestId?: string): Promise> { - logger.info(`Getting user for discord member ${discordUser.displayName}`, { requestId }) + logger.info(`Getting user for discord member ${discordUser.displayName}`, { requestId, guildId: discordUser.guild.id }) const jfUsers = await this.getCurrentUsers(discordUser.guild.id, requestId) const foundUser = jfUsers.find(x => x.name?.includes(discordUser.displayName)) 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 }) + logger.info(`${level == "TEMPORARY" ? "Deleting" : "Disabling"} user ${newMember.displayName}, but method is not implemented`, { requestId, guildId: newMember.guild.id }) const jfuser = await this.getUser(newMember, requestId) if (jfuser && jfuser.id) { if (level === "TEMPORARY") { @@ -116,11 +116,11 @@ export class JellyfinHandler { } public async purge(guildId: string, requestId?: string) { - logger.info("Deleting tmp users") + logger.info("Deleting tmp users", { requestId, guildId }) const users = (await this.userApi.getUsers()).filter(user => user.name?.endsWith("_tmp")) users.forEach(user => { - if(user.id) { + if (user.id) { const r: DeleteUserRequest = { userId: user.id } @@ -130,7 +130,7 @@ export class JellyfinHandler { } public async resetUserPasswort(member: GuildMember, requestId?: string) { - logger.info(`Resetting password for user ${member.displayName}`, { requestId }) + logger.info(`Resetting password for user ${member.displayName}`, { requestId, guildId: member.guild.id }) const jfUser = await this.getUser(member, requestId) if (jfUser && jfUser.id) { @@ -160,7 +160,7 @@ export class JellyfinHandler { userId: jfUser.id } - logger.info("Setting new password", { requestId }) + logger.info("Setting new password", { requestId, guildId: member.guild.id }) await this.userApi.updateUserPassword(passwordOperationRequest); @@ -175,7 +175,7 @@ export class JellyfinHandler { public async disableUser(user: UserDto, guildId?: string, requestId?: string): Promise { if (user.id) { const jfUser = await this.getUser({ displayName: user.name, guild: { id: guildId } }, requestId) - logger.info(`Trying to disable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`) + logger.info(`Trying to disable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`, { guildId, requestId }) const r: UpdateUserPolicyOperationRequest = { userId: user.id ?? "", updateUserPolicyRequest: { @@ -184,7 +184,7 @@ export class JellyfinHandler { } } await this.userApi.updateUserPolicy(r) - logger.info(`Succeeded with disabling user: ${user.name}`) + logger.info(`Succeeded with disabling user: ${user.name}`, { guildId, requestId }) } else { logger.error(`Can not disable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId }) @@ -194,7 +194,7 @@ export class JellyfinHandler { public async enableUser(user: UserDto, guildId: string, requestId?: string): Promise { if (user.id) { const jfUser = await this.getUser({ displayName: user.name, guild: { id: guildId } }, requestId) - logger.info(`Trying to enable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`) + logger.info(`Trying to enable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`, { guildId, requestId }) const r: UpdateUserPolicyOperationRequest = { userId: user.id ?? "", updateUserPolicyRequest: { @@ -203,7 +203,7 @@ export class JellyfinHandler { } } await this.userApi.updateUserPolicy(r) - logger.info(`Succeeded with enabling user: ${user.name}`) + logger.info(`Succeeded with enabling user: ${user.name}`, { guildId, requestId }) } else { logger.error(`Can not enable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId }) @@ -211,10 +211,10 @@ export class JellyfinHandler { } public async upsertUser(newMember: GuildMember, level: PermissionLevel, requestId?: string) { - logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level}`) + logger.error(`Trying to upsert user ${newMember.displayName}, with permissionLevel ${level}`, { guildId: newMember.guild.id, requestId }) const jfuser = await this.getUser(newMember, requestId) if (jfuser) { - logger.info(`User with name ${newMember.displayName} is already present`) + logger.info(`User with name ${newMember.displayName} is already present`, { guildId: newMember.guild.id, requestId }) await this.enableUser(jfuser, newMember.guild.id, requestId) } else { this.createUserAccountForDiscordUser(newMember, level, newMember.guild.id, requestId) From 84a0d7bbe1182441ee605820af4b83f4946e840e Mon Sep 17 00:00:00 2001 From: mightypanders Date: Tue, 6 Jun 2023 23:27:41 +0200 Subject: [PATCH 2/2] turn all slash command replies into an ephemeral message Setting ephemeral has to happen in an initial reply (reply() or deferReply()) to set the mode for all replies in this 'thread'. Once the ephemeral state of a message has been set it can not be changed. If you want to send a visible message after an ephemeral message it has to be new message. See https://discordjs.guide/slash-commands/response-methods.html#ephemeral-responses --- server/events/interactionCreate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/events/interactionCreate.ts b/server/events/interactionCreate.ts index d15d07a..c483f8f 100644 --- a/server/events/interactionCreate.ts +++ b/server/events/interactionCreate.ts @@ -8,7 +8,7 @@ export async function execute(interaction: ExtendedInteraction) { //console.dir(interaction, { depth: null }) if (interaction.isCommand()) { logger.info(`Interaction is a command.`, { guildId: interaction.guild?.id }) - await interaction.deferReply() + await interaction.deferReply({ ephemeral: true }) const command = client.commands.get(interaction.commandName) if (!command) return interaction.followUp('Invalid command')