Vencord/src/plugins/messageActions.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

import { addClickListener, removeClickListener } from '../api/MessageEvents';
2022-08-29 20:05:22 +00:00
import definePlugin from "../utils/types";
2022-08-31 02:07:16 +00:00
import { find, findByProps } from "../webpack";
2022-08-29 20:05:22 +00:00
let isDeletePressed = false;
const keydown = (e: KeyboardEvent) => e.key === "Backspace" && (isDeletePressed = true);
const keyup = (e: KeyboardEvent) => e.key === "Backspace" && (isDeletePressed = false);
2022-08-29 20:05:22 +00:00
export default definePlugin({
name: "MessageQuickActions",
description: "Quick Delete, Quick edit",
author: "Vendicated",
2022-08-31 18:58:21 +00:00
dependencies: ["MessageEventsAPI"],
2022-08-29 20:05:22 +00:00
start() {
const { deleteMessage, startEditMessage } = findByProps("deleteMessage");
const { can } = findByProps("can", "initialize");
const { Permissions: { MANAGE_MESSAGES } } = find(m => m.Permissions?.MANAGE_MESSAGES);
const { getCurrentUser } = findByProps("getCurrentUser");
document.addEventListener("keydown", keydown);
document.addEventListener("keyup", keyup);
2022-08-29 20:05:22 +00:00
this.onClick = addClickListener((msg, chan, event) => {
2022-08-29 20:05:22 +00:00
const isMe = msg.author.id === getCurrentUser().id;
if (!isDeletePressed) {
if (isMe && event.detail >= 2) {
startEditMessage(chan.id, msg.id, msg.content);
event.preventDefault();
}
} else if (isMe || can(MANAGE_MESSAGES, chan)) {
deleteMessage(chan.id, msg.id);
event.preventDefault();
}
});
},
stop() {
removeClickListener(this.onClick);
document.removeEventListener("keydown", keydown);
document.removeEventListener("keyup", keyup);
2022-08-29 20:05:22 +00:00
}
});