Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
fix: have cooldowns work member-wide not globally, no matter the user
Browse files Browse the repository at this point in the history
  • Loading branch information
alexthemaster committed Aug 1, 2020
1 parent e728547 commit ee7d4cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
42 changes: 22 additions & 20 deletions src/events/Message.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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)!');

Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -50,9 +52,6 @@ export class MoonlightClient extends Client {
/** The task pool that stores all tasks */
public readonly tasks: TaskPool<string, Task> = new TaskPool(this);

/** The Map that stores command cooldowns */
public readonly cooldowns: Map<Command, Date> = new Map();

/** An array of owners */
public owners: string[] = new Array();

Expand Down
13 changes: 13 additions & 0 deletions src/lib/extendables/MoonlightGuildMember.ts
Original file line number Diff line number Diff line change
@@ -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<Command, Date> = new Map();
}

Structures.extend('GuildMember', () => MoonlightGuildMember);

export { MoonlightGuildMember };

0 comments on commit ee7d4cc

Please sign in to comment.