2022-01-03 15:07:49 +01:00
|
|
|
import { REST } from '@discordjs/rest'
|
|
|
|
import { Routes } from 'discord-api-types/v9'
|
|
|
|
import { config } from './configuration'
|
2022-03-05 01:24:22 +01:00
|
|
|
import { Client, CommandInteraction, Intents, } from 'discord.js'
|
2022-01-03 15:07:49 +01:00
|
|
|
import { discordCommand } from './interfaces'
|
2022-01-19 22:19:06 +01:00
|
|
|
import eventHandler from './eventHandler'
|
2022-03-05 01:24:22 +01:00
|
|
|
import fs from 'fs'
|
2022-01-03 15:07:49 +01:00
|
|
|
|
|
|
|
export default class DiscordAdapter {
|
|
|
|
private rest: REST
|
|
|
|
private client: Client
|
2022-03-05 01:24:22 +01:00
|
|
|
private eventFilePath = `${__dirname}/events`
|
2022-01-19 22:19:06 +01:00
|
|
|
|
2022-01-03 15:07:49 +01:00
|
|
|
public constructor() {
|
|
|
|
this.rest = new REST({ version: '9' }).setToken(config.token)
|
2022-03-05 01:24:22 +01:00
|
|
|
this.client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_SCHEDULED_EVENTS] })
|
|
|
|
this.registerEventCallback().then(() => {
|
|
|
|
this.client.login(config.token)
|
|
|
|
})
|
2022-01-03 15:07:49 +01:00
|
|
|
this.registerCommands(this.commandList)
|
|
|
|
}
|
|
|
|
public async registerCommands(pCommands: discordCommand[]) {
|
|
|
|
try {
|
|
|
|
console.log('Refreshing slash commands')
|
|
|
|
await this.rest.put(Routes.applicationGuildCommands(config.client_id, config.guild_id), { body: pCommands })
|
|
|
|
console.log('Successfully refreshed slash commands')
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error refreshing slash commands: ${error}`)
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 01:24:22 +01:00
|
|
|
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 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.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)
|
|
|
|
}
|
2022-01-03 15:07:49 +01:00
|
|
|
}
|
2022-02-27 16:20:42 +01:00
|
|
|
public async showNext(interaction: CommandInteraction): Promise<void> {
|
|
|
|
const guild = interaction.guild
|
|
|
|
if (!guild) {
|
|
|
|
console.log(`There is no guild here`)
|
|
|
|
return
|
2022-01-03 15:07:49 +01:00
|
|
|
}
|
2022-02-27 16:20:42 +01:00
|
|
|
|
|
|
|
const output = new eventHandler().getNextEvent(guild)
|
|
|
|
await interaction.reply(output)
|
2022-01-03 15:07:49 +01:00
|
|
|
return
|
|
|
|
}
|
2022-02-27 16:20:42 +01:00
|
|
|
public async listEvents(interaction: CommandInteraction): Promise<void> {
|
2022-01-19 22:19:06 +01:00
|
|
|
const guild = interaction.guild
|
|
|
|
if (!guild) {
|
|
|
|
console.log(`There is no guild here`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const output = new eventHandler().listAllEvents(guild)
|
|
|
|
await interaction.reply(output)
|
|
|
|
}
|
2022-01-03 15:07:49 +01:00
|
|
|
|
|
|
|
public commandList: discordCommand[] = [
|
|
|
|
{
|
2022-01-19 22:19:06 +01:00
|
|
|
name: "shownext",
|
|
|
|
description: "Shows next Events",
|
|
|
|
performCommand: this.showNext
|
2022-01-03 15:07:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-01-19 22:19:06 +01:00
|
|
|
name: "listevents",
|
|
|
|
description: "Lists all Events",
|
|
|
|
performCommand: this.listEvents
|
2022-01-03 15:07:49 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|