Skip to content

Commit

Permalink
✨ Add toggle module command
Browse files Browse the repository at this point in the history
  • Loading branch information
Romitou committed Jun 25, 2021
1 parent c387541 commit 71cf51c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 17 deletions.
24 changes: 24 additions & 0 deletions config/commands/admin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import { hasActiveMemberRole, hasStaffRole, permissions } from '@/conf/configUtils';

export const module = {
settings: {
aliases: ['module'],
clientPermissions: permissions.SEND_MESSAGES,
userPermissions: hasStaffRole,
},
details: {
name: 'Modifier les modules',
content: "Permet d'activer ou de désactiver certains modules de Swan.",
usage: 'module',
examples: ['module skriptReleases off'],
},
embed: {
title: 'Consultez la liste des modules sur Swan Dashboard',
link: 'https://swan.skript-mc.fr/modules',
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>`.",
},
messages: {
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.",
noStatus: ":x: Vous n'avez pas spécifié le statut à définir. Utilisez plutôt : `.module {module.name} <on|off>`.",
success: ':white_check_mark: Le module a bien été {status}.',
},
};

export const refresh = {
settings: {
aliases: ['refresh'],
Expand Down
67 changes: 67 additions & 0 deletions src/commands/admin/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Command } from 'discord-akairo';
import { MessageEmbed } from 'discord.js';
import pupa from 'pupa';
import SwanModule from '@/app/models/swanModule';
import type { GuildMessage, SwanModuleDocument } from '@/app/types';
import type { ModuleCommandArguments } from '@/app/types/CommandArguments';
import { noop, toggleModule } from '@/app/utils';
import { module as config } from '@/conf/commands/admin';
import messages from '@/conf/messages';
import settings from '@/conf/settings';

class ModuleCommand extends Command {
constructor() {
super('module', {
aliases: config.settings.aliases,
details: config.details,
clientPermissions: config.settings.clientPermissions,
userPermissions: config.settings.userPermissions,
channel: 'guild',
args: [
{
id: 'moduleName',
type: 'string',
},
{
id: 'enabled',
type: 'string',
},
],
});
}

public async exec(message: GuildMessage, args: ModuleCommandArguments): Promise<void> {
const modules: SwanModuleDocument[] = await SwanModule.find();

if (!args.moduleName) {
const embed = new MessageEmbed()
.setTitle(config.embed.title)
.setURL(config.embed.link)
.setColor(settings.colors.default)
.setDescription(config.embed.content)
.setFooter(pupa(messages.global.executedBy, { member: message.member }));
void message.channel.send(embed).catch(noop);
return;
}

const module = modules.find(m => m.name === args.moduleName);
if (!module) {
void message.channel.send(config.messages.noModuleFound).catch(noop);
return;
}

if (!args.enabled) {
void message.channel.send(pupa(config.messages.noStatus, { module })).catch(noop);
return;
}

const enabled = args.enabled === 'on';

toggleModule(this.client, module, enabled);
await SwanModule.findOneAndUpdate({ name: module.name }, { enabled });

void message.channel.send(pupa(config.messages.success, { status: enabled ? 'activé' : 'désactivé' })).catch(noop);
}
}

export default ModuleCommand;
20 changes: 3 additions & 17 deletions src/commands/admin/refresh.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { AkairoHandler } from 'discord-akairo';
import { Command } from 'discord-akairo';
import SharedConfig from '@/app/models/sharedConfig';
import SwanModule from '@/app/models/swanModule';
import type { GuildMessage, SharedConfigDocument } from '@/app/types';
import { SharedConfigName } from '@/app/types';
import type { RefreshCommandArgument } from '@/app/types/CommandArguments';
import { nullop } from '@/app/utils';
import { nullop, toggleModule } from '@/app/utils';
import { refresh as config } from '@/conf/commands/admin';

class RefreshCommand extends Command {
Expand All @@ -22,21 +21,8 @@ class RefreshCommand extends Command {
public async exec(message: GuildMessage, _args: RefreshCommandArgument): Promise<void> {
// Refresh modules
const modules = await SwanModule.find();
for (const module of modules) {
const handler: AkairoHandler = this.client[module.handler];
const cachedModule = this.client.cache.modules.find(mod => mod.id === module.name);
if (!cachedModule)
continue;
// See if the module is present in handler.modules (= if it is loaded).x
const currentState = Boolean(handler.modules.findKey((_, key) => key === cachedModule.id));

if (handler && module.enabled !== currentState) {
if (module.enabled)
handler.load(cachedModule.filepath);
else
handler.remove(module.name);
}
}
for (const module of modules)
toggleModule(this.client, module, module.enabled);

// Refresh saved channels
const configDocument: SharedConfigDocument = await SharedConfig.findOne({
Expand Down
5 changes: 5 additions & 0 deletions src/types/CommandArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export interface LinksCommandArguments {
page: number;
}

export interface ModuleCommandArguments {
moduleName: string;
enabled: string;
}

export interface MoveCommandArguments {
channel: TextChannel;
message: Message;
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export { default as toValidName } from './toValidName';
export { default as trimText } from './trimText';
export { default as uncapitalize } from './uncapitalize';
export { default as stripTags } from './stripTags';
export { default as toggleModule } from './toggleModule';
18 changes: 18 additions & 0 deletions src/utils/toggleModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { AkairoClient, AkairoHandler } from 'discord-akairo';
import type { SwanModuleDocument } from '@/app/types';

export default function toggleModule(client: AkairoClient, module: SwanModuleDocument, isEnabled: boolean): void {
const handler: AkairoHandler = client[module.handler];
const cachedModule = client.cache.modules.find(mod => mod.id === module.name);
if (!cachedModule)
return;
// See if the module is present in handler.modules (= if it is loaded).
const currentState = Boolean(handler.modules.findKey((_, key) => key === cachedModule.id));

if (handler && isEnabled !== currentState) {
if (isEnabled)
handler.load(cachedModule.filepath);
else
handler.remove(module.name);
}
}

0 comments on commit 71cf51c

Please sign in to comment.