Skip to content

Commit

Permalink
[commands] Add /eval command
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Apr 30, 2024
1 parent 6dc85aa commit a2314eb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/core/commands/eval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Strings } from "@core/i18n";
import { ApplicationCommand, ApplicationCommandOptionType } from "@lib/api/commands/types";
import { settings } from "@lib/settings";
import { messageUtil } from "@metro/common";
import { findByProps } from "@metro/filters";

Expand All @@ -15,6 +16,7 @@ function wrapInJSCodeblock(resString: string) {
export default () => <ApplicationCommand>{
name: "eval",
description: Strings.COMMAND_EVAL_DESC,
shouldHide: () => settings.enableEvalCommand === true,
options: [
{
name: "code",
Expand Down
2 changes: 2 additions & 0 deletions src/core/i18n/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"DISABLE_THEME": "Disable Theme",
"DISABLE_UPDATES": "Disable updates",
"DISCORD_SERVER": "Discord Server",
"ENABLE_EVAL_COMMAND": "Enable /eval command",
"ENABLE_EVAL_COMMAND_DESC": "Evaluate JavaScript directly from command. Be cautious when using this command as it may pose a security risk. Make sure to know what you are doing.",
"ENABLE_UPDATES": "Enable updates",
"ERROR_BOUNDARY_TOOLS_LABEL": "ErrorBoundary Tools",
"GENERAL": "General",
Expand Down
9 changes: 9 additions & 0 deletions src/core/ui/settings/pages/Developer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ export default function Developer() {
/>
}
/>
<TableSwitchRow
label={Strings.ENABLE_EVAL_COMMAND}
subLabel={Strings.ENABLE_EVAL_COMMAND_DESC}
icon={<TableRow.Icon source={getAssetIDByName("PencilIcon")} />}
value={settings.enableEvalCommand}
onValueChange={(v: boolean) => {
settings.enableEvalCommand = v;
}}
/>
</TableRowGroup>
</Stack>
</ScrollView>
Expand Down
15 changes: 10 additions & 5 deletions src/lib/api/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApplicationCommand, ApplicationCommandInputType, ApplicationCommandType } from "@lib/api/commands/types";
import { ApplicationCommand, ApplicationCommandInputType, ApplicationCommandType, BunnyApplicationCommand } from "@lib/api/commands/types";
import { after, instead } from "@lib/api/patcher";
import { logger } from "@lib/utils/logger";
import { commands as commandsModule, messageUtil } from "@metro/common";
Expand All @@ -10,12 +10,14 @@ let commands: ApplicationCommand[] = [];
*/
export function patchCommands() {
const unpatch = after("getBuiltInCommands", commandsModule, ([type], res: ApplicationCommand[]) => {
if (type === ApplicationCommandType.CHAT) return res.concat(commands);
if (type === ApplicationCommandType.CHAT) {
return res.concat(commands.filter(c => c.__bunny?.shouldHide?.() !== false));
}
});

// Register core commands
[
// require("@core/commands/eval"),
require("@core/commands/eval"),
require("@core/commands/debug"),
require("@core/commands/plugins")
].forEach(r => registerCommand(r.default()));
Expand All @@ -26,7 +28,7 @@ export function patchCommands() {
};
}

export function registerCommand(command: ApplicationCommand): () => void {
export function registerCommand(command: BunnyApplicationCommand): () => void {
// Get built in commands
const builtInCommands = commandsModule.getBuiltInCommands(ApplicationCommandType.CHAT, true, false);
builtInCommands.sort((a: ApplicationCommand, b: ApplicationCommand) => parseInt(b.id!) - parseInt(a.id!));
Expand All @@ -37,7 +39,10 @@ export function registerCommand(command: ApplicationCommand): () => void {
command.id = (parseInt(lastCommand.id, 10) - 1).toString();

// Fill optional args
command.__isBunny = true;
command.__bunny = {
shouldHide: command.shouldHide
};

command.applicationId ??= "-1";
command.type ??= ApplicationCommandType.CHAT;
command.inputType = ApplicationCommandInputType.BUILT_IN;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/api/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export interface ApplicationCommand {
displayDescription?: string;
inputType?: ApplicationCommandInputType;
type?: ApplicationCommandType;
__isBunny?: true;
__bunny?: {
shouldHide: () => boolean;
};
}

export interface BunnyApplicationCommand extends ApplicationCommand {
shouldHide: () => boolean;
}

export enum ApplicationCommandInputType {
Expand Down
1 change: 1 addition & 0 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Settings {
enabled: boolean;
currentThemeId?: string;
};
enableEvalCommand?: boolean;
}

export interface LoaderConfig {
Expand Down

0 comments on commit a2314eb

Please sign in to comment.