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

Restores original 3 duration formats for Mod Actions #928

Merged
merged 8 commits into from
Feb 7, 2025
Merged
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
16 changes: 8 additions & 8 deletions src/discord/commands/guild/d.moderate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { stripIndents } from 'common-tags';
import { user_action_type, user_actions, users } from '@prisma/client';
import moment from 'moment';
import { SlashCommand } from '../../@types/commandDef';
import { makeValid, parseDuration, validateDurationInput } from '../../../global/utils/parseDuration';
import { parseDuration, validateDurationInput } from '../../../global/utils/parseDuration';
import commandContext from '../../utils/context'; // eslint-disable-line
import { getDiscordMember } from '../../utils/guildMemberLookup';
import { embedTemplate } from '../../utils/embedTemplate';
Expand Down Expand Up @@ -1396,9 +1396,9 @@ export async function moderate(

if (durationVal === '') {
durationVal = '7d';
} else {
durationVal = await makeValid(durationVal);
}
} // else {
// durationVal = await makeValid(durationVal);
// }

if (!validateDurationInput(durationVal)) {
return {
Expand All @@ -1418,10 +1418,10 @@ export async function moderate(
// log.debug(F, `duration: ${duration}`);
}
if (isFullBan(command)) {
let durationVal = modalInt.fields.getTextInputValue('ban_duration');
const durationVal = modalInt.fields.getTextInputValue('ban_duration');

if (durationVal !== '') {
durationVal = await makeValid(durationVal);
// durationVal = await makeValid(durationVal);
let tempBanDuration = parseInt(durationVal, 10);

if (Number.isNaN(tempBanDuration)) {
Expand Down Expand Up @@ -1568,11 +1568,11 @@ export async function moderate(
if (isFullBan(command) || isUnderban(command) || isBanEvasion(command)) {
targetData.removed_at = new Date();

let deleteMessageValue = modalInt.fields.getTextInputValue('days');
const deleteMessageValue = modalInt.fields.getTextInputValue('days');
let deleteDuration = 0;

if (deleteMessageValue !== '') {
deleteMessageValue = await makeValid(deleteMessageValue);
// deleteMessageValue = await makeValid(deleteMessageValue);
deleteDuration = parseInt(deleteMessageValue, 10);

if (Number.isNaN(deleteDuration)) {
Expand Down
89 changes: 52 additions & 37 deletions src/global/utils/parseDuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default parseDuration;
* @param {string} duration A string representing a duration
* @return {number} The duration in milliseconds
*/
export async function parseDuration(duration:string):Promise<number> {
export async function parseDuration(duration: string): Promise<number> {
// Those code inspired by https://gist.github.com/substanc3-dev/306bb4d04b2aad3a5d019052b1a0dec0
// This is super cool, thanks a lot!
const supported = 'smMhdwmoy';
Expand All @@ -19,74 +19,89 @@ export async function parseDuration(duration:string):Promise<number> {
let tempNumber = 0;
let tempString = '';
let timeValue = 0;

while (idx < duration.length) {
const c = duration[idx];
const c = duration[idx]; // Normalize to lowercase for easier comparison
switch (stage) {
case 1: // waiting for number
{
idx += 1;
case 1: // Waiting for number
if (numbers.includes(c)) {
tempString = c.toString();
tempString = c;
stage = 2;
}
idx += 1;
break;
}
case 2: // parsing the number
{

case 2: // Parsing the number
if (numbers.includes(c)) {
tempString += c;
idx += 1;
} else {
// log.debug(F, `TValue: ${tempString}`);
tempNumber = Number.parseInt(tempString, 10);
stage = 3;
}
break;
}
case 3: // parsing the qualifier
{
idx += 1;

case 3: // Parsing the qualifier
if (c === ' ') {
idx += 1;
break;
} else if (supported.includes(c)) {
// log.debug(F, `Qualifier ${c}`);
// Handle single-letter qualifiers
if (c === 'h') {
timeValue += tempNumber * 60 * 60 * 1000;
}
if (c === 'M') {
timeValue += tempNumber * 30 * 24 * 60 * 60 * 1000;
}
if (c === 'm') {
timeValue += tempNumber * 60 * 1000;
}
if (c === 's') {
timeValue += tempNumber * 1000;
}
if (c === 'd') {
timeValue += tempNumber * 24 * 60 * 60 * 1000;
}
if (c === 'w') {
timeValue += tempNumber * 7 * 24 * 60 * 60 * 1000;
}
if (c === 'y') {
timeValue += tempNumber * 365 * 24 * 60 * 60 * 1000;
timeValue += tempNumber * 60 * 60 * 1000; // Hours
} else if (c === 'M' || c === 'm') {
// Check if the next characters spell "month" or "months"
const nextChars = duration.slice(idx, idx + 5).toLowerCase();
if (nextChars.startsWith('month') || c === 'M') {
timeValue += tempNumber * 30 * 24 * 60 * 60 * 1000; // Convert months to milliseconds
if (c === 'M') {
idx += 1; // Skip 'M'
} else if (nextChars.startsWith('months')) {
idx += 6; // Skip 'months'
} else {
idx += 5; // Skip 'month'
}
} else {
timeValue += tempNumber * 60 * 1000; // Minutes
}
} else if (c === 's') {
timeValue += tempNumber * 1000; // Seconds
} else if (c === 'd') {
timeValue += tempNumber * 24 * 60 * 60 * 1000; // Days
} else if (c === 'w') {
timeValue += tempNumber * 7 * 24 * 60 * 60 * 1000; // Weeks
} else if (c === 'y') {
timeValue += tempNumber * 365 * 24 * 60 * 60 * 1000; // Years
}
stage = 1;
break;
} else return timeValue;
}
} else {
return timeValue; // Unsupported qualifier
}

default:
break;
}
}
return timeValue;
}

/*
Input validation for parseDuration. Ensures string is a written time in one or multiple of 3 formats.
Huge thank you to /u/gumnos on Reddit for the regex!
*/
export const validateDurationInput = (input: string): boolean => {
const regex = /^(\d+(y|M|w|d|h|m|s)\s?)+$/;
// eslint-disable-next-line max-len
const regex = /^(?: *(?:\d+ *(?:y(?:ears?)?|M|mon(ths?)?|w(?:eeks?)?|d(?:ays?)?|h(?:ours?)?|m(?:in(?:ute)?s?)?|s(?:ec(?:ond)?s?)?)))+$/;

return regex.test(input.trim());
};

/*
This function takes input in the form of years/months, etc, as written and converts them to smMhdwmoy format.
It was used to force string compliance for parseDuration but may not be necessary.
Feel free to remove it if it's not used in a year or two.
*/
export async function makeValid(duration: string): Promise<string> {
// Define a map for the units and their short forms
const unitMap: Record<string, string> = {
Expand Down
Loading