From ee7d4cca5eb347422fdf2eab6aac7f630d21dd04 Mon Sep 17 00:00:00 2001 From: Kovacs Alex Date: Sun, 2 Aug 2020 00:16:33 +0300 Subject: [PATCH] fix: have cooldowns work member-wide not globally, no matter the user --- src/events/Message.ts | 42 +++++++++++---------- src/lib/Client.ts | 5 +-- src/lib/extendables/MoonlightGuildMember.ts | 13 +++++++ 3 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 src/lib/extendables/MoonlightGuildMember.ts diff --git a/src/events/Message.ts b/src/events/Message.ts index b72968a0..7b0c198c 100644 --- a/src/events/Message.ts +++ b/src/events/Message.ts @@ -1,5 +1,6 @@ import { Event } from '../lib/structures/Event'; import { MoonlightClient } from '../lib/Client'; +import { MoonlightGuildMember } from '../lib/extendables/MoonlightGuildMember'; import { BasePool } from '../lib/structures/Pools/Base/BasePool'; import { Command } from '../lib/structures/Command'; import { ArgumentParser } from '../lib/util'; @@ -41,31 +42,33 @@ export default class extends Event { const cmd: Command | null = this.client.commands.get(command.toLowerCase()) ?? this.client.commands.get((this.client.aliases.get(command.toLowerCase()) as string)) ?? null; if (!cmd) return; - if (!this.client.owners.some(owner => owner === message.author.id) && this.client.cooldowns.has(cmd)) { - // Shout-out to this amazing Stack Overflow answer for this solution https://stackoverflow.com/a/53829705 - const cooldownEnd = moment(this.client.cooldowns.get(cmd)); - const now = moment(); - moment.relativeTimeThreshold('ss', 60); - moment.updateLocale('en', { - relativeTime: { - s: function (number) { - return number + ' seconds'; + if (message.guild) { + if (!this.client.owners.some(owner => owner === message.author.id) && (message.member as MoonlightGuildMember).cooldowns.has(cmd)) { + // Shout-out to this amazing Stack Overflow answer for this solution https://stackoverflow.com/a/53829705 + const cooldownEnd = moment((message.member as MoonlightGuildMember).cooldowns.get(cmd)); + const now = moment(); + moment.relativeTimeThreshold('ss', 60); + moment.updateLocale('en', { + relativeTime: { + s: function (number) { + return number + ' seconds'; + } } - } - }); + }); - const duration = moment.duration(cooldownEnd.diff(now)).humanize(); + const duration = moment.duration(cooldownEnd.diff(now)).humanize(); - return message.channel.send(`You have already used this command recently. Please try again ${duration}.`); - }; + return message.channel.send(`You have already used this command recently. Please try again ${duration}.`); + }; - const cooldownEnd = moment(new Date()).add(cmd.cooldown, 'seconds').toDate(); + const cooldownEnd = moment(new Date()).add(cmd.cooldown, 'seconds').toDate(); - this.client.cooldowns.set(cmd, cooldownEnd); + (message.member as MoonlightGuildMember).cooldowns.set(cmd, cooldownEnd); - setTimeout(() => { - this.client.cooldowns.delete(cmd); - }, cmd.cooldown * 1000); + setTimeout(() => { + (message.member as MoonlightGuildMember).cooldowns.delete(cmd); + }, cmd.cooldown * 1000); + } if (cmd.ownerOnly && !this.client.owners.includes(message.author.id)) return message.channel.send('This command can only be used by the bot owner(s)!'); @@ -75,7 +78,6 @@ export default class extends Event { if (cmd.nsfw && message.channel.type !== 'dm' && !message.channel.nsfw) return message.channel.send('This command can only be used in NSFW chnanels.'); - if (message.guild) { const missingPerms = message.member?.permissionsIn(message.channel).missing(cmd.requiredPermissions); const missingBotPerms = message.guild.me?.permissionsIn(message.channel).missing(cmd.requiredBotPermissions); diff --git a/src/lib/Client.ts b/src/lib/Client.ts index bad152c2..d382c421 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -10,6 +10,8 @@ import { TaskPool } from './structures/Pools/TaskPool'; import { Stopwatch } from './util'; import path from 'path'; +import './extendables/MoonlightGuildMember'; + /** * @external ClientOptions * @see {@link https://discord.js.org/#/docs/main/stable/typedef/ClientOptions} @@ -50,9 +52,6 @@ export class MoonlightClient extends Client { /** The task pool that stores all tasks */ public readonly tasks: TaskPool = new TaskPool(this); - /** The Map that stores command cooldowns */ - public readonly cooldowns: Map = new Map(); - /** An array of owners */ public owners: string[] = new Array(); diff --git a/src/lib/extendables/MoonlightGuildMember.ts b/src/lib/extendables/MoonlightGuildMember.ts new file mode 100644 index 00000000..59da79fe --- /dev/null +++ b/src/lib/extendables/MoonlightGuildMember.ts @@ -0,0 +1,13 @@ +import { Command } from '../structures/Command'; +import { Structures } from 'discord.js'; + +const GuildMember = Structures.get('GuildMember'); + +class MoonlightGuildMember extends GuildMember { + /** The Map that stores command cooldowns */ + public readonly cooldowns: Map = new Map(); +} + +Structures.extend('GuildMember', () => MoonlightGuildMember); + +export { MoonlightGuildMember }; \ No newline at end of file