Skip to content

Commit

Permalink
Added cooldown utility & it to tripsitme, checkMoodle now skips 101 r…
Browse files Browse the repository at this point in the history
…ole if NeedsHelp present
  • Loading branch information
theimperious1 committed Oct 3, 2024
1 parent cf1d8e4 commit a5e8f6a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
48 changes: 48 additions & 0 deletions src/discord/utils/commandCooldown.ts
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;
21 changes: 21 additions & 0 deletions src/discord/utils/tripsitme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { ticket_status, user_tickets } from '@prisma/client';
import commandContext from './context';
import { embedTemplate } from './embedTemplate';
import { checkChannelPermissions, checkGuildPermissions } from './checkPermissions';
import { commandCooldown } from './commandCooldown';

const F = f(__filename);

Expand Down Expand Up @@ -837,6 +838,13 @@ export async function tripsitmeUserClose(
if (!interaction.channel) return;
log.info(F, await commandContext(interaction));

const cooldown = await commandCooldown(interaction.user, interaction.customId);

if (!cooldown.success && cooldown.message) {
await interaction.reply({ content: cooldown.message, ephemeral: true });
return;
}

await interaction.deferReply({ ephemeral: false });

const targetId = interaction.customId.split('~')[1];
Expand Down Expand Up @@ -1182,6 +1190,12 @@ export async function tripSitMe(
return null;
}

const cooldown = await commandCooldown(interaction.user, interaction.customId);

if (!cooldown.success && cooldown.message) {
await interaction.editReply(cooldown.message);
}

// const actor = interaction.member;
const guildData = await db.discord_guilds.upsert({
where: {
Expand Down Expand Up @@ -1462,6 +1476,13 @@ export async function tripsitmeButton(
log.info(F, await commandContext(interaction));
const target = interaction.member as GuildMember;

const cooldown = await commandCooldown(interaction.user, interaction.customId);

if (!cooldown.success && cooldown.message) {
await interaction.reply({ content: cooldown.message, ephemeral: true });
return;
}

// log.debug(F, `target: ${JSON.stringify(target, n ull, 2)}`);

// const actorIsAdmin = target.permissions.has(PermissionsBitField.Flags.Administrator);
Expand Down
9 changes: 6 additions & 3 deletions src/global/utils/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,12 @@ async function checkMoodle() { // eslint-disable-line
},
); // eslint-disable-line
}

member.roles.add(role);
log.info(F, `Gave ${member.user.username} the ${role.name} role`);
if (!member.roles.cache.has(env.ROLE_NEEDS_HELP)) {
member.roles.add(role);
log.info(F, `Gave ${member.user.username} the ${role.name} role`);
} else {
log.info(F, `Skipped giving ${member.user.username} the ${role.name} because they have the Needs Help role`);
}

// eslint-disable max-len
member.user.send({
Expand Down

0 comments on commit a5e8f6a

Please sign in to comment.