register commands to guilds the bot is in
	
		
			
	
		
	
	
		
	
		
			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:
		@@ -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}`)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user