Compare commits
No commits in common. "7e37641e0b98200ed2e37d7e56ab78d7684b690f" and "2a03cd75890e815db52216c962972ff7fe412b29" have entirely different histories.
7e37641e0b
...
2a03cd7589
@ -2,23 +2,22 @@ import { Collection, GuildMember } from "discord.js"
|
||||
import { filterRolesFromMemberUpdate } from "../helper/roleFilter"
|
||||
import { ChangedRoles, PermissionLevel } from "../interfaces"
|
||||
import { jellyfinHandler } from "../.."
|
||||
import { v4 as uuid } from "uuid"
|
||||
|
||||
export const name = 'guildMemberUpdate'
|
||||
export async function execute(oldMember: GuildMember, newMember: GuildMember) {
|
||||
try {
|
||||
const requestId = uuid()
|
||||
|
||||
const changedRoles: ChangedRoles = filterRolesFromMemberUpdate(oldMember, newMember)
|
||||
const triggerRoleIds: Collection<string, PermissionLevel> = getGuildSpecificTriggerRoleId(oldMember.guild.id)
|
||||
|
||||
triggerRoleIds.forEach((level, key) => {
|
||||
const addedRoleMatches = changedRoles.addedRoles.find(aRole => aRole.id === key)
|
||||
if (addedRoleMatches) {
|
||||
jellyfinHandler.upsertUser(newMember, level, requestId)
|
||||
jellyfinHandler.upsertUser(newMember, level)
|
||||
}
|
||||
const removedRoleMatches = changedRoles.removedRoles.find(rRole => rRole.id === key)
|
||||
if (removedRoleMatches) {
|
||||
jellyfinHandler.removeUser(newMember, requestId)
|
||||
jellyfinHandler.removeUser(newMember)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
|
@ -1,13 +1,11 @@
|
||||
import { GuildMember } from "discord.js";
|
||||
import { CreateUserByNameOperationRequest, SystemApi, UpdateUserPasswordOperationRequest, UpdateUserPolicyOperationRequest, UserApi } from "./apis";
|
||||
import { CreateUserByNameOperationRequest, SystemApi, UpdateUserPolicyOperationRequest, UserApi } from "./apis";
|
||||
import { UserDto } from "./models/UserDto";
|
||||
import { Configuration, ConfigurationParameters } from "./runtime";
|
||||
import { CreateUserByNameRequest, UpdateUserEasyPasswordRequest, UpdateUserPasswordRequest, UpdateUserPolicyRequest } from "./models";
|
||||
import { CreateUserByNameRequest, UpdateUserPolicyRequest } from "./models";
|
||||
import { Config } from "../configuration";
|
||||
import { logger } from "../logger";
|
||||
import { Maybe } from "../interfaces";
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
|
||||
export class JellyfinHandler {
|
||||
|
||||
@ -54,8 +52,8 @@ export class JellyfinHandler {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
private generatePasswordForUser(): string {
|
||||
return (Math.random() * 10000 + 10000).toFixed(0)
|
||||
private generatePasswordForUser(user: GuildMember): string {
|
||||
return user.displayName + user.user.discriminator
|
||||
}
|
||||
|
||||
public async createUserAccountForDiscordUser(discordUser: GuildMember, guildId?: string, requestId?: string): Promise<UserDto> {
|
||||
@ -64,24 +62,39 @@ export class JellyfinHandler {
|
||||
const req: CreateUserByNameOperationRequest = {
|
||||
createUserByNameRequest: {
|
||||
name: newUserName,
|
||||
password: this.generatePasswordForUser(),
|
||||
password: this.generatePasswordForUser(discordUser),
|
||||
}
|
||||
}
|
||||
logger.debug(JSON.stringify(req))
|
||||
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}"!` )
|
||||
if (createResult)
|
||||
return createResult
|
||||
}
|
||||
else throw new Error('Could not create User in Jellyfin')
|
||||
}
|
||||
|
||||
|
||||
public async disableJfUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
|
||||
if (user.id) {
|
||||
logger.info(`Trying to disable user: ${user.name}|${user.id}`)
|
||||
const r: UpdateUserPolicyOperationRequest = {
|
||||
userId: user.id,
|
||||
updateUserPolicyRequest: {
|
||||
isDisabled: true
|
||||
},
|
||||
}
|
||||
await this.userApi.updateUserPolicy(r)
|
||||
logger.info(`Succeeded with disabling user: ${user.name}`)
|
||||
}
|
||||
else {
|
||||
logger.error(`Can not disable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId })
|
||||
}
|
||||
}
|
||||
|
||||
public async isUserAlreadyPresent(discordUser: GuildMember, requestId?: string): Promise<boolean> {
|
||||
const jfuser = await this.getUser(discordUser, requestId)
|
||||
logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`)
|
||||
return jfuser !== undefined
|
||||
}
|
||||
|
||||
|
||||
public async getCurrentUsers(guildId: string, requestId?: string): Promise<UserDto[]> {
|
||||
try {
|
||||
logger.info(`Fetching current users from Jellyfin`, { requestId, guildId })
|
||||
@ -92,72 +105,28 @@ export class JellyfinHandler {
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
|
||||
public async getUser(discordUser: GuildMember, requestId?: string): Promise<Maybe<UserDto>> {
|
||||
const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser)
|
||||
const jfUsers = await this.getCurrentUsers(discordUser.guild.id, requestId)
|
||||
const foundUser = jfUsers.find(x => x.name === jfUsernameFromDiscordUsername)
|
||||
return foundUser
|
||||
}
|
||||
|
||||
|
||||
public async removeUser(newMember: GuildMember, requestId?: string) {
|
||||
logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`)
|
||||
const jfuser = await this.getUser(newMember, requestId)
|
||||
if (jfuser) {
|
||||
await this.disableUser(jfuser, newMember.guild.id, requestId)
|
||||
await this.disableJfUser(jfuser, newMember.guild.id, requestId)
|
||||
}
|
||||
}
|
||||
|
||||
public async resetUserPasswort(member: GuildMember, requestId?: string) {
|
||||
const jfUser = await this.getUser(member, requestId)
|
||||
if (jfUser && jfUser.id) {
|
||||
|
||||
const r: UpdateUserPasswordRequest = {
|
||||
newPw: this.generatePasswordForUser()
|
||||
}
|
||||
|
||||
const shit: UpdateUserPasswordOperationRequest = {
|
||||
updateUserPasswordRequest: r,
|
||||
userId: jfUser.id
|
||||
}
|
||||
|
||||
this.userApi.updateUserPassword(shit)
|
||||
|
||||
} else {
|
||||
(await member.createDM()).send("Ich konnte leider keinen User von dir auf Jellyfin finden. Bitte melde dich bei Markus oder Samantha!")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async disableUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
|
||||
public async enableUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
|
||||
if (user.id) {
|
||||
const jfUser = await this.getUser(<GuildMember>{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 enable user: ${user.name}|${user.id}`)
|
||||
const r: UpdateUserPolicyOperationRequest = {
|
||||
userId: user.id ?? "",
|
||||
updateUserPolicyRequest: {
|
||||
...jfUser?.policy,
|
||||
isDisabled: true,
|
||||
}
|
||||
}
|
||||
await this.userApi.updateUserPolicy(r)
|
||||
logger.info(`Succeeded with disabling user: ${user.name}`)
|
||||
}
|
||||
else {
|
||||
logger.error(`Can not disable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId })
|
||||
}
|
||||
}
|
||||
|
||||
public async enableUser(user: UserDto, guildId: string, requestId?: string): Promise<void> {
|
||||
if (user.id) {
|
||||
const jfUser = await this.getUser(<GuildMember>{displayName: user.name, guild: {id: guildId}}, requestId)
|
||||
logger.info(`Trying to enable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`)
|
||||
const r: UpdateUserPolicyOperationRequest = {
|
||||
userId: user.id ?? "",
|
||||
updateUserPolicyRequest: {
|
||||
...jfUser?.policy,
|
||||
isDisabled: false,
|
||||
}
|
||||
}
|
||||
await this.userApi.updateUserPolicy(r)
|
||||
@ -173,10 +142,13 @@ export class JellyfinHandler {
|
||||
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)
|
||||
await this.enableUser(jfuser, requestId)
|
||||
} else {
|
||||
this.createUserAccountForDiscordUser(newMember, newMember.guild.id, requestId)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user