fix(package/addCommand): allow typed arguments

This commit is contained in:
Linden
2024-01-15 17:52:59 +11:00
parent e2e6ae34ac
commit c383e42602

View File

@@ -34,7 +34,7 @@ function parseArguments(
raw: string, raw: string,
params?: OxCommandParams[] params?: OxCommandParams[]
): OxCommandArguments | undefined { ): OxCommandArguments | undefined {
if (!params) { return args; } if (!params) return args;
const result = params.every((param, index) => { const result = params.every((param, index) => {
const arg = args[index]; const arg = args[index];
@@ -51,9 +51,8 @@ function parseArguments(
case 'playerId': case 'playerId':
value = arg === 'me' ? source : +arg; value = arg === 'me' ? source : +arg;
if (!value || !GetPlayerGuid(value.toString())) { if (!value || !GetPlayerGuid(value.toString())) value = false;
value = false;
}
break; break;
default: default:
@@ -78,9 +77,9 @@ function parseArguments(
return result ? args : undefined; return result ? args : undefined;
} }
export function addCommand( export function addCommand<T extends OxCommandArguments>(
commandName: string | string[], commandName: string | string[],
cb: (source: number, args: object, raw: string) => any, cb: (source: number, args: T, raw: string) => any,
properties?: OxCommandProperties properties?: OxCommandProperties
) { ) {
const restricted = properties?.restricted; const restricted = properties?.restricted;
@@ -88,9 +87,8 @@ export function addCommand(
if (params) { if (params) {
params.forEach((param) => { params.forEach((param) => {
if (param.paramType) { if (param.paramType)
param.help = param.help ? `${param.help} (type: ${param.paramType})` : `(type: ${param.paramType})`; param.help = param.help ? `${param.help} (type: ${param.paramType})` : `(type: ${param.paramType})`;
}
}); });
} }
@@ -98,8 +96,9 @@ export function addCommand(
const numCommands = commands.length; const numCommands = commands.length;
const commandHandler = (source: number, args: OxCommandArguments, raw: string) => { const commandHandler = (source: number, args: OxCommandArguments, raw: string) => {
const parsed = parseArguments(source, args, raw, params); const parsed = parseArguments(source, args, raw, params) as T | undefined;
if (!parsed) { return; }
if (!parsed) return;
cb(source, parsed, raw); cb(source, parsed, raw);
}; };
@@ -116,9 +115,7 @@ export function addCommand(
} else if (restrictedType === 'object') { } else if (restrictedType === 'object') {
const _restricted = restricted as string[]; const _restricted = restricted as string[];
_restricted.forEach((principal) => { _restricted.forEach((principal) => {
if (!IsPrincipalAceAllowed(principal, ace)) { if (!IsPrincipalAceAllowed(principal, ace)) addAce(principal as string, ace, true);
addAce(principal as string, ace, true);
}
}); });
} }
} }
@@ -128,13 +125,9 @@ export function addCommand(
delete properties.restricted; delete properties.restricted;
registeredCommmands.push(properties); registeredCommmands.push(properties);
if (index !== numCommands && numCommands !== 1) { if (index !== numCommands && numCommands !== 1) properties = { ...properties };
properties = { ...properties };
}
if (shouldSendCommands) { if (shouldSendCommands) emitNet('chat:addSuggestions', -1, properties);
emitNet('chat:addSuggestions', -1, properties);
}
} }
}); });
} }