[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:
parent
bff883ec30
commit
7282d45d39
3 changed files with 96 additions and 3 deletions
65
src/def.d.ts
vendored
65
src/def.d.ts
vendored
|
@ -63,6 +63,66 @@ interface Settings {
|
|||
developerSettings: boolean;
|
||||
}
|
||||
|
||||
export interface ApplicationCommand {
|
||||
description: string;
|
||||
name: string;
|
||||
options: ApplicationCommandOption[];
|
||||
execute: (args: any[], ctx: CommandContext) => CommandResult | void | Promise<CommandResult> | Promise<void>;
|
||||
id?: string;
|
||||
applicationId: string;
|
||||
displayName: string;
|
||||
displayDescription: string;
|
||||
inputType: ApplicationCommandInputType;
|
||||
type: ApplicationCommandType;
|
||||
}
|
||||
|
||||
export enum ApplicationCommandInputType {
|
||||
BUILT_IN,
|
||||
BUILT_IN_TEXT,
|
||||
BUILT_IN_INTEGRATION,
|
||||
BOT,
|
||||
PLACEHOLDER,
|
||||
}
|
||||
|
||||
export interface ApplicationCommandOption {
|
||||
name: string;
|
||||
description: string;
|
||||
required?: boolean;
|
||||
type: ApplicationCommandOptionType;
|
||||
displayName: string;
|
||||
displayDescription: string;
|
||||
}
|
||||
|
||||
export enum ApplicationCommandOptionType {
|
||||
SUB_COMMAND = 1,
|
||||
SUB_COMMAND_GROUP,
|
||||
STRING,
|
||||
INTEGER,
|
||||
BOOLEAN,
|
||||
USER,
|
||||
CHANNEL,
|
||||
ROLE,
|
||||
MENTIONABLE,
|
||||
NUMBER,
|
||||
ATTACHMENT,
|
||||
}
|
||||
|
||||
export enum ApplicationCommandType {
|
||||
CHAT = 1,
|
||||
USER,
|
||||
MESSAGE,
|
||||
}
|
||||
|
||||
export interface CommandContext {
|
||||
channel: any;
|
||||
guild: any;
|
||||
}
|
||||
|
||||
export interface CommandResult {
|
||||
content: string;
|
||||
tts?: boolean;
|
||||
}
|
||||
|
||||
interface RNConstants extends _RN.PlatformConstants {
|
||||
// Android
|
||||
Version: number;
|
||||
|
@ -174,7 +234,10 @@ interface VendettaObject {
|
|||
stopPlugin: (id: string) => void;
|
||||
removePlugin: (id: string) => void;
|
||||
getSettings: (id: string) => JSX.Element;
|
||||
}
|
||||
};
|
||||
commands: {
|
||||
registerCommand: (command: ApplicationCommand) => () => void;
|
||||
};
|
||||
settings: Settings;
|
||||
logger: Logger;
|
||||
version: string;
|
||||
|
|
|
@ -14,6 +14,7 @@ import { fixTheme } from "@ui/fixTheme";
|
|||
import { connectToDebugger, patchLogHook, versionHash } from "@lib/debug";
|
||||
import { plugins, fetchPlugin, evalPlugin, stopPlugin, removePlugin, getSettings } from "@lib/plugins";
|
||||
import settings from "@lib/settings";
|
||||
import { registerCommand } from "./lib/commands";
|
||||
|
||||
console.log("Hello from Vendetta!");
|
||||
|
||||
|
@ -50,7 +51,10 @@ async function init() {
|
|||
evalPlugin: evalPlugin,
|
||||
stopPlugin: stopPlugin,
|
||||
removePlugin: removePlugin,
|
||||
getSettings: getSettings
|
||||
getSettings: getSettings,
|
||||
},
|
||||
commands: {
|
||||
registerCommand: registerCommand,
|
||||
},
|
||||
settings: settings,
|
||||
logger: logger,
|
||||
|
|
26
src/lib/commands.ts
Normal file
26
src/lib/commands.ts
Normal 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);
|
||||
}
|
Loading…
Reference in a new issue