Vencord/src/plugins/messageActions.ts

46 lines
1.7 KiB
TypeScript
Raw Normal View History

import { addClickListener, removeClickListener } from "../api/MessageEvents";
2022-09-30 20:27:28 -04:00
import { Devs } from "../utils/constants";
2022-08-29 16:05:22 -04:00
import definePlugin from "../utils/types";
2022-08-30 22:07:16 -04:00
import { find, findByProps } from "../webpack";
import { UserStore } from "../webpack/common";
2022-08-29 16:05:22 -04: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 16:05:22 -04:00
export default definePlugin({
name: "MessageQuickActions",
description: "Quick Delete, Quick edit",
2022-09-30 20:27:28 -04:00
authors: [Devs.Ven],
2022-08-31 14:58:21 -04:00
dependencies: ["MessageEventsAPI"],
2022-08-29 16:05:22 -04:00
start() {
const { deleteMessage, startEditMessage } = findByProps("deleteMessage");
const { can } = findByProps("can", "initialize");
2022-09-27 08:34:57 -04:00
const { MANAGE_MESSAGES } = find(m => typeof m.MANAGE_MESSAGES === "bigint");
const { isEditing } = findByProps("isEditing");
2022-08-29 16:05:22 -04:00
document.addEventListener("keydown", keydown);
document.addEventListener("keyup", keyup);
2022-08-29 16:05:22 -04:00
this.onClick = addClickListener((msg, chan, event) => {
const isMe = msg.author.id === UserStore.getCurrentUser().id;
2022-08-29 16:05:22 -04:00
if (!isDeletePressed) {
if (isMe && event.detail >= 2 && !isEditing(chan.id, msg.id)) {
2022-08-29 16:05:22 -04:00
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 16:05:22 -04:00
}
2022-09-16 16:59:34 -04:00
});