Skip to content

Commit 83a8018

Browse files
committed
Remove duration and snowflake option type in favour of custom option
types
1 parent d897e02 commit 83a8018

36 files changed

Lines changed: 502 additions & 581 deletions

backend/src/plugin/core/commandEngine/parsing/stringReader.ts renamed to backend/src/common/stringReader.ts

File renamed without changes.

backend/src/plugin/core/command/access.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { escapeMarkdown } from "#common/discord/markdown.ts";
22
import { dateToUnixSecs } from "#common/time.ts";
33
import { BOT_ALLOWED_GUILDS, BOT_STAFF } from "#environment.ts";
44
import { grantAccess, revokeAccess } from "#plugin/core/guildInfoSync.ts";
5-
import {
6-
OptionType,
7-
type CommandContext,
8-
} from "#plugin/core/public/command.ts";
5+
import { type CommandContext } from "#plugin/core/public/command.ts";
96
import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
107
import { icons } from "#plugin/core/public/icons.ts";
8+
import { snowflake } from "../public/helper/customOptionTypes.ts";
119

1210
function checkBotStaff(ctx: CommandContext): boolean {
1311
return BOT_STAFF.includes(ctx.user.id);
@@ -20,7 +18,7 @@ const grantAccessCommand = defineCommand({
2018

2119
options: {
2220
guild: {
23-
type: OptionType.Snowflake,
21+
type: snowflake,
2422
name: ["server"],
2523
required: true,
2624
position: 0,
@@ -52,7 +50,7 @@ const revokeAccessCommand = defineCommand({
5250

5351
options: {
5452
guild: {
55-
type: OptionType.Snowflake,
53+
type: snowflake,
5654
name: ["server"],
5755
required: true,
5856
position: 0,

backend/src/plugin/core/command/groups.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
makeMarkdownInlineCodeblock,
66
} from "#common/discord/markdown.ts";
77
import { coreConfigStore as coreConfigCache } from "#plugin/core/index.ts";
8-
import { OptionType } from "#plugin/core/public/command.ts";
98
import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
109
import { permissionsGuard } from "#plugin/core/public/helper/commandGuards.ts";
1110
import { icons } from "#plugin/core/public/icons.ts";
@@ -19,7 +18,7 @@ export default defineCommand({
1918

2019
options: {
2120
user: {
22-
type: OptionType.User,
21+
type: "user",
2322
name: ["user", "u"],
2423
position: 0,
2524
},

backend/src/plugin/core/command/help/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
import { renderCommandPage } from "#plugin/core/command/help/show.ts";
66
import { getCommandByName } from "#plugin/core/commandEngine/commandCache.ts";
77
import { coreConfigStore } from "#plugin/core/index.ts";
8-
import { OptionType } from "#plugin/core/public/command.ts";
98
import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
109
import { permissionsGuard } from "#plugin/core/public/helper/commandGuards.ts";
1110
import { icons } from "#plugin/core/public/icons.ts";
@@ -18,7 +17,7 @@ export default defineCommand({
1817

1918
options: {
2019
command: {
21-
type: OptionType.String,
20+
type: "string",
2221
name: ["command", "c"],
2322
position: 0,
2423
},

backend/src/plugin/core/commandEngine/commandCache.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { onBotInit } from "#discord/extensionPoints.ts";
22
import { EventListenerPhase } from "#extensionPoint.ts";
3-
import {
4-
OptionType,
5-
type Command,
6-
type Option,
7-
} from "#plugin/core/public/command.ts";
3+
import { type Command, type Option } from "#plugin/core/public/command.ts";
84
import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
95

106
export interface CommandCacheEntry {
@@ -146,7 +142,7 @@ function formatCommandPrefixUsage(
146142
result += "-" + option.name[0];
147143
}
148144

149-
if (option.type !== OptionType.Flag) {
145+
if (option.type !== "boolean") {
150146
result += " <" + formatOptionValue(option) + ">";
151147
}
152148

@@ -160,23 +156,21 @@ function formatCommandPrefixUsage(
160156

161157
function formatOptionValue(option: Option): string {
162158
switch (option.type) {
163-
case OptionType.Flag:
159+
case "boolean":
164160
return "";
165-
case OptionType.Integer:
161+
case "integer":
166162
return option.array ? "integer(s)" : "integer";
167-
case OptionType.Number:
163+
case "number":
168164
return option.array ? "number(s)" : "number";
169-
case OptionType.String:
165+
case "string":
170166
return option.array ? "text(s)" : "text";
171-
case OptionType.Snowflake:
172-
return option.array ? "id(s)" : "id";
173-
case OptionType.User:
167+
case "user":
174168
return option.array ? "user(s)" : "user";
175-
case OptionType.Role:
169+
case "role":
176170
return option.array ? "role(s)" : "role";
177-
case OptionType.Channel:
171+
case "channel":
178172
return option.array ? "channel(s)" : "channel";
179-
case OptionType.Duration:
180-
return option.array ? "duration(s)" : "duration";
173+
default:
174+
return option.type.name;
181175
}
182176
}

backend/src/plugin/core/commandEngine/handler/autocompleteHandler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { getCommandByName } from "#plugin/core/commandEngine/commandCache.ts";
55
import { safePreRun } from "#plugin/core/helper/commands.ts";
66
import { coreConfigStore } from "#plugin/core/index.ts";
77
import {
8-
OptionType,
98
type AutocompleteContext,
109
type Command,
1110
} from "#plugin/core/public/command.ts";
@@ -97,7 +96,7 @@ async function handle(
9796
)!;
9897

9998
if (
100-
commandOption.type !== OptionType.String ||
99+
commandOption.type !== "string" ||
101100
commandOption.autocomplete === undefined
102101
) {
103102
return;

backend/src/plugin/core/commandEngine/handler/prefixHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { debugFormatPermissionContext } from "#common/discord/debugFormat.ts";
22
import { makeMarkdownInlineCodeblock } from "#common/discord/markdown.ts";
33
import { canWriteInChannel } from "#common/discord/permissions.ts";
44
import { moduleLogger } from "#common/logger/index.ts";
5+
import { StringReader } from "#common/stringReader.ts";
56
import { TTLMap } from "#common/ttlMap.ts";
67
import type { SquirrelDiscordContext } from "#discord/index.ts";
78
import { getCommandByName } from "#plugin/core/commandEngine/commandCache.ts";
@@ -14,7 +15,6 @@ import {
1415
readPrefixArgs,
1516
readPrefixName,
1617
} from "#plugin/core/commandEngine/parsing/prefixParser.ts";
17-
import { StringReader } from "#plugin/core/commandEngine/parsing/stringReader.ts";
1818
import {
1919
COMMAND_STATE_CLEANUP_INTERVAL,
2020
COMMAND_STATE_EXPIRE_AFTER,

backend/src/plugin/core/commandEngine/handler/slashHandler.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { coreConfigStore } from "#plugin/core/index.ts";
2121
import {
2222
type Command,
2323
type CommandContext,
24-
OptionType,
2524
type Reply,
2625
} from "#plugin/core/public/command.ts";
2726
import { onBotEvent } from "#plugin/core/public/extensionPoints.ts";
@@ -214,7 +213,7 @@ function mapCommand({
214213
};
215214

216215
switch (option.type) {
217-
case OptionType.Flag:
216+
case "boolean":
218217
options.push({
219218
type: SlashOptionTypes.NUMBER,
220219
choices: [
@@ -224,7 +223,7 @@ function mapCommand({
224223
...base,
225224
});
226225
break;
227-
case OptionType.String:
226+
case "string":
228227
options.push({
229228
type: SlashOptionTypes.STRING,
230229
autocomplete: option.autocomplete !== undefined,
@@ -234,29 +233,24 @@ function mapCommand({
234233
...base,
235234
});
236235
break;
237-
case OptionType.Integer:
236+
case "integer":
238237
options.push({ type: SlashOptionTypes.INTEGER, ...base });
239238
break;
240-
case OptionType.Number:
239+
case "number":
241240
options.push({ type: SlashOptionTypes.NUMBER, ...base });
242241
break;
243-
case OptionType.User:
242+
case "user":
244243
options.push({ type: SlashOptionTypes.USER, ...base });
245244
break;
246-
case OptionType.Role:
245+
case "role":
247246
options.push({ type: SlashOptionTypes.ROLE, ...base });
248247
break;
249-
case OptionType.Channel:
248+
case "channel":
250249
options.push({ type: SlashOptionTypes.CHANNEL, ...base });
251250
break;
252-
case OptionType.Snowflake:
253-
options.push({ type: SlashOptionTypes.STRING, ...base });
254-
break;
255-
case OptionType.Duration:
256-
options.push({ type: SlashOptionTypes.STRING, ...base });
257-
break;
258251
default:
259-
option satisfies never;
252+
option satisfies object;
253+
options.push({ type: SlashOptionTypes.STRING, ...base });
260254
break;
261255
}
262256
}

backend/src/plugin/core/commandEngine/parsing/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { AnyArgsValue } from "#plugin/core/public/command.ts";
2-
31
export const enum ArgsParseError {
42
MissingOptions,
53
BadNamedKey,
@@ -24,7 +22,7 @@ export type ArgsParseResultWithError =
2422
};
2523

2624
export type ArgsParseResult =
27-
| { error: null; result: Record<string, AnyArgsValue> }
25+
| { error: null; result: Record<string, {} | null> }
2826
| ArgsParseResultWithError;
2927

3028
export function formatArgsParseError(error: ArgsParseResultWithError): string {

backend/src/plugin/core/commandEngine/parsing/prefixParser.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
import type { CommandCacheEntry } from "#plugin/core/commandEngine/commandCache.ts";
1+
import type { StringReader } from "#common/stringReader.ts";
22
import {
33
ArgsParseError,
44
type ArgsParseResult,
55
} from "#plugin/core/commandEngine/parsing/index.ts";
66
import {
77
readChannel,
8-
readDuration,
98
readInteger,
109
readNumber,
1110
readRole,
12-
readSnowflake,
1311
readString,
1412
readUser,
15-
} from "#plugin/core/commandEngine/parsing/primitiveParser.ts";
16-
import type { StringReader } from "#plugin/core/commandEngine/parsing/stringReader.ts";
13+
} from "#plugin/core/commandEngine/parsing/primitiveParsers.ts";
1714
import { SafeArgs } from "#plugin/core/commandEngine/safeArgs.ts";
18-
import {
19-
OptionType,
20-
type AnyArgsValue,
21-
type AnyArgsValueItem,
22-
type Option,
23-
} from "#plugin/core/public/command.ts";
15+
import { type Option } from "#plugin/core/public/command.ts";
16+
import type { CommandCacheEntry } from "../commandCache.ts";
2417

2518
const LIMITED_WHITESPACE_EATER_PATTERN = /\s{0,3}/y;
2619

@@ -93,8 +86,8 @@ export function readPrefixArgs(
9386
};
9487
}
9588

96-
if (value instanceof Array) {
97-
output.pushTo(key, ...value);
89+
if (Array.isArray(value)) {
90+
output.pushTo(key, ...(value as unknown[]));
9891
} else {
9992
output.set(key, value);
10093
}
@@ -154,7 +147,7 @@ function readNamedArg(
154147

155148
// maybe best not to make this immutable? it causes typing issues
156149
if (value instanceof Array) {
157-
output.pushTo(key, ...value);
150+
output.pushTo(key, ...(value as unknown[]));
158151
} else {
159152
output.set(key, value);
160153
}
@@ -182,8 +175,8 @@ function readCommandArg(
182175
reader: StringReader,
183176
option: Option,
184177
propagateArrayError: boolean,
185-
): AnyArgsValue | null {
186-
if (option.type === OptionType.Flag) {
178+
): {} | null {
179+
if (option.type === "boolean") {
187180
return true;
188181
}
189182

@@ -198,7 +191,7 @@ function readCommandArg(
198191
return result;
199192
}
200193

201-
const result: AnyArgsValueItem[] = [];
194+
const result: unknown[] = [];
202195

203196
while (reader.canRead() && !reader.match(ARRAY_TERMINATOR)) {
204197
const prevCursor = reader.cursor;
@@ -223,21 +216,16 @@ function readCommandArg(
223216
return result;
224217
}
225218

226-
function readCommandArgValue(
227-
reader: StringReader,
228-
option: Option,
229-
): AnyArgsValueItem | null {
219+
function readCommandArgValue(reader: StringReader, option: Option): {} | null {
230220
switch (option.type) {
231-
case OptionType.Flag:
221+
case "boolean":
232222
return true;
233-
case OptionType.Integer:
223+
case "integer":
234224
return readInteger(reader);
235-
case OptionType.Number:
225+
case "number":
236226
return readNumber(reader);
237-
case OptionType.String: {
238-
const terminator =
239-
(option.greedy ?? true) ? GREEDY_VALUE_TERMINATOR : undefined;
240-
const result = readString(reader, terminator);
227+
case "string": {
228+
const result = readString(reader, GREEDY_VALUE_TERMINATOR);
241229

242230
if (result === null || result.length === 0) {
243231
return null;
@@ -259,15 +247,13 @@ function readCommandArgValue(
259247

260248
return result;
261249
}
262-
case OptionType.Snowflake:
263-
return readSnowflake(reader);
264-
case OptionType.User:
250+
case "user":
265251
return readUser(reader);
266-
case OptionType.Role:
252+
case "role":
267253
return readRole(reader);
268-
case OptionType.Channel:
254+
case "channel":
269255
return readChannel(reader);
270-
case OptionType.Duration:
271-
return readDuration(reader);
256+
default:
257+
return option.type.read(reader);
272258
}
273259
}

0 commit comments

Comments
 (0)