Update index.ts
This commit is contained in:
parent
b77421f3ce
commit
0ad2557275
1 changed files with 35 additions and 19 deletions
|
|
@ -2,23 +2,26 @@ import { defineCorePlugin } from "..";
|
|||
import { findByProps } from "@metro";
|
||||
import { logger } from "@lib/utils/logger";
|
||||
|
||||
// Get DCDSoundManager the same way as enhancements plugin
|
||||
const { ReactNative } = window as any;
|
||||
const { DCDSoundManager } = ReactNative?.NativeModules || {};
|
||||
|
||||
const AUDIO_URL = "https://github.com/OTKUSteyler/GoonCord/raw/refs/heads/main/src/core/plugins/maxverstappen/Max%20Verstappen.mp3";
|
||||
const SOUND_ID = 6972; // different id from enhancements (6971)
|
||||
const TRIGGER_CHANCE = 0.33;
|
||||
const SOUND_ID = 6972;
|
||||
const TRIGGER_CHANCE = 0.33;
|
||||
const MAX_PATTERN = /\bmax\s*verstappen\b/i;
|
||||
|
||||
let isPlaying = false;
|
||||
let timeoutId: NodeJS.Timeout | null = null;
|
||||
|
||||
let onMessageCreate: ((e: any) => void) | null = null;
|
||||
let onMessageSend: ((e: any) => void) | null = null;
|
||||
|
||||
function shouldTrigger(): boolean {
|
||||
return Math.random() < TRIGGER_CHANCE;
|
||||
}
|
||||
|
||||
function checkText(text: string): boolean {
|
||||
if (!text) return false;
|
||||
return MAX_PATTERN.test(text) && shouldTrigger();
|
||||
}
|
||||
|
||||
|
|
@ -28,14 +31,12 @@ async function playAudio() {
|
|||
return;
|
||||
}
|
||||
|
||||
// Stop if already playing
|
||||
if (isPlaying) {
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
DCDSoundManager.stop(SOUND_ID);
|
||||
isPlaying = false;
|
||||
}
|
||||
|
||||
// Prepare then play
|
||||
DCDSoundManager.prepare(AUDIO_URL, "music", SOUND_ID, async (error: any, sound: any) => {
|
||||
if (error) {
|
||||
logger.error("[MaxVerstappen] Failed to prepare sound:", error);
|
||||
|
|
@ -84,21 +85,33 @@ export default defineCorePlugin({
|
|||
return;
|
||||
}
|
||||
|
||||
FluxDispatcher.subscribe("MESSAGE_CREATE", ({ message }: any) => {
|
||||
if (!message?.content) return;
|
||||
if (checkText(message.content)) {
|
||||
logger.log("[MaxVerstappen] Triggered!");
|
||||
playAudio();
|
||||
|
||||
onMessageCreate = ({ message }: any) => {
|
||||
try {
|
||||
if (!message?.content) return;
|
||||
if (checkText(message.content)) {
|
||||
logger.log("[MaxVerstappen] Triggered on received message!");
|
||||
playAudio();
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error("[MaxVerstappen] Error in MESSAGE_CREATE handler:", e);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
FluxDispatcher.subscribe("MESSAGE_SEND", ({ message }: any) => {
|
||||
if (!message?.content) return;
|
||||
if (checkText(message.content)) {
|
||||
logger.log("[MaxVerstappen] Triggered on sent message!");
|
||||
playAudio();
|
||||
onMessageSend = ({ message }: any) => {
|
||||
try {
|
||||
if (!message?.content) return;
|
||||
if (checkText(message.content)) {
|
||||
logger.log("[MaxVerstappen] Triggered on sent message!");
|
||||
playAudio();
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error("[MaxVerstappen] Error in MESSAGE_SEND handler:", e);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
FluxDispatcher.subscribe("MESSAGE_CREATE", onMessageCreate);
|
||||
FluxDispatcher.subscribe("MESSAGE_SEND", onMessageSend);
|
||||
|
||||
logger.log("[MaxVerstappen] Enabled");
|
||||
},
|
||||
|
|
@ -116,10 +129,13 @@ export default defineCorePlugin({
|
|||
|
||||
const FluxDispatcher = findByProps("dispatch", "subscribe");
|
||||
if (FluxDispatcher) {
|
||||
FluxDispatcher.unsubscribe("MESSAGE_CREATE");
|
||||
FluxDispatcher.unsubscribe("MESSAGE_SEND");
|
||||
if (onMessageCreate) FluxDispatcher.unsubscribe("MESSAGE_CREATE", onMessageCreate);
|
||||
if (onMessageSend) FluxDispatcher.unsubscribe("MESSAGE_SEND", onMessageSend);
|
||||
}
|
||||
|
||||
onMessageCreate = null;
|
||||
onMessageSend = null;
|
||||
|
||||
logger.log("[MaxVerstappen] Disabled");
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue