|
1 |
| -import { PermissionResolvable, PermissionsBitField } from "discord.js"; |
| 1 | +import { APIApplicationCommandOptionChoice, PermissionResolvable, PermissionsBitField } from "discord.js"; |
| 2 | +import { distance } from "fastest-levenshtein"; |
2 | 3 |
|
3 | 4 | export function resolvePermissionString(...permissions: PermissionResolvable[]): string {
|
4 | 5 | return PermissionsBitField
|
5 | 6 | .resolve(permissions)
|
6 | 7 | .toString();
|
7 | 8 | }
|
| 9 | + |
| 10 | +function standardizeString(str: string): string { |
| 11 | + return str |
| 12 | + .replaceAll(/\s+/g, "_") |
| 13 | + .toLowerCase(); |
| 14 | +} |
| 15 | + |
| 16 | +function parseChoiceData<T extends string | number>(query: string, choiceData: APIApplicationCommandOptionChoice<T>): QueriedChoiceData<T> { |
| 17 | + const standardized = standardizeString(choiceData.name); |
| 18 | + return { |
| 19 | + ...choiceData, |
| 20 | + startsWith: Number(standardized.startsWith(query)), |
| 21 | + distance: distance(query, choiceData.name) |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +interface Reducible<K, V> { |
| 26 | + reduce<T>( |
| 27 | + fn: ( |
| 28 | + accumulator: T, |
| 29 | + value: V, |
| 30 | + key: K, |
| 31 | + reducible: this |
| 32 | + ) => T, |
| 33 | + initialValue?: T |
| 34 | + ): T; |
| 35 | +} |
| 36 | + |
| 37 | +type QueriedChoiceData<T extends string | number> = APIApplicationCommandOptionChoice<T> & { |
| 38 | + startsWith: number; |
| 39 | + distance: number; |
| 40 | +}; |
| 41 | + |
| 42 | +export function parseQuery<K, V, T extends string | number>( |
| 43 | + query: string, |
| 44 | + reducible: Reducible<K, V>, |
| 45 | + fn: (value: V, key: K, reducible: Reducible<K, V>) => APIApplicationCommandOptionChoice<T> | null, |
| 46 | + firstWhenEmptyQuery?: T |
| 47 | +): APIApplicationCommandOptionChoice<T>[] { |
| 48 | + let choices: APIApplicationCommandOptionChoice<T>[]; |
| 49 | + if (query) { |
| 50 | + const standardizedQuery = standardizeString(query); |
| 51 | + choices = reducible |
| 52 | + .reduce<QueriedChoiceData<T>[]>((accumulator, value, key, reducible) => { |
| 53 | + const data = fn(value, key, reducible); |
| 54 | + if (data) { |
| 55 | + accumulator.push(parseChoiceData(standardizedQuery, data)); |
| 56 | + } |
| 57 | + return accumulator; |
| 58 | + }, []) |
| 59 | + .sort((a, b) => |
| 60 | + a.startsWith === b.startsWith ? |
| 61 | + a.distance - b.distance : |
| 62 | + b.startsWith - a.startsWith |
| 63 | + ); |
| 64 | + } |
| 65 | + else { |
| 66 | + choices = reducible.reduce<APIApplicationCommandOptionChoice<T>[]>((accumulator, value, key, reducible) => { |
| 67 | + const data = fn(value, key, reducible); |
| 68 | + if (data) { |
| 69 | + if (data.value === firstWhenEmptyQuery) { |
| 70 | + accumulator.unshift(data); |
| 71 | + } |
| 72 | + else { |
| 73 | + accumulator.push(data); |
| 74 | + } |
| 75 | + } |
| 76 | + return accumulator; |
| 77 | + }, []); |
| 78 | + } |
| 79 | + return choices.slice(0, 25); |
| 80 | +} |
0 commit comments