use event handling code
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
mightypanders 2022-03-05 01:24:22 +01:00
parent 03d9b43c44
commit a60f35459c
1 changed files with 32 additions and 17 deletions

View File

@ -1,22 +1,22 @@
import { REST } from '@discordjs/rest' import { REST } from '@discordjs/rest'
import { Routes } from 'discord-api-types/v9' import { Routes } from 'discord-api-types/v9'
import { config } from './configuration' import { config } from './configuration'
import { Client, Collection, CommandInteraction, GuildMember, GuildScheduledEvent, GuildScheduledEventManager, Intents, Snowflake } from 'discord.js' import { Client, CommandInteraction, Intents, } from 'discord.js'
import RegistrationHandler from './RegistrationHandler'
import { discordCommand } from './interfaces' import { discordCommand } from './interfaces'
import eventHandler from './eventHandler' import eventHandler from './eventHandler'
import fs from 'fs'
export default class DiscordAdapter { export default class DiscordAdapter {
private rest: REST private rest: REST
private client: Client private client: Client
private registration: RegistrationHandler private eventFilePath = `${__dirname}/events`
public constructor() { public constructor() {
this.rest = new REST({ version: '9' }).setToken(config.token) this.rest = new REST({ version: '9' }).setToken(config.token)
this.client = new Client({ intents: [Intents.FLAGS.GUILDS] }) this.client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_SCHEDULED_EVENTS] })
this.registration = RegistrationHandler.Instance this.registerEventCallback().then(() => {
this.setupCallbacks(this.client)
this.client.login(config.token) this.client.login(config.token)
})
this.registerCommands(this.commandList) this.registerCommands(this.commandList)
} }
public async registerCommands(pCommands: discordCommand[]) { public async registerCommands(pCommands: discordCommand[]) {
@ -28,16 +28,31 @@ export default class DiscordAdapter {
console.log(`Error refreshing slash commands: ${error}`) console.log(`Error refreshing slash commands: ${error}`)
} }
} }
private setupCallbacks(c: Client) { private async importFile(filepath: string): Promise<any> {
c.on('ready', () => { console.debug(`Importing ${filepath}`)
console.log(`Logged in as ${c.user?.tag}`) const imported = await import(filepath)
}) console.debug(`Imported ${JSON.stringify(imported)}`)
c.on('interactionCreate', async interaction => { return imported
if (!interaction.isCommand()) return }
const interactionCommand = this.commandList.find(x => x.name == interaction.commandName) public async registerEventCallback() {
if (interactionCommand) try {
interactionCommand.performCommand(interaction, this.registration) 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.client.once(event.name, (...args: any[]) => event.execute(...args))
}
else {
console.log(`Registering on ${file}`)
this.client.on(event.name, (...args: any[]) => event.execute(...args))
}
}
console.log(this.client.eventNames())
} catch (error) {
console.error(error)
}
} }
public async showNext(interaction: CommandInteraction): Promise<void> { public async showNext(interaction: CommandInteraction): Promise<void> {
const guild = interaction.guild const guild = interaction.guild