-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cooldown utility & it to tripsitme, checkMoodle now skips 101 r…
…ole if NeedsHelp present
- Loading branch information
1 parent
cf1d8e4
commit a5e8f6a
Showing
3 changed files
with
75 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { User, GuildMember } from 'discord.js'; | ||
|
||
// Map to store cooldowns for users and their commands | ||
const cooldowns = new Map<string, Map<string, number>>(); | ||
|
||
/** | ||
* commandCooldown | ||
* @param {User | GuildMember} user The user or guild member | ||
* @param {string} commandName The name of the command being executed | ||
* @param {number} cooldownAmount The cooldown duration in milliseconds (default is 30 seconds) | ||
* @return {Promise<{ success: boolean; message?: string }>} | ||
*/ | ||
async function commandCooldown( | ||
user: User | GuildMember, | ||
commandName: string, | ||
cooldownAmount: number = 30000, | ||
): Promise<{ success: boolean; message?: string }> { | ||
const now = Date.now(); | ||
|
||
// Ensure there's a map for the user in the cooldowns map | ||
if (!cooldowns.has(user.id)) { | ||
cooldowns.set(user.id, new Map()); | ||
} | ||
|
||
const userCooldowns = cooldowns.get(user.id) as Map<string, number>; | ||
|
||
// Check if the user has a cooldown for the specific command | ||
const commandExpiration = userCooldowns.get(commandName); | ||
if (commandExpiration) { | ||
const expirationTime = commandExpiration + cooldownAmount; | ||
|
||
// If the cooldown is still active, inform the user | ||
if (now < expirationTime) { | ||
const timeLeft = (expirationTime - now) / 1000; // Time left in seconds | ||
return { | ||
success: false, | ||
message: `Please wait ${timeLeft.toFixed(1)} more seconds before using this command or button again.`, | ||
}; | ||
} | ||
} | ||
|
||
// Set or reset the cooldown for the specific command | ||
userCooldowns.set(commandName, now); | ||
|
||
return { success: true }; | ||
} | ||
|
||
export default commandCooldown; |
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