Skip to content

Commit 6030c23

Browse files
committed
✨ Enable/Disable
1 parent ed0deff commit 6030c23

3 files changed

Lines changed: 67 additions & 18 deletions

File tree

src/classes/DiscordGuild.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import { Collection } from "discord.js";
22

3-
import BaseClass from "./BaseClass";
4-
53
import type DiscordCommand from "./DiscordCommand";
64
import type DiscordEvent from "./DiscordEvent";
75
import type DiscordModule from "./DiscordModule";
86

97
import type { Snowflake } from "discord.js";
108

11-
export default class DiscordGuild extends BaseClass {
9+
export default class DiscordGuild {
10+
disabled = false;
1211
events = new Collection<string, DiscordEvent<any>>();
1312
commands = new Collection<string, DiscordCommand>();
1413
modules = new Collection<string, DiscordModule>();
15-
constructor(public id: Snowflake) {
16-
super();
14+
constructor(public id: Snowflake) {}
15+
16+
enable() {
17+
this.disabled = false;
18+
for (const event of this.events.values()) event.enable();
19+
for (const command of this.commands.values()) command.enable();
20+
for (const module of this.modules.values()) module.enable();
21+
}
22+
23+
disable() {
24+
this.disabled = true;
25+
for (const event of this.events.values()) event.disable();
26+
for (const command of this.commands.values()) command.disable();
27+
for (const module of this.modules.values()) module.disable();
1728
}
1829
}

src/classes/DiscordModule.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { Collection } from "discord.js";
22

3-
import BaseClass from "./BaseClass";
4-
53
import type DiscordCommand from "./DiscordCommand";
64
import type DiscordEvent from "./DiscordEvent";
75

8-
export default class DiscordModule extends BaseClass {
6+
export default class DiscordModule {
7+
disabled = false;
98
events = new Collection<string, DiscordEvent<any>>();
109
commands = new Collection<string, DiscordCommand>();
1110
modules = new Collection<string, DiscordModule>();
12-
constructor(public name: string) {
13-
super();
11+
constructor(public name: string) {}
12+
13+
enable() {
14+
this.disabled = false;
15+
for (const event of this.events.values()) event.enable();
16+
for (const command of this.commands.values()) command.enable();
17+
for (const module of this.modules.values()) module.enable();
18+
}
19+
20+
disable() {
21+
this.disabled = true;
22+
for (const event of this.events.values()) event.disable();
23+
for (const command of this.commands.values()) command.disable();
24+
for (const module of this.modules.values()) module.disable();
1425
}
1526
}

src/classes/ModuleLoader.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,36 @@ import type {
1919
} from "discord.js";
2020

2121
export interface ModuleLoaderOptions {
22-
disallowedChannelMessage: string;
23-
commandCooldownMessage: string;
22+
unknownCommandMessage?: string;
23+
disabledCommandMessage?: string;
24+
disallowedChannelMessage?: string;
25+
commandCooldownMessage?: string;
2426
}
2527

2628
export default class DiscordModuleLoader {
29+
unknownCommandMessage =
30+
"Couldn't find executed command. Please try again later, or report the issue.";
31+
disabledCommandMessage =
32+
"This command is currently disabled. Please try again later.";
2733
disallowedChannelMessage =
2834
"You're not allowed to execute this command in this channel!";
2935
commandCooldownMessage =
3036
"Please wait % seconds before using this command again.";
3137

32-
commands: Collection<string, DiscordCommand> = new Collection();
33-
modules: Collection<string, DiscordModule> = new Collection();
34-
guilds: Collection<string, DiscordGuild> = new Collection();
35-
cooldowns: Collection<string, Collection<string, number>> = new Collection();
38+
events = new Collection<string, DiscordEvent<any>>();
39+
commands = new Collection<string, DiscordCommand>();
40+
modules = new Collection<string, DiscordModule>();
41+
guilds = new Collection<string, DiscordGuild>();
42+
cooldowns = new Collection<string, Collection<string, number>>();
3643
log = debug("Discord-Module-Loader");
3744

3845
constructor(public client: Client, options?: ModuleLoaderOptions) {
46+
if (options?.unknownCommandMessage)
47+
this.unknownCommandMessage = options.unknownCommandMessage;
48+
49+
if (options?.disabledCommandMessage)
50+
this.disabledCommandMessage = options.disabledCommandMessage;
51+
3952
if (options?.disallowedChannelMessage)
4053
this.disallowedChannelMessage = options.disallowedChannelMessage;
4154

@@ -169,7 +182,11 @@ export default class DiscordModuleLoader {
169182
if (!(event instanceof DiscordEvent))
170183
throw new Error(`Event ${file} is not an Event`);
171184

172-
this.client.on(event.event, event.listener);
185+
this.client.on(event.event, (...args) => {
186+
if (!event.disabled) event.listener(...args);
187+
});
188+
189+
this.events.set(event.event, event);
173190
returnEvents.push([event.event, event]);
174191
log("Loaded event %s", event.event);
175192
}
@@ -310,7 +327,17 @@ export default class DiscordModuleLoader {
310327
private async handleInteraction(interaction: Interaction<CacheType>) {
311328
if (interaction.isCommand() || interaction.isContextMenu()) {
312329
const command = this.commands.get(interaction.commandName.toLowerCase());
313-
if (!command) return;
330+
if (!command)
331+
return await interaction.reply({
332+
content: this.unknownCommandMessage,
333+
ephemeral: true
334+
});
335+
336+
if (command.disabled)
337+
return await interaction.reply({
338+
content: this.disabledCommandMessage,
339+
ephemeral: true
340+
});
314341

315342
let allowed = true;
316343
if (

0 commit comments

Comments
 (0)