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