aboutsummaryrefslogtreecommitdiff
path: root/src/api/MessageEvents.ts
blob: 9ee54a6a4bbb5e604670e9c4de866be14f76456b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { Message, Channel } from 'discord-types/general';
import Logger from '../utils/logger';

const MessageEventsLogger = new Logger("MessageEvents", "#e5c890");

interface Emoji {
    require_colons: boolean,
    originalName: string,
    animated: boolean
    guildId: string,
    name: string,
    url: string,
    id: string,
}

interface MessageObject {
    content: string,
    validNonShortcutEmojis: Emoji[]
}

type SendListener = (channelId: string, messageObj: MessageObject, extra: any) => void;
type EditListener = (channelId: string, messageId: string, messageObj: MessageObject) => void;

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})`) }
    }
}

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})`) }
    }
}

/**
 * Note: This event fires off before a message is sent, allowing you to edit the message.
 */
export function addPreSendListener(listener: SendListener) { sendListeners.add(listener) }
/**
 * 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) }
export function removePreSendListener(listener: SendListener) { sendListeners.delete(listener) }
export function removePreEditListener(listener: EditListener) { editListeners.delete(listener) }

// Message clicks
type ClickListener = (message: Message, channel: Channel, event: MouseEvent) => void;

const listeners = new Set<ClickListener>();

export function _handleClick(message, channel, event) {
    for (const listener of listeners) {
        try {
            listener(message, channel, event);
        } catch (e) { MessageEventsLogger.error(`MessageClickHandler: Listener encoutered an unknown error. (${e})`) }
    }
}

export function addClickListener(listener: ClickListener) {
    listeners.add(listener);
}

export function removeClickListener(listener: ClickListener) {
    return listeners.delete(listener);
}