[commands] Add /eval command

This commit is contained in:
amsyarasyiq 2024-04-30 19:02:20 +08:00
parent 6dc85aab2b
commit a2314eb86f
6 changed files with 31 additions and 6 deletions

View file

@ -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";
@ -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",

View file

@ -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",

View file

@ -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>

View file

@ -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";
@ -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()));
@ -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!));
@ -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;

View file

@ -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 {

View file

@ -9,6 +9,7 @@ export interface Settings {
enabled: boolean;
currentThemeId?: string;
};
enableEvalCommand?: boolean;
}
export interface LoaderConfig {