Skip to content

Commit

Permalink
refactor: Improve warning functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
juddisjudd committed Jun 3, 2024
1 parent 0f02230 commit 9d38ca0
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/commands/moderation/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,36 @@ const Warn: Command = {
});

const timeoutDuration = calculateTimeoutDuration(targetUser.warns);
handleMemberWarn(
userOption,
interaction.user,
reasonOptionRaw,
targetUser.warns,
timeoutDuration,
interaction.guild,
id
);

try {
const member = await interaction.guild.members.fetch(userOption.id);
await member.timeout(timeoutDuration, `Accumulated Warns: ${targetUser.warns}`);
const currentTimeoutEnd = member.communicationDisabledUntilTimestamp;

let combinedTimeoutDuration = timeoutDuration;
if (currentTimeoutEnd && currentTimeoutEnd > Date.now()) {
const remainingTime = currentTimeoutEnd - Date.now();
combinedTimeoutDuration += remainingTime;
}

await member.timeout(combinedTimeoutDuration, `Accumulated Warns: ${targetUser.warns}`);

handleMemberWarn(
userOption,
interaction.user,
reasonOptionRaw,
targetUser.warns,
combinedTimeoutDuration,
interaction.guild,
id
);

await sendWarningDM(interaction, userOption, reasonOptionRaw, combinedTimeoutDuration);
await interaction.followUp(`<@${userOption.id}> has been warned. Reason: ${reasonOptionRaw}`);
} catch (error) {
logger.error('Error fetching guild member:', error);
await interaction.followUp({ content: 'Failed to find the specified user in the guild.' });
return;
}

await sendWarningDM(interaction, userOption, reasonOptionRaw, timeoutDuration);

await interaction.channel?.send(`<@${userOption.id}> has been warned. Reason: ${reasonOptionRaw}`);
} catch (error) {
logger.error('Error processing the warning:', error);
let errorMessage = 'An error occurred while processing the warning.';
Expand Down Expand Up @@ -121,7 +129,6 @@ async function sendWarningDM(
}
}


function calculateTimeoutDuration(warnCount: number): number {
let minutes;

Expand Down

0 comments on commit 9d38ca0

Please sign in to comment.