Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cooldown utility & apply it to tripsitme, prevent checkMoodle timer function from giving the 101 role back during TripSit sessions #850

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading