Vencord/src/api/MessageEvents.ts

104 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-10-21 19:17:06 -04: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/>.
*/
import type { Message, Channel } from "discord-types/general";
import Logger from "../utils/logger";
2022-08-31 14:53:36 -04:00
const MessageEventsLogger = new Logger("MessageEvents", "#e5c890");
export interface Emoji {
2022-08-31 14:53:36 -04:00
require_colons: boolean,
originalName: string,
animated: boolean;
2022-08-31 14:53:36 -04:00
guildId: string,
name: string,
url: string,
id: string,
}
export interface MessageObject {
2022-08-31 14:53:36 -04:00
content: string,
validNonShortcutEmojis: Emoji[];
2022-08-31 14:53:36 -04:00
}
export type SendListener = (channelId: string, messageObj: MessageObject, extra: any) => void;
export type EditListener = (channelId: string, messageId: string, messageObj: MessageObject) => void;
2022-08-31 14:53:36 -04:00
const sendListeners = new Set<SendListener>();
const editListeners = new Set<EditListener>();
export function _handlePreSend(channelId: string, messageObj: MessageObject, extra: any) {
for (const listener of sendListeners) {
try {
listener(channelId, messageObj, extra);
} catch (e) { MessageEventsLogger.error(`MessageSendHandler: Listener encoutered an unknown error. (${e})`); }
2022-08-31 14:53:36 -04:00
}
}
export function _handlePreEdit(channeld: string, messageId: string, messageObj: MessageObject) {
for (const listener of editListeners) {
try {
listener(channeld, messageId, messageObj);
} catch (e) { MessageEventsLogger.error(`MessageEditHandler: Listener encoutered an unknown error. (${e})`); }
2022-08-31 14:53:36 -04:00
}
}
/**
* Note: This event fires off before a message is sent, allowing you to edit the message.
*/
export function addPreSendListener(listener: SendListener) {
sendListeners.add(listener);
return listener;
}
2022-08-31 14:53:36 -04:00
/**
* Note: This event fires off before a message's edit is applied, allowing you to further edit the message.
*/
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 14:53:36 -04:00
// Message clicks
type ClickListener = (message: Message, channel: Channel, event: MouseEvent) => void;
const listeners = new Set<ClickListener>();
export function _handleClick(message: Message, channel: Channel, event: MouseEvent) {
2022-08-31 14:53:36 -04:00
for (const listener of listeners) {
try {
listener(message, channel, event);
} catch (e) { MessageEventsLogger.error(`MessageClickHandler: Listener encoutered an unknown error. (${e})`); }
2022-08-31 14:53:36 -04:00
}
}
export function addClickListener(listener: ClickListener) {
listeners.add(listener);
return listener;
2022-08-31 14:53:36 -04:00
}
export function removeClickListener(listener: ClickListener) {
return listeners.delete(listener);
2022-09-16 16:59:34 -04:00
}