This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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>
- Loading branch information
1 parent
028bb19
commit 3083f8d
Showing
1 changed file
with
17 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
import { ApplicationCommand } from "@types"; | ||
import { ApplicationCommand, ApplicationCommandType } from "@types"; | ||
import { commands as commandsModule } from "@metro/common"; | ||
import { after } from "@lib/patcher"; | ||
|
||
let commands: ApplicationCommand[] = []; | ||
|
||
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 () => { | ||
commands = []; | ||
unpatch(); | ||
} | ||
}; | ||
} | ||
|
||
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!) }); | ||
// Get built in commands | ||
const builtInCommands = commandsModule.getBuiltInCommands(ApplicationCommandType.CHAT, true, false); | ||
builtInCommands.sort((a: ApplicationCommand, b: ApplicationCommand) => parseInt(b.id!) - parseInt(a.id!)); | ||
|
||
const lastCommand = builtInCommands[builtInCommands.length - 1]; | ||
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(); | ||
// 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); | ||
// 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); | ||
} | ||
// Return command id so it can be unregistered | ||
return () => (commands = commands.filter(({ id }) => id !== command.id)); | ||
} |