/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
import "./styles.css";
import { addChatBarButton, ChatBarButton, removeChatBarButton } from "@api/ChatButtons";
import { addPreSendListener, removePreSendListener } from "@api/MessageEvents";
import { definePluginSettings } from "@api/Settings";
import { classNameFactory } from "@api/Styles";
import { Devs } from "@utils/constants";
import { getTheme, insertTextIntoChatInputBox, Theme } from "@utils/discord";
import { Margins } from "@utils/margins";
import { closeModal, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, openModal } from "@utils/modal";
import definePlugin, { OptionType } from "@utils/types";
import { Button, Forms, Parser, Select, useMemo, useState } from "@webpack/common";
const settings = definePluginSettings({
replaceMessageContents: {
description: "Replace timestamps in message contents",
type: OptionType.BOOLEAN,
default: true,
},
});
function parseTime(time: string) {
const cleanTime = time.slice(1, -1).replace(/(\d)(AM|PM)$/i, "$1 $2");
let ms = new Date(`${new Date().toDateString()} ${cleanTime}`).getTime() / 1000;
if (isNaN(ms)) return time;
// add 24h if time is in the past
if (Date.now() / 1000 > ms) ms += 86400;
return ``;
}
const Formats = ["", "t", "T", "d", "D", "f", "F", "R"] as const;
type Format = typeof Formats[number];
const cl = classNameFactory("vc-st-");
function PickerModal({ rootProps, close }: { rootProps: ModalProps, close(): void; }) {
const [value, setValue] = useState();
const [format, setFormat] = useState("");
const time = Math.round((new Date(value!).getTime() || Date.now()) / 1000);
const formatTimestamp = (time: number, format: Format) => ``;
const [formatted, rendered] = useMemo(() => {
const formatted = formatTimestamp(time, format);
return [formatted, Parser.parse(formatted)];
}, [time, format]);
return (
Timestamp Picker
setValue(e.currentTarget.value)}
style={{
colorScheme: getTheme() === Theme.Light ? "light" : "dark",
}}
/>
Timestamp Format
);
}
const ChatBarIcon: ChatBarButton = ({ isMainChat }) => {
if (!isMainChat) return null;
return (
{
const key = openModal(props => (
closeModal(key)}
/>
));
}}
buttonProps={{ "aria-haspopup": "dialog" }}
>
);
};
export default definePlugin({
name: "SendTimestamps",
description: "Send timestamps easily via chat box button & text shortcuts. Read the extended description!",
authors: [Devs.Ven, Devs.Tyler, Devs.Grzesiek11],
dependencies: ["MessageEventsAPI", "ChatInputButtonAPI"],
settings,
start() {
addChatBarButton("SendTimestamps", ChatBarIcon);
this.listener = addPreSendListener((_, msg) => {
if (settings.store.replaceMessageContents) {
msg.content = msg.content.replace(/`\d{1,2}:\d{2} ?(?:AM|PM)?`/gi, parseTime);
}
});
},
stop() {
removeChatBarButton("SendTimestamps");
removePreSendListener(this.listener);
},
settingsAboutComponent() {
const samples = [
"12:00",
"3:51",
"17:59",
"24:00",
"12:00 AM",
"0:13PM"
].map(s => `\`${s}\``);
return (
<>
To quickly send send time only timestamps, include timestamps formatted as `HH:MM` (including the backticks!) in your message
See below for examples.
If you need anything more specific, use the Date button in the chat bar!
Examples: