Vencord/src/plugins/messageActions.ts

44 lines
1.7 KiB
TypeScript
Raw Normal View History

import { addClickListener, removeClickListener } from '../api/MessageEvents';
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",
author: "Vendicated",
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");
const { Permissions: { MANAGE_MESSAGES } } = find(m => m.Permissions?.MANAGE_MESSAGES);
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
}
});