From f4d4bea5be6d29e2de96b7e7ff185824280b6d0a Mon Sep 17 00:00:00 2001 From: Kovacs Alex Date: Sat, 1 Aug 2020 00:01:27 +0300 Subject: [PATCH] feat(Commands): add a reload command --- src/commands/Reload.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/commands/Reload.ts diff --git a/src/commands/Reload.ts b/src/commands/Reload.ts new file mode 100644 index 00000000..391c3e5d --- /dev/null +++ b/src/commands/Reload.ts @@ -0,0 +1,42 @@ +import { Command } from '../lib/structures/Command'; +import { MoonlightClient } from '../lib/Client'; +import { Stopwatch } from '../lib/util'; +import { BasePool } from '../lib/structures/Pools/Base/BasePool'; +import { Message } from 'discord.js'; + +/** @ignore */ +export default class extends Command { + constructor(client: MoonlightClient, pool: BasePool) { + super(client, pool, { cooldown: 5, ownerOnly: true, usage: '[everything] [piece:string]' }); + } + + public async run(message: Message, args: ReloadCommandArgs) { + if (!args.everything && !args.piece) return message.channel.send('Please provide the name of a piece you want to reload or provide "everything" as an argument to reload everything.'); + + if (args.everything) { + const stopwatch = new Stopwatch(); + await Promise.all(Array.from(this.client.pools).map(async ([_, poolPiece]) => await poolPiece.reload())); + stopwatch.stop(); + + return message.channel.send(`Done! Reloaded everything in: ${stopwatch.getElapsedHuman}`); + } + + if (!args.piece) return message.channel.send('Please provide a piece name to reload!'); + + if (!this.client.pools.has(args.piece)) return message.channel.send(`No such piece named \`${args.piece}\``); + + const pieces = Array.from(this.client.pools).filter(([name]) => name === args.piece).map(([_, piece]) => piece); + + const stopwatch = new Stopwatch(); + await Promise.all(pieces.map(async piece => await piece.reload())); + stopwatch.stop(); + + if (pieces.length === 1) return message.channel.send(`Done! Reloaded ${args.piece} in: ${stopwatch.getElapsedHuman}`); + else return message.channel.send(`Done! Reloaded every piece with the name of ${args.piece} in: ${stopwatch.getElapsedHuman}`); + } +} + +interface ReloadCommandArgs { + everything?: string; + piece?: string; +} \ No newline at end of file