feat(api): Message Accessories API (#131)
This commit is contained in:
parent
4b1e96b76e
commit
e2b622c76b
41
src/api/MessageAccessories.ts
Normal file
41
src/api/MessageAccessories.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
export type AccessoryCallback = (props: Record<string, any>) => JSX.Element;
|
||||||
|
export type Accessory = {
|
||||||
|
callback: AccessoryCallback;
|
||||||
|
position?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const accessories = new Map<String, Accessory>();
|
||||||
|
|
||||||
|
export function addAccessory(
|
||||||
|
identifier: string,
|
||||||
|
callback: AccessoryCallback,
|
||||||
|
position?: number
|
||||||
|
) {
|
||||||
|
accessories.set(identifier, {
|
||||||
|
callback,
|
||||||
|
position,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeAccessory(identifier: string) {
|
||||||
|
accessories.delete(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function _modifyAccessories(
|
||||||
|
elements: JSX.Element[],
|
||||||
|
props: Record<string, any>
|
||||||
|
) {
|
||||||
|
for (const accessory of accessories.values()) {
|
||||||
|
elements.splice(
|
||||||
|
accessory.position != null
|
||||||
|
? accessory.position < 0
|
||||||
|
? elements.length + accessory.position
|
||||||
|
: accessory.position
|
||||||
|
: elements.length,
|
||||||
|
0,
|
||||||
|
accessory.callback(props)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return elements;
|
||||||
|
}
|
@ -6,6 +6,7 @@ import * as $MessageEventsAPI from "./MessageEvents";
|
|||||||
import * as $Notices from "./Notices";
|
import * as $Notices from "./Notices";
|
||||||
import * as $Commands from "./Commands";
|
import * as $Commands from "./Commands";
|
||||||
import * as $DataStore from "./DataStore";
|
import * as $DataStore from "./DataStore";
|
||||||
|
import * as $MessageAccessories from "./MessageAccessories";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An API allowing you to listen to Message Clicks or run your own logic
|
* An API allowing you to listen to Message Clicks or run your own logic
|
||||||
@ -38,10 +39,9 @@ const Commands = $Commands;
|
|||||||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types}
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types}
|
||||||
*/
|
*/
|
||||||
const DataStore = $DataStore;
|
const DataStore = $DataStore;
|
||||||
|
/**
|
||||||
|
* An API allowing you to add custom components as message accessories
|
||||||
|
*/
|
||||||
|
const MessageAccessories = $MessageAccessories;
|
||||||
|
|
||||||
export {
|
export { DataStore, MessageAccessories, MessageEvents, Notices, Commands };
|
||||||
DataStore,
|
|
||||||
MessageEvents,
|
|
||||||
Notices,
|
|
||||||
Commands
|
|
||||||
};
|
|
||||||
|
18
src/plugins/apiMessageAccessories.ts
Normal file
18
src/plugins/apiMessageAccessories.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { Devs } from "../utils/constants";
|
||||||
|
import definePlugin from "../utils/types";
|
||||||
|
|
||||||
|
export default definePlugin({
|
||||||
|
name: "MessageAccessoriesAPI",
|
||||||
|
description: "API to add message accessories.",
|
||||||
|
authors: [Devs.Cyn],
|
||||||
|
patches: [
|
||||||
|
{
|
||||||
|
find: "_messageAttachmentToEmbedMedia",
|
||||||
|
replacement: {
|
||||||
|
match: /\(\)\.container\)},(.+?)\)};return/,
|
||||||
|
replace: (_, accessories) =>
|
||||||
|
`().container)},Vencord.Api.MessageAccessories._modifyAccessories([${accessories}],this.props))};return`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user