Improve timestamp handling and scoreCommand patching

Refactor timestamp handling and patch scoreCommand for null checks.
This commit is contained in:
OTKUSteyler 2026-01-17 23:00:06 +01:00 • committed by GitHub
parent 9984b28e45
commit 68660a2e06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,29 +1,20 @@
/**
* Why do we need this exactly? It's simple, to fight the side effects caused by when you
* initialize modules not in the right time or when you're not supposed to.
* Back then, we had major bugs like the mixing of light and dark themes and AMOLED option getting ignored.
* Nowadays, there are only three which I can spot: Hindi timestamps, crashing on some native components and slower startup time.
* - @pylixonly
*/
import { instead } from "spitroast"; // Assuming you have this from entry.ts setup
import { logger } from "@lib/utils/logger";
import { FluxDispatcher } from "@metro/common";
// Find the module with scoreCommand (use your mod's finder utils, like findByCode if you have one)
const searchModule = /* Your finder here, e.g., findByCode("toLocaleLowerCase", "scoreCommand") or similar */;
function onDispatch({ locale }: { locale: string }) {
// Timestamps - require moment lazily so we avoid top-level import cost
try {
// require at runtime (lazy) to keep startup/bundle lighter
// eslint-disable-next-line @typescript-eslint/no-var-requires
const moment = require("moment");
moment.locale(locale.toLowerCase());
} catch (e) {
logger.error("Failed to fix timestamps (lazy moment)...", e);
}
// We're done here!
FluxDispatcher.unsubscribe("I18N_LOAD_SUCCESS", onDispatch);
if (searchModule) {
instead(searchModule.scoreCommand, (args, orig) => {
// Add null check before the lowercase call
const [command, query] = args; // Assuming args structure based on typical Discord code
if (query === undefined || query === null) {
return 0; // Or some safe default score to skip
}
// Or target the exact line: wrap the string op
// e.g., if the buggy line is something like str.toLocaleLowerCase(), replace with str?.toLocaleLowerCase() ?? ""
return orig(...args);
});
logger.log("Patched scoreCommand to handle undefined values!");
} else {
logger.warn("Couldn't find scoreCommand module to patch.");
}
export default () => {
FluxDispatcher.subscribe("I18N_LOAD_SUCCESS", onDispatch);
};