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

feat: fix leap year rules, add year and month to the delay string #501

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dotenv": "^4.0.0",
"emoji-regex": "^8.0.0",
"escape-string-regexp": "^1.0.5",
"express": "^4.19.2",
"express": "^4.20.0",
"fp-ts": "^2.0.1",
"humanize-duration": "^3.15.0",
"js-yaml": "^3.13.1",
Expand Down
34 changes: 34 additions & 0 deletions backend/src/humanizeDuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import humanizeduration from "humanize-duration";

export const delayStringMultipliers = {
y: 1000 * 60 * 60 * 24 * (365 + 1 / 4 - 1 / 100 + 1 / 400), // 365 + 1/4 - 1/100 + 1/400 (leap year rules) = 365.2425 days
mo: (1000 * 60 * 60 * 24 * (365 + 1 / 4 - 1 / 100 + 1 / 400)) / 12, // 365.2425 / 12 = 30.436875 days
w: 1000 * 60 * 60 * 24 * 7,
d: 1000 * 60 * 60 * 24,
h: 1000 * 60 * 60,
m: 1000 * 60,
s: 1000,
ms: 1,
};

export const humanizeDurationShort = humanizeduration.humanizer({
language: "shortEn",
languages: {
shortEn: {
y: () => "y",
mo: () => "mo",
w: () => "w",
d: () => "d",
h: () => "h",
m: () => "m",
s: () => "s",
ms: () => "ms",
},
},
spacer: "",
unitMeasures: delayStringMultipliers,
});

export const humanizeDuration = humanizeduration.humanizer({
unitMeasures: delayStringMultipliers,
});
18 changes: 0 additions & 18 deletions backend/src/humanizeDurationShort.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import z from "zod";
import { SavedMessage } from "../../../data/entities/SavedMessage.js";
import { humanizeDurationShort } from "../../../humanizeDurationShort.js";
import { humanizeDurationShort } from "../../../humanizeDuration.js";
import { getBaseUrl } from "../../../pluginUtils.js";
import { convertDelayStringToMS, sorter, zDelayString } from "../../../utils.js";
import { RecentActionType } from "../constants.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/ContextMenus/actions/mute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContextMenuCommandInteraction } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { canActOn } from "../../../pluginUtils.js";
import { convertDelayStringToMS } from "../../../utils.js";
import { CaseArgs } from "../../Cases/types.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/LocateUser/commands/FollowCmd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import humanizeDuration from "humanize-duration";
import moment from "moment-timezone";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { registerExpiringVCAlert } from "../../../data/loops/expiringVCAlertsLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { MINUTES, SECONDS } from "../../../utils.js";
import { locateUserCmd } from "../types.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Logs/logFunctions/logMemberJoin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GuildMember } from "discord.js";
import humanizeDuration from "humanize-duration";
import { humanizeDuration } from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { LogType } from "../../../data/LogType.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/ModActions/commands/BanCmd.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import humanizeDuration from "humanize-duration";
import { getMemberLevel } from "knub/helpers";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { CaseTypes } from "../../../data/CaseTypes.js";
import { clearExpiringTempban, registerExpiringTempban } from "../../../data/loops/expiringTempbansLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { canActOn, hasPermission, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin.js";
import { renderUsername, resolveMember, resolveUser } from "../../../utils.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/ModActions/commands/MassBanCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { performance } from "perf_hooks";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { CaseTypes } from "../../../data/CaseTypes.js";
import { LogType } from "../../../data/LogType.js";
import { humanizeDurationShort } from "../../../humanizeDurationShort.js";
import { humanizeDurationShort } from "../../../humanizeDuration.js";
import { canActOn, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin.js";
import { DAYS, MINUTES, SECONDS, noop } from "../../../utils.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GuildMember, GuildTextBasedChannel, Message, User } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { logger } from "../../../logger.js";
import { hasPermission, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { UnknownUser, asSingleLine, isDiscordAPIError, renderUsername } from "../../../utils.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GuildMember, Message, User } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { hasPermission, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { MutesPlugin } from "../../../plugins/Mutes/MutesPlugin.js";
import { UnknownUser, asSingleLine, renderUsername } from "../../../utils.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/ModActions/functions/banUserId.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DiscordAPIError, Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { CaseTypes } from "../../../data/CaseTypes.js";
import { LogType } from "../../../data/LogType.js";
import { registerExpiringTempban } from "../../../data/loops/expiringTempbansLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { logger } from "../../../logger.js";
import { TemplateParseError, TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter.js";
import {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/ModActions/functions/clearTempban.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { CaseTypes } from "../../../data/CaseTypes.js";
import { LogType } from "../../../data/LogType.js";
import { Tempban } from "../../../data/entities/Tempban.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { logger } from "../../../logger.js";
import { resolveUser } from "../../../utils.js";
import { CasesPlugin } from "../../Cases/CasesPlugin.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Mutes/commands/MutesCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "discord.js";
import moment from "moment-timezone";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { humanizeDurationShort } from "../../../humanizeDurationShort.js";
import { humanizeDurationShort } from "../../../humanizeDuration.js";
import { getBaseUrl } from "../../../pluginUtils.js";
import { DBDateFormat, MINUTES, renderUsername, resolveMember } from "../../../utils.js";
import { IMuteWithDetails, mutesCmd } from "../types.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Mutes/functions/muteUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError.js";
import { CaseTypes } from "../../../data/CaseTypes.js";
Expand All @@ -8,6 +7,7 @@ import { MuteTypes } from "../../../data/MuteTypes.js";
import { Case } from "../../../data/entities/Case.js";
import { Mute } from "../../../data/entities/Mute.js";
import { registerExpiringMute } from "../../../data/loops/expiringMutesLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { LogsPlugin } from "../../../plugins/Logs/LogsPlugin.js";
import { TemplateParseError, TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter.js";
import {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Mutes/functions/unmuteUser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { CaseTypes } from "../../../data/CaseTypes.js";
import { AddMuteParams } from "../../../data/GuildMutes.js";
import { MuteTypes } from "../../../data/MuteTypes.js";
import { Mute } from "../../../data/entities/Mute.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { noop, resolveMember, resolveUser } from "../../../utils.js";
import { CasesPlugin } from "../../Cases/CasesPlugin.js";
import { CaseArgs } from "../../Cases/types.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Post/commands/ScheduledPostsListCmd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { escapeCodeBlock } from "discord.js";
import humanizeDuration from "humanize-duration";
import moment from "moment-timezone";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { createChunkedMessage, DBDateFormat, deactivateMentions, sorter, trimLines } from "../../../utils.js";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin.js";
import { postCmd } from "../types.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Post/util/actualPostCmd.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GuildTextBasedChannel, Message } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { registerUpcomingScheduledPost } from "../../../data/loops/upcomingScheduledPostsLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { DBDateFormat, MINUTES, StrictMessageContent, errorMessage, renderUsername } from "../../../utils.js";
import { LogsPlugin } from "../../Logs/LogsPlugin.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Reminders/commands/RemindCmd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import humanizeDuration from "humanize-duration";
import moment from "moment-timezone";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { registerUpcomingReminder } from "../../../data/loops/upcomingRemindersLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { convertDelayStringToMS, messageLink } from "../../../utils.js";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Reminders/commands/RemindersCmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import humanizeDuration from "humanize-duration";
import moment from "moment-timezone";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { sendErrorMessage } from "../../../pluginUtils.js";
import { createChunkedMessage, DBDateFormat, sorter } from "../../../utils.js";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Slowmode/commands/SlowmodeGetCmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import humanizeDuration from "humanize-duration";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { slowmodeCmd } from "../types.js";

export const SlowmodeGetCmd = slowmodeCmd({
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Slowmode/commands/SlowmodeListCmd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GuildChannel, TextChannel } from "discord.js";
import humanizeDuration from "humanize-duration";
import { createChunkedMessage } from "knub/helpers";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { errorMessage } from "../../../utils.js";
import { slowmodeCmd } from "../types.js";

Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Slowmode/commands/SlowmodeSetCmd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { escapeInlineCode, PermissionsBitField } from "discord.js";
import humanizeDuration from "humanize-duration";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { asSingleLine, DAYS, HOURS, MINUTES } from "../../../utils.js";
import { getMissingPermissions } from "../../../utils/getMissingPermissions.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Tags/TagsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration";
import { PluginOptions, guildPlugin } from "knub";
import moment from "moment-timezone";
import { GuildArchives } from "../../data/GuildArchives.js";
import { GuildLogs } from "../../data/GuildLogs.js";
import { GuildSavedMessages } from "../../data/GuildSavedMessages.js";
import { GuildTags } from "../../data/GuildTags.js";
import { humanizeDuration } from "../../humanizeDuration.js";
import { makePublicFn } from "../../pluginUtils.js";
import { convertDelayStringToMS } from "../../utils.js";
import { LogsPlugin } from "../Logs/LogsPlugin.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Utility/commands/AboutCmd.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { APIEmbed, GuildChannel } from "discord.js";
import humanizeDuration from "humanize-duration";
import LCL from "last-commit-log";
import shuffle from "lodash/shuffle.js";
import moment from "moment-timezone";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { rootDir } from "../../../paths.js";
import { getCurrentUptime } from "../../../uptime.js";
import { resolveMember, sorter } from "../../../utils.js";
Expand Down
2 changes: 1 addition & 1 deletion backend/src/plugins/Utility/commands/CleanCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { allowTimeout } from "../../../RegExpRunner.js";
import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { LogType } from "../../../data/LogType.js";
import { SavedMessage } from "../../../data/entities/SavedMessage.js";
import { humanizeDurationShort } from "../../../humanizeDurationShort.js";
import { humanizeDurationShort } from "../../../humanizeDuration.js";
import { getBaseUrl, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils.js";
import { ModActionsPlugin } from "../../../plugins/ModActions/ModActionsPlugin.js";
import { DAYS, SECONDS, chunkArray, getInviteCodesInString, noop } from "../../../utils.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { APIEmbed, ChannelType, Snowflake, StageChannel, VoiceChannel } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { EmbedWith, MINUTES, formatNumber, preEmbedPadding, trimLines, verboseUserMention } from "../../../utils.js";
import { UtilityPluginType } from "../types.js";

Expand Down
17 changes: 5 additions & 12 deletions backend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import {
import emojiRegex from "emoji-regex";
import fs from "fs";
import https from "https";
import humanizeDuration from "humanize-duration";
import isEqual from "lodash/isEqual.js";
import { performance } from "perf_hooks";
import tlds from "tlds" assert { type: "json" };
import tmp from "tmp";
import { URL } from "url";
import { z, ZodEffects, ZodError, ZodRecord, ZodString } from "zod";
import { ISavedMessageAttachmentData, SavedMessage } from "./data/entities/SavedMessage.js";
import { delayStringMultipliers, humanizeDuration } from "./humanizeDuration.js";
import { getProfiler } from "./profiler.js";
import { SimpleCache } from "./SimpleCache.js";
import { sendDM } from "./utils/sendDM.js";
Expand All @@ -44,21 +44,14 @@ import { waitForButtonConfirm } from "./utils/waitForInteraction.js";

const fsp = fs.promises;

const delayStringMultipliers = {
w: 1000 * 60 * 60 * 24 * 7,
d: 1000 * 60 * 60 * 24,
h: 1000 * 60 * 60,
m: 1000 * 60,
s: 1000,
x: 1,
};

export const MS = 1;
export const SECONDS = 1000 * MS;
export const MINUTES = 60 * SECONDS;
export const HOURS = 60 * MINUTES;
export const DAYS = 24 * HOURS;
export const WEEKS = 7 * 24 * HOURS;
export const WEEKS = 7 * DAYS;
export const YEARS = (365 + 1 / 4 - 1 / 100 + 1 / 400) * DAYS;
export const MONTHS = YEARS / 12;

export const EMPTY_CHAR = "\u200b";

Expand Down Expand Up @@ -407,7 +400,7 @@ const MAX_DELAY_STRING_AMOUNT = 100 * 365 * DAYS;
* Turns a "delay string" such as "1h30m" to milliseconds
*/
export function convertDelayStringToMS(str, defaultUnit = "m"): number | null {
const regex = /^([0-9]+)\s*([wdhms])?[a-z]*\s*/;
const regex = /^([0-9]+)\s*((?:mo?)|[ywdhs])?[a-z]*\s*/;
let match;
let ms = 0;

Expand Down
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"vue-loader": "^15.10.1",
"vue-style-loader": "^4.1.3",
"vue-template-compiler": "^2.7.14",
"webpack": "^5.88.0",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1",
"webpack-merge": "^5.9.0"
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/components/dashboard/GuildAccess.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ import {
GuildState,
RootState
} from "../../store/types";
import humanizeDuration from "humanize-duration";
import { humanizeDuration } from "humanize-duration";
import moment from "moment";

export default {
Expand Down
Loading
Loading