-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |