Vencord/src/plugins/moyai.ts

106 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-10-09 16:18:18 +00:00
import definePlugin from "../utils/types";
import { Devs } from "../utils/constants";
2022-10-09 17:48:22 +00:00
import { Message, ReactionEmoji } from "discord-types/general";
import { FluxDispatcher, SelectedChannelStore } from "../webpack/common";
import { sleep } from "../utils/misc";
2022-10-09 16:18:18 +00:00
interface IMessageCreate {
type: "MESSAGE_CREATE";
optimistic: boolean;
2022-10-09 17:48:22 +00:00
isPushNotification: boolean;
channelId: string;
2022-10-09 16:18:18 +00:00
message: Message;
}
2022-10-09 17:48:22 +00:00
interface IReactionAdd {
type: "MESSAGE_REACTION_ADD";
optimistic: boolean;
channelId: string;
messageId: string;
userId: "195136840355807232";
emoji: ReactionEmoji;
}
const MOYAI = "🗿";
2022-10-09 16:18:18 +00:00
const MOYAI_URL =
"https://raw.githubusercontent.com/MeguminSama/VencordPlugins/main/plugins/moyai/moyai.mp3";
2022-10-09 16:18:18 +00:00
2022-10-09 17:48:22 +00:00
// Implement once Settings are a thing
const ignoreBots = true;
2022-10-09 16:18:18 +00:00
export default definePlugin({
name: "Moyai",
authors: [Devs.Megu],
description: "🗿🗿🗿🗿🗿🗿🗿🗿",
2022-10-09 17:48:22 +00:00
async onMessage(e: IMessageCreate) {
if (e.optimistic || e.type !== "MESSAGE_CREATE") return;
if (e.message.state === "SENDING") return;
if (ignoreBots && e.message.author?.bot) return;
if (!e.message.content) return;
if (e.channelId !== SelectedChannelStore.getChannelId()) return;
2022-10-09 16:18:18 +00:00
2022-10-09 17:48:22 +00:00
const moyaiCount = getMoyaiCount(e.message.content);
2022-10-09 16:18:18 +00:00
for (let i = 0; i < moyaiCount; i++) {
2022-10-09 17:48:22 +00:00
boom();
await sleep(300);
2022-10-09 16:18:18 +00:00
}
},
2022-10-09 17:48:22 +00:00
onReaction(e: IReactionAdd) {
if (e.optimistic || e.type !== "MESSAGE_REACTION_ADD") return;
if (e.channelId !== SelectedChannelStore.getChannelId()) return;
const name = e.emoji.name.toLowerCase();
if (name !== MOYAI && !name.includes("moyai") && !name.includes("moai")) return;
boom();
},
2022-10-09 16:27:50 +00:00
start() {
2022-10-09 17:48:22 +00:00
FluxDispatcher.subscribe("MESSAGE_CREATE", this.onMessage);
FluxDispatcher.subscribe("MESSAGE_REACTION_ADD", this.onReaction);
2022-10-09 16:27:50 +00:00
},
2022-10-09 17:48:22 +00:00
2022-10-09 16:27:50 +00:00
stop() {
2022-10-09 17:48:22 +00:00
FluxDispatcher.unsubscribe("MESSAGE_CREATE", this.onMessage);
FluxDispatcher.unsubscribe("MESSAGE_REACTION_ADD", this.onReaction);
},
2022-10-09 16:18:18 +00:00
});
2022-10-09 17:48:22 +00:00
function countOccurrences(sourceString: string, subString: string) {
let i = 0;
let lastIdx = 0;
while ((lastIdx = sourceString.indexOf(subString, lastIdx) + 1) !== 0)
i++;
return i;
}
2022-10-09 16:18:18 +00:00
2022-10-09 17:48:22 +00:00
function countMatches(sourceString: string, pattern: RegExp) {
if (!pattern.global)
throw new Error("pattern must be global");
2022-10-09 16:18:18 +00:00
2022-10-09 17:48:22 +00:00
let i = 0;
while (pattern.test(sourceString))
i++;
2022-10-09 16:18:18 +00:00
2022-10-09 17:48:22 +00:00
return i;
}
2022-10-09 16:18:18 +00:00
2022-10-09 17:48:22 +00:00
const customMoyaiRe = /<a?:\w*moy?ai\w*:\d{17,20}>/gi;
function getMoyaiCount(message: string) {
let count = countOccurrences(message, MOYAI)
+ countMatches(message, customMoyaiRe);
return Math.min(count, 10);
}
2022-10-09 16:18:18 +00:00
2022-10-09 17:48:22 +00:00
function boom() {
const audioElement = document.createElement("audio");
audioElement.src = MOYAI_URL;
audioElement.play();
2022-10-09 16:18:18 +00:00
}