[Global] Basic commands api implementation (#9)

* [Global] Basic commands api implementation

* [Commands] Return unregister method when registering command

* [Commands] Code cleanup pass

Co-authored-by: Beef <beefers@riseup.net>
This commit is contained in:
Jack 2023-01-25 03:28:48 -05:00 committed by GitHub
parent bff883ec30
commit 7282d45d39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 3 deletions

26
src/lib/commands.ts Normal file
View file

@ -0,0 +1,26 @@
import { ApplicationCommand } from "@types";
import { findByProps } from "@metro/filters";
import { after } from "@lib/patcher";
const commandsModule = findByProps("getBuiltInCommands")
let commands: ApplicationCommand[] = [];
after("getBuiltInCommands", commandsModule, (args, res) => res.concat(commands));
export function registerCommand(command: ApplicationCommand): () => void {
// Get built in commands
const builtInCommands = commandsModule.getBuiltInCommands(1, true, false);
builtInCommands.sort(function (a: ApplicationCommand, b: ApplicationCommand) { return parseInt(b.id!) - parseInt(a.id!) });
const lastCommand = builtInCommands[builtInCommands.length - 1];
// Override the new command's id to the last command id - 1
command.id = (parseInt(lastCommand.id, 10) - 1).toString();
// Add it to the commands array
commands.push(command);
// Return command id so it can be unregistered
return () => commands = commands.filter(({ id }) => id !== command.id);
}