2022-10-21 23:17:06 +00:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 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/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-28 12:37:55 +00:00
|
|
|
import Logger from "@utils/Logger";
|
|
|
|
import { MessageStore } from "@webpack/common";
|
2022-11-08 17:09:11 +00:00
|
|
|
import type { Channel, Message } from "discord-types/general";
|
2023-03-25 02:54:20 +00:00
|
|
|
import type { Promisable } from "type-fest";
|
2022-10-22 16:18:41 +00:00
|
|
|
|
2022-08-31 18:53:36 +00:00
|
|
|
const MessageEventsLogger = new Logger("MessageEvents", "#e5c890");
|
|
|
|
|
2022-08-31 20:08:05 +00:00
|
|
|
export interface Emoji {
|
2022-08-31 18:53:36 +00:00
|
|
|
require_colons: boolean,
|
|
|
|
originalName: string,
|
2022-08-31 20:08:05 +00:00
|
|
|
animated: boolean;
|
2022-08-31 18:53:36 +00:00
|
|
|
guildId: string,
|
|
|
|
name: string,
|
|
|
|
url: string,
|
|
|
|
id: string,
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:04:18 +00:00
|
|
|
export interface MessageObject {
|
2022-08-31 18:53:36 +00:00
|
|
|
content: string,
|
2022-08-31 20:08:05 +00:00
|
|
|
validNonShortcutEmojis: Emoji[];
|
2023-05-02 00:55:21 +00:00
|
|
|
invalidEmojis: any[];
|
|
|
|
tts: boolean;
|
2022-08-31 18:53:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 00:55:21 +00:00
|
|
|
export interface Upload {
|
|
|
|
classification: string;
|
|
|
|
currentSize: number;
|
|
|
|
description: string | null;
|
|
|
|
filename: string;
|
|
|
|
id: string;
|
|
|
|
isImage: boolean;
|
|
|
|
isVideo: boolean;
|
|
|
|
item: {
|
|
|
|
file: File;
|
|
|
|
platform: number;
|
|
|
|
};
|
|
|
|
loaded: number;
|
|
|
|
mimeType: string;
|
|
|
|
preCompressionSize: number;
|
|
|
|
responseUrl: string;
|
|
|
|
sensitive: boolean;
|
|
|
|
showLargeMessageDialog: boolean;
|
|
|
|
spoiler: boolean;
|
|
|
|
status: "NOT_STARTED" | "STARTED" | "UPLOADING" | "ERROR" | "COMPLETED" | "CANCELLED";
|
|
|
|
uniqueId: string;
|
|
|
|
uploadedFilename: string;
|
|
|
|
}
|
2022-11-07 21:23:34 +00:00
|
|
|
export interface MessageExtra {
|
|
|
|
stickerIds?: string[];
|
2023-05-02 00:55:21 +00:00
|
|
|
uploads?: Upload[];
|
2022-11-07 21:23:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 02:54:20 +00:00
|
|
|
export type SendListener = (channelId: string, messageObj: MessageObject, extra: MessageExtra) => Promisable<void | { cancel: boolean; }>;
|
|
|
|
export type EditListener = (channelId: string, messageId: string, messageObj: MessageObject) => Promisable<void>;
|
2022-08-31 18:53:36 +00:00
|
|
|
|
|
|
|
const sendListeners = new Set<SendListener>();
|
|
|
|
const editListeners = new Set<EditListener>();
|
|
|
|
|
2023-05-02 00:55:21 +00:00
|
|
|
export async function _handlePreSend(channelId: string, messageObj: MessageObject, extra: MessageExtra, uploads: Upload[]) {
|
|
|
|
extra.uploads = uploads;
|
2022-08-31 18:53:36 +00:00
|
|
|
for (const listener of sendListeners) {
|
|
|
|
try {
|
2023-03-25 02:54:20 +00:00
|
|
|
const result = await listener(channelId, messageObj, extra);
|
2022-11-07 21:23:34 +00:00
|
|
|
if (result && result.cancel === true) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-08 17:09:11 +00:00
|
|
|
} catch (e) {
|
|
|
|
MessageEventsLogger.error("MessageSendHandler: Listener encountered an unknown error\n", e);
|
|
|
|
}
|
2022-08-31 18:53:36 +00:00
|
|
|
}
|
2022-11-07 21:23:34 +00:00
|
|
|
return false;
|
2022-08-31 18:53:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 02:54:20 +00:00
|
|
|
export async function _handlePreEdit(channelId: string, messageId: string, messageObj: MessageObject) {
|
2022-08-31 18:53:36 +00:00
|
|
|
for (const listener of editListeners) {
|
|
|
|
try {
|
2023-03-25 02:54:20 +00:00
|
|
|
await listener(channelId, messageId, messageObj);
|
2022-11-08 17:09:11 +00:00
|
|
|
} catch (e) {
|
|
|
|
MessageEventsLogger.error("MessageEditHandler: Listener encountered an unknown error\n", e);
|
|
|
|
}
|
2022-08-31 18:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: This event fires off before a message is sent, allowing you to edit the message.
|
|
|
|
*/
|
2022-08-31 20:08:05 +00:00
|
|
|
export function addPreSendListener(listener: SendListener) {
|
|
|
|
sendListeners.add(listener);
|
|
|
|
return listener;
|
|
|
|
}
|
2022-08-31 18:53:36 +00:00
|
|
|
/**
|
|
|
|
* Note: This event fires off before a message's edit is applied, allowing you to further edit the message.
|
|
|
|
*/
|
2022-08-31 20:08:05 +00:00
|
|
|
export function addPreEditListener(listener: EditListener) {
|
|
|
|
editListeners.add(listener);
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
export function removePreSendListener(listener: SendListener) {
|
|
|
|
return sendListeners.delete(listener);
|
|
|
|
}
|
|
|
|
export function removePreEditListener(listener: EditListener) {
|
|
|
|
return editListeners.delete(listener);
|
|
|
|
}
|
|
|
|
|
2022-08-31 18:53:36 +00:00
|
|
|
|
|
|
|
// Message clicks
|
|
|
|
type ClickListener = (message: Message, channel: Channel, event: MouseEvent) => void;
|
|
|
|
|
|
|
|
const listeners = new Set<ClickListener>();
|
|
|
|
|
2022-10-09 20:58:08 +00:00
|
|
|
export function _handleClick(message: Message, channel: Channel, event: MouseEvent) {
|
2022-11-08 17:09:11 +00:00
|
|
|
// message object may be outdated, so (try to) fetch latest one
|
|
|
|
message = MessageStore.getMessage(channel.id, message.id) ?? message;
|
2022-08-31 18:53:36 +00:00
|
|
|
for (const listener of listeners) {
|
|
|
|
try {
|
|
|
|
listener(message, channel, event);
|
2022-11-08 17:09:11 +00:00
|
|
|
} catch (e) {
|
|
|
|
MessageEventsLogger.error("MessageClickHandler: Listener encountered an unknown error\n", e);
|
|
|
|
}
|
2022-08-31 18:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addClickListener(listener: ClickListener) {
|
|
|
|
listeners.add(listener);
|
2022-08-31 20:08:05 +00:00
|
|
|
return listener;
|
2022-08-31 18:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removeClickListener(listener: ClickListener) {
|
|
|
|
return listeners.delete(listener);
|
2022-09-16 20:59:34 +00:00
|
|
|
}
|