Skip to content

Commit 71cf51c

Browse files
committed
✨ Add toggle module command
1 parent c387541 commit 71cf51c

File tree

6 files changed

+118
-17
lines changed

6 files changed

+118
-17
lines changed

config/commands/admin.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
import { hasActiveMemberRole, hasStaffRole, permissions } from '@/conf/configUtils';
22

3+
export const module = {
4+
settings: {
5+
aliases: ['module'],
6+
clientPermissions: permissions.SEND_MESSAGES,
7+
userPermissions: hasStaffRole,
8+
},
9+
details: {
10+
name: 'Modifier les modules',
11+
content: "Permet d'activer ou de désactiver certains modules de Swan.",
12+
usage: 'module',
13+
examples: ['module skriptReleases off'],
14+
},
15+
embed: {
16+
title: 'Consultez la liste des modules sur Swan Dashboard',
17+
link: 'https://swan.skript-mc.fr/modules',
18+
content: "Vous pouvez consulter la liste des modules et modifier leurs états simplement depuis Swan Dashboard. Vous pouvez aussi directement utiliser Swan pour modifier l'état d'un de ces modules, via la commande `.module <nom> <on|off>`.",
19+
},
20+
messages: {
21+
noModuleFound: ":x: Aucun module avec ce nom n'a été trouvé. Rendez-vous sur https://swan.skript-mc.fr/modules pour consulter la liste des modules.",
22+
noStatus: ":x: Vous n'avez pas spécifié le statut à définir. Utilisez plutôt : `.module {module.name} <on|off>`.",
23+
success: ':white_check_mark: Le module a bien été {status}.',
24+
},
25+
};
26+
327
export const refresh = {
428
settings: {
529
aliases: ['refresh'],

src/commands/admin/module.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Command } from 'discord-akairo';
2+
import { MessageEmbed } from 'discord.js';
3+
import pupa from 'pupa';
4+
import SwanModule from '@/app/models/swanModule';
5+
import type { GuildMessage, SwanModuleDocument } from '@/app/types';
6+
import type { ModuleCommandArguments } from '@/app/types/CommandArguments';
7+
import { noop, toggleModule } from '@/app/utils';
8+
import { module as config } from '@/conf/commands/admin';
9+
import messages from '@/conf/messages';
10+
import settings from '@/conf/settings';
11+
12+
class ModuleCommand extends Command {
13+
constructor() {
14+
super('module', {
15+
aliases: config.settings.aliases,
16+
details: config.details,
17+
clientPermissions: config.settings.clientPermissions,
18+
userPermissions: config.settings.userPermissions,
19+
channel: 'guild',
20+
args: [
21+
{
22+
id: 'moduleName',
23+
type: 'string',
24+
},
25+
{
26+
id: 'enabled',
27+
type: 'string',
28+
},
29+
],
30+
});
31+
}
32+
33+
public async exec(message: GuildMessage, args: ModuleCommandArguments): Promise<void> {
34+
const modules: SwanModuleDocument[] = await SwanModule.find();
35+
36+
if (!args.moduleName) {
37+
const embed = new MessageEmbed()
38+
.setTitle(config.embed.title)
39+
.setURL(config.embed.link)
40+
.setColor(settings.colors.default)
41+
.setDescription(config.embed.content)
42+
.setFooter(pupa(messages.global.executedBy, { member: message.member }));
43+
void message.channel.send(embed).catch(noop);
44+
return;
45+
}
46+
47+
const module = modules.find(m => m.name === args.moduleName);
48+
if (!module) {
49+
void message.channel.send(config.messages.noModuleFound).catch(noop);
50+
return;
51+
}
52+
53+
if (!args.enabled) {
54+
void message.channel.send(pupa(config.messages.noStatus, { module })).catch(noop);
55+
return;
56+
}
57+
58+
const enabled = args.enabled === 'on';
59+
60+
toggleModule(this.client, module, enabled);
61+
await SwanModule.findOneAndUpdate({ name: module.name }, { enabled });
62+
63+
void message.channel.send(pupa(config.messages.success, { status: enabled ? 'activé' : 'désactivé' })).catch(noop);
64+
}
65+
}
66+
67+
export default ModuleCommand;

src/commands/admin/refresh.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import type { AkairoHandler } from 'discord-akairo';
21
import { Command } from 'discord-akairo';
32
import SharedConfig from '@/app/models/sharedConfig';
43
import SwanModule from '@/app/models/swanModule';
54
import type { GuildMessage, SharedConfigDocument } from '@/app/types';
65
import { SharedConfigName } from '@/app/types';
76
import type { RefreshCommandArgument } from '@/app/types/CommandArguments';
8-
import { nullop } from '@/app/utils';
7+
import { nullop, toggleModule } from '@/app/utils';
98
import { refresh as config } from '@/conf/commands/admin';
109

1110
class RefreshCommand extends Command {
@@ -22,21 +21,8 @@ class RefreshCommand extends Command {
2221
public async exec(message: GuildMessage, _args: RefreshCommandArgument): Promise<void> {
2322
// Refresh modules
2423
const modules = await SwanModule.find();
25-
for (const module of modules) {
26-
const handler: AkairoHandler = this.client[module.handler];
27-
const cachedModule = this.client.cache.modules.find(mod => mod.id === module.name);
28-
if (!cachedModule)
29-
continue;
30-
// See if the module is present in handler.modules (= if it is loaded).x
31-
const currentState = Boolean(handler.modules.findKey((_, key) => key === cachedModule.id));
32-
33-
if (handler && module.enabled !== currentState) {
34-
if (module.enabled)
35-
handler.load(cachedModule.filepath);
36-
else
37-
handler.remove(module.name);
38-
}
39-
}
24+
for (const module of modules)
25+
toggleModule(this.client, module, module.enabled);
4026

4127
// Refresh saved channels
4228
const configDocument: SharedConfigDocument = await SharedConfig.findOne({

src/types/CommandArguments.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export interface LinksCommandArguments {
7878
page: number;
7979
}
8080

81+
export interface ModuleCommandArguments {
82+
moduleName: string;
83+
enabled: string;
84+
}
85+
8186
export interface MoveCommandArguments {
8287
channel: TextChannel;
8388
message: Message;

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export { default as toValidName } from './toValidName';
1414
export { default as trimText } from './trimText';
1515
export { default as uncapitalize } from './uncapitalize';
1616
export { default as stripTags } from './stripTags';
17+
export { default as toggleModule } from './toggleModule';

src/utils/toggleModule.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { AkairoClient, AkairoHandler } from 'discord-akairo';
2+
import type { SwanModuleDocument } from '@/app/types';
3+
4+
export default function toggleModule(client: AkairoClient, module: SwanModuleDocument, isEnabled: boolean): void {
5+
const handler: AkairoHandler = client[module.handler];
6+
const cachedModule = client.cache.modules.find(mod => mod.id === module.name);
7+
if (!cachedModule)
8+
return;
9+
// See if the module is present in handler.modules (= if it is loaded).
10+
const currentState = Boolean(handler.modules.findKey((_, key) => key === cachedModule.id));
11+
12+
if (handler && isEnabled !== currentState) {
13+
if (isEnabled)
14+
handler.load(cachedModule.filepath);
15+
else
16+
handler.remove(module.name);
17+
}
18+
}

0 commit comments

Comments
 (0)