Skip to content

Instantly share code, notes, and snippets.

@rniii
Last active May 2, 2024 00:56
Show Gist options
  • Save rniii/2dcedb15142f25683f855311ecd3abb6 to your computer and use it in GitHub Desktop.
Save rniii/2dcedb15142f25683f855311ecd3abb6 to your computer and use it in GitHub Desktop.
Vencord plugin to override timestamp formats
/*
* Vencord, a Discord client mod
* Copyright (c) 2023 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
// Formats are specified as moment.js formats: https://momentjs.com/docs/#/displaying/format/
// [relative] and [calendar] are automatically replaced by time relative to now and to today respectively
import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import { moment } from "@webpack/common";
const settings = definePluginSettings({
cozyFormat: {
type: OptionType.STRING,
default: "[calendar]",
description: "time format to use in messages on cozy mode",
},
compactFormat: {
type: OptionType.STRING,
default: "LT",
description: "time format to use in messages on compact mode",
},
tooltipFormat: {
type: OptionType.STRING,
default: "LLLL • [relative]",
description: "time format to use on tooltip",
},
});
export default definePlugin({
name: "CustomTimestamps",
description: "Customises timestamps on messages and tooltips",
authors: [Devs.Rini],
settings,
patches: [{
find: "MESSAGE_EDITED_TIMESTAMP_A11Y_LABEL.format",
replacement: [
{
match: /(?<=\i=\i\?)\(0,\i\.\i\)\((\i),"LT"\):\(0,\i\.\i\)\(\i\)/,
replace: '$self.format($1,"compactFormat","[calendar]"):$self.format($1,"cozyFormat","LT")',
},
{
match: /(?<=text:)\(0,\i.\i\)\((\i),"LLLL"\)(?=,)/,
replace: '$self.format($1,"tooltipFormat","LLLL")',
},
]
}],
format(date: Date, key: string, fallback: string) {
const t = moment(date);
return t.format(settings.store[key] || fallback)
.replace("relative", () => t.fromNow())
.replace("calendar", () => t.calendar());
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment