Equicord/src/plugins/messageClickActions/index.ts

191 lines
7.1 KiB
TypeScript
Raw Normal View History

2022-10-22 01:17:06 +02:00
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 Vendicated and contributors
2022-10-22 01:17:06 +02:00
*
* 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 definePlugin, { OptionType } from "@utils/types";
import { ApplicationIntegrationType, MessageFlags } from "@vencord/discord-types/enums";
2023-03-19 04:53:00 -03:00
import { findByPropsLazy } from "@webpack";
import { AuthenticationStore, FluxDispatcher, MessageTypeSets, PermissionsBits, PermissionStore, RestAPI, WindowStore } from "@webpack/common";
2022-08-29 22:05:22 +02:00
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: `/channels/${channelId}/messages/${messageId}/reactions/${emojiParam}/@me`
});
} catch (e) {
console.error("[MessageClickActions] Failed to add reaction:", e);
}
}
2022-08-29 22:05:22 +02:00
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,
2022-08-29 22:05:22 +02:00
start() {
document.addEventListener("keydown", keydown);
document.addEventListener("keyup", keyup);
WindowStore.addChangeListener(focusChanged);
},
2022-08-29 22:05:22 +02:00
stop() {
document.removeEventListener("keydown", keydown);
document.removeEventListener("keyup", keyup);
WindowStore.removeChangeListener(focusChanged);
},
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);
2022-08-29 22:05:22 +02:00
}
pendingDoubleClickAction = executeDoubleClick;
doubleClickTimeout = setTimeout(() => {
pendingDoubleClickAction?.();
pendingDoubleClickAction = null;
doubleClickTimeout = null;
}, 300);
event.preventDefault();
} else {
executeDoubleClick();
event.preventDefault();
}
},
2022-09-16 22:59:34 +02:00
});