Moyai: Support Reactions, ignore bots
This commit is contained in:
parent
43951456d3
commit
bb7332cefd
1 changed files with 77 additions and 47 deletions
|
@ -1,75 +1,105 @@
|
||||||
import definePlugin from "../utils/types";
|
import definePlugin from "../utils/types";
|
||||||
import { Devs } from "../utils/constants";
|
import { Devs } from "../utils/constants";
|
||||||
import { Message } from "discord-types/general";
|
import { Message, ReactionEmoji } from "discord-types/general";
|
||||||
import { FluxDispatcher } from "../webpack/common";
|
import { FluxDispatcher, SelectedChannelStore } from "../webpack/common";
|
||||||
|
import { sleep } from "../utils/misc";
|
||||||
|
|
||||||
interface IMessageCreate {
|
interface IMessageCreate {
|
||||||
type: "MESSAGE_CREATE";
|
type: "MESSAGE_CREATE";
|
||||||
channelId: string;
|
|
||||||
isPushNotification: boolean;
|
|
||||||
optimistic: boolean;
|
optimistic: boolean;
|
||||||
|
isPushNotification: boolean;
|
||||||
|
channelId: string;
|
||||||
message: Message;
|
message: Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IReactionAdd {
|
||||||
|
type: "MESSAGE_REACTION_ADD";
|
||||||
|
optimistic: boolean;
|
||||||
|
channelId: string;
|
||||||
|
messageId: string;
|
||||||
|
userId: "195136840355807232";
|
||||||
|
emoji: ReactionEmoji;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MOYAI = "🗿";
|
||||||
const MOYAI_URL =
|
const MOYAI_URL =
|
||||||
"https://github.com/MeguminSama/VencordPlugins/raw/main/plugins/moyai/moyai.mp3";
|
"https://github.com/MeguminSama/VencordPlugins/raw/main/plugins/moyai/moyai.mp3";
|
||||||
|
|
||||||
|
// Implement once Settings are a thing
|
||||||
|
const ignoreBots = true;
|
||||||
|
|
||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
name: "Moyai",
|
name: "Moyai",
|
||||||
authors: [Devs.Megu],
|
authors: [Devs.Megu],
|
||||||
description: "🗿🗿🗿🗿🗿🗿🗿🗿",
|
description: "🗿🗿🗿🗿🗿🗿🗿🗿",
|
||||||
execute: async (event: IMessageCreate) => {
|
|
||||||
if (event?.type !== "MESSAGE_CREATE") return;
|
|
||||||
if (!event.message?.content) return;
|
|
||||||
if (event.message.state === "SENDING") return;
|
|
||||||
if (event.optimistic) return;
|
|
||||||
|
|
||||||
const isInChannel =
|
async onMessage(e: IMessageCreate) {
|
||||||
window.location.pathname.startsWith("/channels/");
|
if (e.optimistic || e.type !== "MESSAGE_CREATE") return;
|
||||||
if (!isInChannel) 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;
|
||||||
|
|
||||||
const channelId = window.location.pathname.split("/")[3];
|
const moyaiCount = getMoyaiCount(e.message.content);
|
||||||
if (!channelId || channelId !== event.channelId) return;
|
|
||||||
|
|
||||||
const moyaiCount = messageContainsMoyai(event.message.content);
|
|
||||||
if (!moyaiCount) return;
|
|
||||||
|
|
||||||
for (let i = 0; i < moyaiCount; i++) {
|
for (let i = 0; i < moyaiCount; i++) {
|
||||||
const audioElement = document.createElement("audio");
|
boom();
|
||||||
audioElement.src = MOYAI_URL;
|
await sleep(300);
|
||||||
audioElement.play();
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 300));
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
start() {
|
|
||||||
FluxDispatcher.subscribe("MESSAGE_CREATE", this.execute);
|
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();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
start() {
|
||||||
|
FluxDispatcher.subscribe("MESSAGE_CREATE", this.onMessage);
|
||||||
|
FluxDispatcher.subscribe("MESSAGE_REACTION_ADD", this.onReaction);
|
||||||
|
},
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
FluxDispatcher.unsubscribe("MESSAGE_CREATE", this.execute);
|
FluxDispatcher.unsubscribe("MESSAGE_CREATE", this.onMessage);
|
||||||
}
|
FluxDispatcher.unsubscribe("MESSAGE_REACTION_ADD", this.onReaction);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const EMOJI_NAME_REGEX = /<a?:(\w+):\d+>/g;
|
function countOccurrences(sourceString: string, subString: string) {
|
||||||
|
let i = 0;
|
||||||
|
let lastIdx = 0;
|
||||||
|
while ((lastIdx = sourceString.indexOf(subString, lastIdx) + 1) !== 0)
|
||||||
|
i++;
|
||||||
|
|
||||||
function messageContainsMoyai(message: string): number {
|
return i;
|
||||||
// get number of 🗿 in a string
|
}
|
||||||
let moyaiCount = (message.match(/🗿/g) || []).length;
|
|
||||||
|
function countMatches(sourceString: string, pattern: RegExp) {
|
||||||
// get number of emojis in message that are called "moyai" or "moai"
|
if (!pattern.global)
|
||||||
const emojiNames = message.matchAll(EMOJI_NAME_REGEX);
|
throw new Error("pattern must be global");
|
||||||
|
|
||||||
if (emojiNames) {
|
let i = 0;
|
||||||
for (const emojiName of emojiNames) {
|
while (pattern.test(sourceString))
|
||||||
if (!emojiName[1]) continue;
|
i++;
|
||||||
let name = emojiName[1];
|
|
||||||
|
return i;
|
||||||
// If emoji starts or ends with (moyai|moai)
|
}
|
||||||
if (/^(moyai|moai)/i.test(name) || /(moyai|moai)$/i.test(name)) {
|
|
||||||
moyaiCount++;
|
const customMoyaiRe = /<a?:\w*moy?ai\w*:\d{17,20}>/gi;
|
||||||
}
|
|
||||||
}
|
function getMoyaiCount(message: string) {
|
||||||
}
|
let count = countOccurrences(message, MOYAI)
|
||||||
|
+ countMatches(message, customMoyaiRe);
|
||||||
// Maximum moyai...
|
|
||||||
return Math.min(moyaiCount, 10);
|
return Math.min(count, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
function boom() {
|
||||||
|
const audioElement = document.createElement("audio");
|
||||||
|
audioElement.src = MOYAI_URL;
|
||||||
|
audioElement.play();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue