From 2d779ba744831d309798cd791e022ea3c6f342be Mon Sep 17 00:00:00 2001 From: newwares Date: Wed, 13 Mar 2024 14:36:32 -0400 Subject: [PATCH] it works --- src/plugins/findReply/index.tsx | 109 ++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/plugins/findReply/index.tsx diff --git a/src/plugins/findReply/index.tsx b/src/plugins/findReply/index.tsx new file mode 100644 index 000000000..cef7a9fac --- /dev/null +++ b/src/plugins/findReply/index.tsx @@ -0,0 +1,109 @@ +/* + * 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 { addButton, removeButton } from "@api/MessagePopover"; +import { Devs } from "@utils/constants"; +import definePlugin, { OptionType } from "@utils/types"; +import { findByPropsLazy } from "@webpack"; +import { ChannelStore, MessageStore, Toasts } from "@webpack/common"; +import Message from "discord-types/general/Message"; + + +const jumper = findByPropsLazy("jumpToMessage"); +const FindReplyIcon = () => { + return ; +}; + +export default definePlugin({ + name: "FindReply", + description: "Jumps to the earliest reply to a message in a channel (lets you follow past conversations more easily).", + authors: [Devs.newwares], + start() { + addButton("vc-findreply", message => { + if (!message.id) return null; + return { + label: "Jump to Reply", + icon: FindReplyIcon, + message, + channel: ChannelStore.getChannel(message.channel_id), + onClick: async () => { + const messages: Array = [...MessageStore.getMessages(message.channel_id)?._array ?? []].filter(m => !m.deleted).sort((a, b) => { + return b.timestamp.toString().localeCompare(a.timestamp.toString()); + }); // Need to deep copy Message array when sorting + console.log(messages); + let reply: Message | null = null; + for (const other of messages) { + if (other.timestamp.toString().localeCompare(message.timestamp.toString()) <= 0) continue; + if (other.messageReference?.message_id === message.id) { + reply = other; + break; + } + if (Vencord.Settings.plugins.FindReply.includePings) { + if (other.content?.includes(`<@${message.author.id}>`)) { + reply = other; + break; + } + } + if (Vencord.Settings.plugins.FindReply.includeAuthor) { + if (messages.find(m => m.id === other.messageReference?.message_id)?.author.id === message.author.id) { + reply = other; + break; + } + } + + } + if (reply) { + const channelId = reply.channel_id; + const messageId = reply.id; + jumper.jumpToMessage({ + channelId, + messageId, + flash: true, + jumpType: "INSTANT" + }); + } else { + Toasts.show({ + id: Toasts.genId(), + message: "Couldn't find a reply.", + type: Toasts.Type.FAILURE + }); + } + } + }; + }); + }, + stop() { + removeButton("vc-findreply"); + }, + options: { + includePings: { + type: OptionType.BOOLEAN, + description: "Will also search for messages that @ the author directly", + default: false, + restartNeeded: false + }, + includeAuthor: { + type: OptionType.BOOLEAN, + description: "Will also search for messages that reply to the author in general, not just that exact message", + default: false, + restartNeeded: false + } + } +});