feat(MessageEvents): Promisable send/edit listeners (#514)
* promisable send/edit listeners * added self * Apply suggestions from code review Co-authored-by: Nuckyz <61953774+Nuckyz@users.noreply.github.com> * fix patches --------- Co-authored-by: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Co-authored-by: Ven <vendicated@riseup.net>
This commit is contained in:
parent
0b4ae729a3
commit
93cb51a975
@ -19,6 +19,7 @@
|
|||||||
import Logger from "@utils/Logger";
|
import Logger from "@utils/Logger";
|
||||||
import { MessageStore } from "@webpack/common";
|
import { MessageStore } from "@webpack/common";
|
||||||
import type { Channel, Message } from "discord-types/general";
|
import type { Channel, Message } from "discord-types/general";
|
||||||
|
import type { Promisable } from "type-fest";
|
||||||
|
|
||||||
const MessageEventsLogger = new Logger("MessageEvents", "#e5c890");
|
const MessageEventsLogger = new Logger("MessageEvents", "#e5c890");
|
||||||
|
|
||||||
@ -41,16 +42,16 @@ export interface MessageExtra {
|
|||||||
stickerIds?: string[];
|
stickerIds?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SendListener = (channelId: string, messageObj: MessageObject, extra: MessageExtra) => void | { cancel: boolean; };
|
export type SendListener = (channelId: string, messageObj: MessageObject, extra: MessageExtra) => Promisable<void | { cancel: boolean; }>;
|
||||||
export type EditListener = (channelId: string, messageId: string, messageObj: MessageObject) => void;
|
export type EditListener = (channelId: string, messageId: string, messageObj: MessageObject) => Promisable<void>;
|
||||||
|
|
||||||
const sendListeners = new Set<SendListener>();
|
const sendListeners = new Set<SendListener>();
|
||||||
const editListeners = new Set<EditListener>();
|
const editListeners = new Set<EditListener>();
|
||||||
|
|
||||||
export function _handlePreSend(channelId: string, messageObj: MessageObject, extra: MessageExtra) {
|
export async function _handlePreSend(channelId: string, messageObj: MessageObject, extra: MessageExtra) {
|
||||||
for (const listener of sendListeners) {
|
for (const listener of sendListeners) {
|
||||||
try {
|
try {
|
||||||
const result = listener(channelId, messageObj, extra);
|
const result = await listener(channelId, messageObj, extra);
|
||||||
if (result && result.cancel === true) {
|
if (result && result.cancel === true) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -61,10 +62,10 @@ export function _handlePreSend(channelId: string, messageObj: MessageObject, ext
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function _handlePreEdit(channelId: string, messageId: string, messageObj: MessageObject) {
|
export async function _handlePreEdit(channelId: string, messageId: string, messageObj: MessageObject) {
|
||||||
for (const listener of editListeners) {
|
for (const listener of editListeners) {
|
||||||
try {
|
try {
|
||||||
listener(channelId, messageId, messageObj);
|
await listener(channelId, messageId, messageObj);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
MessageEventsLogger.error("MessageEditHandler: Listener encountered an unknown error\n", e);
|
MessageEventsLogger.error("MessageEditHandler: Listener encountered an unknown error\n", e);
|
||||||
}
|
}
|
||||||
|
@ -22,22 +22,22 @@ import definePlugin from "@utils/types";
|
|||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
name: "MessageEventsAPI",
|
name: "MessageEventsAPI",
|
||||||
description: "Api required by anything using message events.",
|
description: "Api required by anything using message events.",
|
||||||
authors: [Devs.Arjix],
|
authors: [Devs.Arjix, Devs.hunt],
|
||||||
patches: [
|
patches: [
|
||||||
{
|
{
|
||||||
find: "sendMessage:function",
|
find: '"MessageActionCreators"',
|
||||||
replacement: [{
|
replacement: [{
|
||||||
match: /(?<=_sendMessage:function\([^)]+\)){/,
|
match: /_sendMessage:(function\([^)]+\)){/,
|
||||||
replace: "{if(Vencord.Api.MessageEvents._handlePreSend(...arguments)){return;};"
|
replace: "_sendMessage:async $1{if(await Vencord.Api.MessageEvents._handlePreSend(...arguments))return;"
|
||||||
}, {
|
}, {
|
||||||
match: /(?<=\beditMessage:function\([^)]+\)){/,
|
match: /\beditMessage:(function\([^)]+\)){/,
|
||||||
replace: "{Vencord.Api.MessageEvents._handlePreEdit(...arguments);"
|
replace: "editMessage:async $1{await Vencord.Api.MessageEvents._handlePreEdit(...arguments);"
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
find: '("interactionUsernameProfile',
|
find: '("interactionUsernameProfile',
|
||||||
replacement: {
|
replacement: {
|
||||||
match: /var \w=(\w)\.id,\w=(\w)\.id;return .{1,2}\.useCallback\(\(?function\((.{1,2})\){/,
|
match: /var \i=(\i)\.id,\i=(\i)\.id;return \i\.useCallback\(\(?function\((\i)\){/,
|
||||||
replace: (m, message, channel, event) =>
|
replace: (m, message, channel, event) =>
|
||||||
// the message param is shadowed by the event param, so need to alias them
|
// the message param is shadowed by the event param, so need to alias them
|
||||||
`var _msg=${message},_chan=${channel};${m}Vencord.Api.MessageEvents._handleClick(_msg, _chan, ${event});`
|
`var _msg=${message},_chan=${channel};${m}Vencord.Api.MessageEvents._handleClick(_msg, _chan, ${event});`
|
||||||
|
@ -202,6 +202,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({
|
|||||||
name: "lewisakura",
|
name: "lewisakura",
|
||||||
id: 96269247411400704n
|
id: 96269247411400704n
|
||||||
},
|
},
|
||||||
|
hunt: {
|
||||||
|
name: "hunt-g",
|
||||||
|
id: 222800179697287168n
|
||||||
|
},
|
||||||
cloudburst: {
|
cloudburst: {
|
||||||
name: "cloudburst",
|
name: "cloudburst",
|
||||||
id: 892128204150685769n
|
id: 892128204150685769n
|
||||||
|
Loading…
Reference in New Issue
Block a user