big update of structure
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
76
server/structures/client.ts
Normal file
76
server/structures/client.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import { ApplicationCommandDataResolvable, Client, Collection, Intents } from "discord.js";
|
||||
import { CommandType } from "../types/commandTypes";
|
||||
import fs from 'fs'
|
||||
import { config } from "../configuration";
|
||||
|
||||
export class ExtendedClient extends Client {
|
||||
private eventFilePath = `${__dirname}/../events`
|
||||
private commandFilePath = `${__dirname}/../commands`
|
||||
private commands: Collection<string, CommandType> = new Collection()
|
||||
public constructor() {
|
||||
super({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_SCHEDULED_EVENTS] })
|
||||
}
|
||||
public start() {
|
||||
const promises = []
|
||||
promises.push(this.registerSlashCommands())
|
||||
promises.push(this.registerEventCallback())
|
||||
Promise.all(promises).then(() => {
|
||||
this.login(config.token)
|
||||
})
|
||||
}
|
||||
private async importFile(filepath: string): Promise<any> {
|
||||
console.debug(`Importing ${filepath}`)
|
||||
const imported = await import(filepath)
|
||||
console.debug(`Imported ${JSON.stringify(imported)}`)
|
||||
return imported
|
||||
}
|
||||
public async registerCommands(cmds: ApplicationCommandDataResolvable[], guildId: string) {
|
||||
if (guildId) {
|
||||
this.guilds.cache.get(guildId)?.commands.set(cmds)
|
||||
console.log(`Registering commands to ${guildId}`)
|
||||
} else {
|
||||
this.application?.commands.set(cmds)
|
||||
console.log(`Registering global commands`)
|
||||
}
|
||||
return
|
||||
}
|
||||
public async registerSlashCommands() {
|
||||
try {
|
||||
const slashCommands: ApplicationCommandDataResolvable[] = []
|
||||
const commandFiles = fs.readdirSync(this.commandFilePath).filter(file => file.endsWith('.ts') || file.endsWith('.js'))
|
||||
for (const commandFile of commandFiles) {
|
||||
const command = await this.importFile(commandFile)
|
||||
if (!command.name) return
|
||||
console.debug(command)
|
||||
this.commands.set(command.name, command)
|
||||
slashCommands.push(command)
|
||||
}
|
||||
this.on("ready", (client: Client) => {
|
||||
console.log(`Ready processed ${client}`)
|
||||
this.registerCommands(slashCommands, process.env.guildId ?? "")
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(`Error refreshing slash commands: ${error}`)
|
||||
}
|
||||
}
|
||||
public async registerEventCallback() {
|
||||
try {
|
||||
const eventFiles = fs.readdirSync(this.eventFilePath).filter(file => file.endsWith('.ts') || file.endsWith('.js'));
|
||||
for (const file of eventFiles) {
|
||||
const filePath = `${this.eventFilePath}/${file}`
|
||||
const event = await this.importFile(filePath)
|
||||
if (event.once) {
|
||||
console.log(`Registering once ${file}`)
|
||||
this.once(event.name, (...args: any[]) => event.execute(...args))
|
||||
}
|
||||
else {
|
||||
console.log(`Registering on ${file}`)
|
||||
this.on(event.name, (...args: any[]) => event.execute(...args))
|
||||
}
|
||||
}
|
||||
console.log(this.eventNames())
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
}
|
7
server/structures/command.ts
Normal file
7
server/structures/command.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { CommandType } from "../types/commandTypes";
|
||||
|
||||
export class Command {
|
||||
constructor(commandOptions: CommandType) {
|
||||
Object.assign(this, commandOptions)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user