Skip to content

Commit f0fcc9d

Browse files
committed
🐛 Fix slash commands
1 parent c799107 commit f0fcc9d

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "discord-module-loader",
33
"description": "A package that lets you load events and commands easily and fast.",
4-
"version": "1.0.1",
4+
"version": "1.1.1",
55
"main": "lib/index.js",
66
"repository": "https://github.com/Recodive/Discord-Module-Loader",
77
"author": "Timeraa <me@timeraa.dev>",

src/classes/DiscordCommand.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export default class DiscordCommand extends BaseClass {
2323
permissions?: ApplicationCommandPermissionData[];
2424
hasUserCommand?: boolean;
2525
execute: (
26-
interaction: CommandInteraction | ContextMenuInteraction
26+
interaction:
27+
| CommandInteraction
28+
| ContextMenuInteraction
29+
| AutocompleteInteraction
2730
) => Awaitable<void>;
2831

2932
constructor(options: CommandOptions) {

src/classes/ModuleLoader.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import debug from "debug";
2-
import { Collection } from "discord.js";
2+
import { Collection, Snowflake } from "discord.js";
33
import { existsSync } from "node:fs";
44
import { lstat, readdir } from "node:fs/promises";
55
import { basename, resolve } from "node:path";
@@ -93,6 +93,9 @@ export default class DiscordModuleLoader {
9393
if (this.guilds.has(guild.id))
9494
throw new Error(`Cannot add ${guild.id} more than once.`);
9595

96+
this.guilds.set(guild.id, guild);
97+
returnGuilds.push([guild.id, guild]);
98+
9699
if (existsSync(resolve(dir, folder, "events")))
97100
this.addToColl(
98101
guild.events,
@@ -111,8 +114,6 @@ export default class DiscordModuleLoader {
111114
await this.loadModules(resolve(dir, folder, "modules"), guild.id)
112115
);
113116

114-
this.guilds.set(guild.id, guild);
115-
returnGuilds.push([guild.id, guild]);
116117
log("Loaded guild module for guild: %s", guild.id);
117118
}
118119
return returnGuilds;
@@ -188,7 +189,11 @@ export default class DiscordModuleLoader {
188189
if (!(event instanceof DiscordEvent))
189190
throw new Error(`Event ${file} is not an Event`);
190191

191-
if (guildId) event.guildId = guildId;
192+
if (guildId) {
193+
this.guilds.get(guildId)!.events.set(event.event, event);
194+
195+
event.guildId = guildId;
196+
}
192197

193198
this.client.on(event.event, (...args) => {
194199
if (
@@ -206,7 +211,7 @@ export default class DiscordModuleLoader {
206211
return returnEvents;
207212
}
208213

209-
async loadCommands(dir = "commands", globalCommands: true | string = true) {
214+
async loadCommands(dir = "commands", guildId?: Snowflake) {
210215
dir = resolve(dir);
211216
if (!existsSync(dir)) return [];
212217

@@ -225,8 +230,12 @@ export default class DiscordModuleLoader {
225230
if (this.commands.has(command.name.toLowerCase()))
226231
throw new Error(`Cannot add ${command.name} more than once.`);
227232

228-
if (globalCommands !== true) {
229-
command.guildId = globalCommands;
233+
if (guildId) {
234+
this.guilds
235+
.get(guildId)!
236+
.commands.set(command.name.toLowerCase(), command);
237+
238+
command.guildId = guildId;
230239
}
231240

232241
this.commands.set(command.name.toLowerCase(), command);
@@ -338,6 +347,18 @@ export default class DiscordModuleLoader {
338347
}
339348

340349
private async handleInteraction(interaction: Interaction<CacheType>) {
350+
if (interaction.isAutocomplete()) {
351+
const command = this.commands.get(interaction.commandName.toLowerCase());
352+
353+
if (!command) return await interaction.respond([]);
354+
355+
try {
356+
await command.execute(interaction);
357+
} catch (err) {
358+
console.error(err);
359+
}
360+
}
361+
341362
if (interaction.isCommand() || interaction.isContextMenu()) {
342363
const command = this.commands.get(interaction.commandName.toLowerCase());
343364
if (!command)

0 commit comments

Comments
 (0)