Equicord/src/plugins/messageClickActions/index.ts
Keiran 038aa10bbf
MessageClickActions: Misc Fixes (#597)
* MessageClickActions: add optional triple click action for reacting to messages

* fix(MessageClickActions): handle API failures gracefully and log errors

* fix(MessageClickActions): regex now matches emoji naming requirements

* Fix formatting of IDs in constants file

* fix(MessageClickActions): remove unnecessary selection clear after triple click action

* fix(MessageClickActions): add cleanup, use Constants.Endpoints, and replace console.error with Logger

* fix css

---------

Co-authored-by: thororen <78185467+thororen1234@users.noreply.github.com>
2025-12-25 15:55:09 -05:00

197 lines
7.3 KiB
TypeScript

/*
* 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 <https://www.gnu.org/licenses/>.
*/
import { isPluginEnabled } from "@api/PluginManager";
import { definePluginSettings } from "@api/Settings";
import NoReplyMentionPlugin from "@plugins/noReplyMention";
import { Devs, EquicordDevs } from "@utils/constants";
import { Logger } from "@utils/Logger";
import definePlugin, { OptionType } from "@utils/types";
import { ApplicationIntegrationType, MessageFlags } from "@vencord/discord-types/enums";
import { findByPropsLazy } from "@webpack";
import { AuthenticationStore, Constants, FluxDispatcher, MessageTypeSets, PermissionsBits, PermissionStore, RestAPI, WindowStore } from "@webpack/common";
const MessageActions = findByPropsLazy("deleteMessage", "startEditMessage");
const EditStore = findByPropsLazy("isEditing", "isEditingAny");
let isDeletePressed = false;
const keydown = (e: KeyboardEvent) => e.key === "Backspace" && (isDeletePressed = true);
const keyup = (e: KeyboardEvent) => e.key === "Backspace" && (isDeletePressed = false);
const focusChanged = () => !WindowStore.isFocused() && (isDeletePressed = false);
let doubleClickTimeout: ReturnType<typeof setTimeout> | null = null;
let pendingDoubleClickAction: (() => void) | null = null;
const settings = definePluginSettings({
enableDeleteOnClick: {
type: OptionType.BOOLEAN,
description: "Enable delete on click while holding backspace",
default: true
},
enableDoubleClickToEdit: {
type: OptionType.BOOLEAN,
description: "Enable double click to edit",
default: true
},
enableDoubleClickToReply: {
type: OptionType.BOOLEAN,
description: "Enable double click to reply",
default: true
},
enableTripleClickToReact: {
type: OptionType.BOOLEAN,
description: "Enable triple click to react with an emoji",
default: false
},
reactEmoji: {
type: OptionType.STRING,
description: "Emoji to react with (e.g. 💀 or pepe:123456789)",
default: "💀"
},
requireModifier: {
type: OptionType.BOOLEAN,
description: "Only do double click actions when shift/ctrl is held",
default: false
}
});
async function react(channelId: string, messageId: string, emoji: string) {
const trimmed = emoji.trim();
if (!trimmed) return;
const customMatch = trimmed.match(/^:?([\w-]+):(\d+)$/);
const emojiParam = customMatch
? `${customMatch[1]}:${customMatch[2]}`
: encodeURIComponent(trimmed);
try {
await RestAPI.put({
url: Constants.Endpoints.REACTIONS(channelId, messageId, emojiParam)
});
} catch (e) {
new Logger("MessageClickActions").error("Failed to add reaction:", e);
}
}
export default definePlugin({
name: "MessageClickActions",
description: "Hold Backspace and click to delete, double click to edit/reply, triple click to react",
authors: [Devs.Ven, EquicordDevs.keircn],
settings,
start() {
document.addEventListener("keydown", keydown);
document.addEventListener("keyup", keyup);
WindowStore.addChangeListener(focusChanged);
},
stop() {
document.removeEventListener("keydown", keydown);
document.removeEventListener("keyup", keyup);
WindowStore.removeChangeListener(focusChanged);
if (doubleClickTimeout) {
clearTimeout(doubleClickTimeout);
doubleClickTimeout = null;
}
pendingDoubleClickAction = null;
},
onMessageClick(msg, channel, event) {
const myId = AuthenticationStore.getId();
const isMe = msg.author.id === myId;
const isSelfInvokedUserApp = msg.interactionMetadata?.authorizing_integration_owners[ApplicationIntegrationType.USER_INSTALL] === myId;
if (isDeletePressed) {
if (!settings.store.enableDeleteOnClick) return;
if (!(isMe || PermissionStore.can(PermissionsBits.MANAGE_MESSAGES, channel) || isSelfInvokedUserApp)) return;
if (msg.deleted) {
FluxDispatcher.dispatch({
type: "MESSAGE_DELETE",
channelId: channel.id,
id: msg.id,
mlDeleted: true
});
} else {
MessageActions.deleteMessage(channel.id, msg.id);
}
event.preventDefault();
return;
}
if (event.detail === 3) {
if (doubleClickTimeout) {
clearTimeout(doubleClickTimeout);
doubleClickTimeout = null;
pendingDoubleClickAction = null;
}
if (settings.store.enableTripleClickToReact) {
react(channel.id, msg.id, settings.store.reactEmoji);
event.preventDefault();
}
return;
}
if (event.detail !== 2) return;
if (settings.store.requireModifier && !event.ctrlKey && !event.shiftKey) return;
if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return;
if (msg.deleted === true) return;
const executeDoubleClick = () => {
if (isMe) {
if (!settings.store.enableDoubleClickToEdit || EditStore.isEditing(channel.id, msg.id) || msg.state !== "SENT") return;
MessageActions.startEditMessage(channel.id, msg.id, msg.content);
} else {
if (!settings.store.enableDoubleClickToReply) return;
if (!MessageTypeSets.REPLYABLE.has(msg.type) || msg.hasFlag(MessageFlags.EPHEMERAL)) return;
const isShiftPress = event.shiftKey && !settings.store.requireModifier;
const shouldMention = isPluginEnabled(NoReplyMentionPlugin.name)
? NoReplyMentionPlugin.shouldMention(msg, isShiftPress)
: !isShiftPress;
FluxDispatcher.dispatch({
type: "CREATE_PENDING_REPLY",
channel,
message: msg,
shouldMention,
showMentionToggle: channel.guild_id !== null
});
}
};
if (settings.store.enableTripleClickToReact) {
if (doubleClickTimeout) {
clearTimeout(doubleClickTimeout);
}
pendingDoubleClickAction = executeDoubleClick;
doubleClickTimeout = setTimeout(() => {
pendingDoubleClickAction?.();
pendingDoubleClickAction = null;
doubleClickTimeout = null;
}, 300);
event.preventDefault();
} else {
executeDoubleClick();
event.preventDefault();
}
},
});