register commands to guilds the bot is in
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
mightypanders 2022-03-23 21:59:27 +01:00
parent 9b2ddc35fa
commit 4dad1b9ad8
2 changed files with 23 additions and 9 deletions

8
server/commands/pong.ts Normal file
View File

@ -0,0 +1,8 @@
import { Command } from '../structures/command'
export default new Command({
name: 'ping',
description: 'Does a pong',
run: async ({ interaction }) => {
interaction.reply('pong')
}
})

View File

@ -1,4 +1,4 @@
import { ApplicationCommandDataResolvable, Client, Collection, Intents } from "discord.js";
import { ApplicationCommandDataResolvable, Client, Collection, Guild, Intents, Snowflake } from "discord.js";
import { CommandType } from "../types/commandTypes";
import fs from 'fs'
import { config } from "../configuration";
@ -22,12 +22,14 @@ export class ExtendedClient extends Client {
console.debug(`Importing ${filepath}`)
const imported = await import(filepath)
console.debug(`Imported ${JSON.stringify(imported)}`)
return imported
return imported.default ?? imported
}
public async registerCommands(cmds: ApplicationCommandDataResolvable[], guildId: string) {
if (guildId) {
this.guilds.cache.get(guildId)?.commands.set(cmds)
console.log(`Registering commands to ${guildId}`)
public async registerCommands(cmds: ApplicationCommandDataResolvable[], guildIds: Collection<Snowflake, Guild>) {
if (guildIds) {
guildIds.forEach(guild => {
this.guilds.cache.get(guild.id)?.commands.set(cmds)
console.log(`Registering commands to ${guild}|${guild.id}`)
})
} else {
this.application?.commands.set(cmds)
console.log(`Registering global commands`)
@ -39,15 +41,19 @@ export class ExtendedClient extends Client {
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)
const filePath = `${this.commandFilePath}/${commandFile}`
const command = await this.importFile(filePath)
console.debug(JSON.stringify(command))
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 ?? "")
console.log(`Ready processing ${client}`)
console.log(`${JSON.stringify(slashCommands)}`)
const guilds = client.guilds.cache
this.registerCommands(slashCommands, guilds)
})
} catch (error) {
console.log(`Error refreshing slash commands: ${error}`)