[Commands] Prevent patch from adding commands to all command types (#91)

* [Commands] Prevent patch from adding commands to all command types

* [Commands] Minor stylistic changes

---------

Co-authored-by: Beef <beefers@riseup.net>
This commit is contained in:
Cynthia Foxwell 2023-05-29 16:06:04 -06:00 committed by GitHub
parent 028bb19080
commit 3083f8d53a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,21 +1,24 @@
import { ApplicationCommand } from "@types"; import { ApplicationCommand, ApplicationCommandType } from "@types";
import { commands as commandsModule } from "@metro/common"; import { commands as commandsModule } from "@metro/common";
import { after } from "@lib/patcher"; import { after } from "@lib/patcher";
let commands: ApplicationCommand[] = []; let commands: ApplicationCommand[] = [];
export function patchCommands() { export function patchCommands() {
const unpatch = after("getBuiltInCommands", commandsModule, (_, res) => res.concat(commands)); const unpatch = after("getBuiltInCommands", commandsModule, ([type], res: ApplicationCommand[]) => {
if (type === ApplicationCommandType.CHAT) return res.concat(commands);
});
return () => { return () => {
commands = []; commands = [];
unpatch(); unpatch();
} };
} }
export function registerCommand(command: ApplicationCommand): () => void { export function registerCommand(command: ApplicationCommand): () => void {
// Get built in commands // Get built in commands
const builtInCommands = commandsModule.getBuiltInCommands(1, true, false); const builtInCommands = commandsModule.getBuiltInCommands(ApplicationCommandType.CHAT, true, false);
builtInCommands.sort(function (a: ApplicationCommand, b: ApplicationCommand) { return parseInt(b.id!) - parseInt(a.id!) }); builtInCommands.sort((a: ApplicationCommand, b: ApplicationCommand) => parseInt(b.id!) - parseInt(a.id!));
const lastCommand = builtInCommands[builtInCommands.length - 1]; const lastCommand = builtInCommands[builtInCommands.length - 1];
@ -26,5 +29,5 @@ export function registerCommand(command: ApplicationCommand): () => void {
commands.push(command); commands.push(command);
// Return command id so it can be unregistered // Return command id so it can be unregistered
return () => commands = commands.filter(({ id }) => id !== command.id); return () => (commands = commands.filter(({ id }) => id !== command.id));
} }