update client

This commit is contained in:
mightypanders 2023-05-04 23:38:51 +02:00
parent 3ae97b6f66
commit c400895450

View File

@ -2,8 +2,8 @@ import { ApplicationCommandDataResolvable, Client, ClientOptions, Collection, Ga
import { CommandType } from "../types/commandTypes"; import { CommandType } from "../types/commandTypes";
import fs from 'fs' import fs from 'fs'
import { config } from "../configuration"; import { config } from "../configuration";
import { JellyfinHandler } from "../../jellyfin/handler";
import { logger } from "../logger"; import { logger } from "../logger";
import { JellyfinHandler } from "../jellyfin/handler";
export class ExtendedClient extends Client { export class ExtendedClient extends Client {
private eventFilePath = `${__dirname}/../events` private eventFilePath = `${__dirname}/../events`
@ -19,7 +19,7 @@ export class ExtendedClient extends Client {
} }
public async start() { public async start() {
if (process.env.NODE_ENV === 'test') return if (process.env.NODE_ENV === 'test') return
const promises: Promise<any>[] = Array() const promises: Promise<any>[] = []
promises.push(this.registerSlashCommands()) promises.push(this.registerSlashCommands())
promises.push(this.registerEventCallback()) promises.push(this.registerEventCallback())
Promise.all(promises).then(() => { Promise.all(promises).then(() => {
@ -28,20 +28,20 @@ export class ExtendedClient extends Client {
logger.info(`Connected with ${await this.jellyfin.ServerName()}`) logger.info(`Connected with ${await this.jellyfin.ServerName()}`)
} }
private async importFile(filepath: string): Promise<any> { private async importFile(filepath: string): Promise<any> {
console.debug(`Importing ${filepath}`) logger.debug(`Importing ${filepath}`)
const imported = await import(filepath) const imported = await import(filepath)
console.debug(`Imported ${JSON.stringify(imported)}`) logger.debug(`Imported ${JSON.stringify(imported)}`)
return imported.default ?? imported return imported.default ?? imported
} }
public async registerCommands(cmds: ApplicationCommandDataResolvable[], guildIds: Collection<Snowflake, Guild>) { public async registerCommands(cmds: ApplicationCommandDataResolvable[], guildIds: Collection<Snowflake, Guild>) {
if (guildIds) { if (guildIds) {
guildIds.forEach(guild => { guildIds.forEach(guild => {
this.guilds.cache.get(guild.id)?.commands.set(cmds) this.guilds.cache.get(guild.id)?.commands.set(cmds)
console.log(`Registering commands to ${guild.name}|${guild.id}`) logger.info(`Registering commands to ${guild.name}|${guild.id}`)
}) })
} else { } else {
this.application?.commands.set(cmds) this.application?.commands.set(cmds)
console.log(`Registering global commands`) logger.info(`Registering global commands`)
} }
return return
} }
@ -52,28 +52,27 @@ export class ExtendedClient extends Client {
for (const commandFile of commandFiles) { for (const commandFile of commandFiles) {
const filePath = `${this.commandFilePath}/${commandFile}` const filePath = `${this.commandFilePath}/${commandFile}`
const command = await this.importFile(filePath) const command = await this.importFile(filePath)
console.debug(JSON.stringify(command)) logger.debug(JSON.stringify(command))
if (!command.name) return if (!command.name) return
console.debug(command)
this.commands.set(command.name, command) this.commands.set(command.name, command)
slashCommands.push(command) slashCommands.push(command)
} }
this.on("ready", (client: Client) => { this.on("ready", (client: Client) => {
//console.log(`Ready processing ${JSON.stringify(client)}`) //logger.info(`Ready processing ${JSON.stringify(client)}`)
console.log(`SlashCommands: ${JSON.stringify(slashCommands)}`) logger.info(`SlashCommands: ${JSON.stringify(slashCommands)}`)
const guilds = client.guilds.cache const guilds = client.guilds.cache
this.registerCommands(slashCommands, guilds) this.registerCommands(slashCommands, guilds)
this.cacheUsers(guilds) this.cacheUsers(guilds)
}) })
} catch (error) { } catch (error) {
console.log(`Error refreshing slash commands: ${error}`) logger.info(`Error refreshing slash commands: ${error}`)
} }
} }
public async cacheUsers(guilds: Collection<Snowflake, Guild>) { public async cacheUsers(guilds: Collection<Snowflake, Guild>) {
guilds.forEach((guild: Guild, id: Snowflake) => { guilds.forEach((guild: Guild, id: Snowflake) => {
console.log(`Fetching members for ${guild.name}|${id}`) logger.info(`Fetching members for ${guild.name}|${id}`)
guild.members.fetch() guild.members.fetch()
console.log(`Fetched: ${guild.memberCount} members`) logger.info(`Fetched: ${guild.memberCount} members`)
}) })
} }
@ -84,17 +83,17 @@ export class ExtendedClient extends Client {
const filePath = `${this.eventFilePath}/${file}` const filePath = `${this.eventFilePath}/${file}`
const event = await this.importFile(filePath) const event = await this.importFile(filePath)
if (event.once) { if (event.once) {
console.log(`Registering once ${file}`) logger.info(`Registering once ${file}`)
this.once(event.name, (...args: any[]) => event.execute(...args)) this.once(event.name, (...args: any[]) => event.execute(...args))
} }
else { else {
console.log(`Registering on ${file}`) logger.info(`Registering on ${file}`)
this.on(event.name, (...args: any[]) => event.execute(...args)) this.on(event.name, (...args: any[]) => event.execute(...args))
} }
} }
console.log(this.eventNames()) logger.info(`Registered event names ${this.eventNames()}`)
} catch (error) { } catch (error) {
console.error(error) logger.error(error)
} }
} }
} }