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