-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
executable file
·233 lines (210 loc) · 8.25 KB
/
types.ts
File metadata and controls
executable file
·233 lines (210 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import type { Attachment, CommandInteraction, Message, Role, User, AnyInteractionChannel,
ApplicationCommandOptionsWithValue,
ApplicationCommandOptionTypes,
ApplicationCommandTypes,
AutocompleteInteraction,
ComponentInteraction,
ComponentTypes,
CreateApplicationCommandOptions,
CreateMessageOptions,
ModalComponent,
ModalSubmitInteraction,
Uncached,
EditMessageOptions } from "oceanic.js";
import type { PermissionTier } from "./permissions.ts";
import type { prompt } from "./utils/gemini.ts";
export type NonEmptyArray<T> = [T, ...T[]];
export interface BangResult {
content: CreateMessageOptions | string;
link?: string;
afterSend?: (msg: Message<AnyInteractionChannel | Uncached>) => any;
}
export type Context = (
Message<AnyInteractionChannel | Uncached>
| (CommandInteraction<AnyInteractionChannel | Uncached> & { author: User })
) & {
editSelf: (content: EditMessageOptions) => Promise<Message<AnyInteractionChannel | Uncached>>;
};
export enum ComponentHandlerTypes {
MODAL,
BUTTON,
STRING_SELECT
}
export interface ButtonComponentHandler {
match: RegExp;
type: ComponentHandlerTypes.BUTTON;
handle: (ctx: ComponentInteraction<ComponentTypes.BUTTON>) => Promise<any>;
}
export interface StringSelectComponentHandler {
match: RegExp;
type: ComponentHandlerTypes.STRING_SELECT;
handle: (ctx: ComponentInteraction<ComponentTypes.STRING_SELECT>, value: string) => Promise<any>;
}
interface ModalInputValueMap {
[ComponentTypes.TEXT_INPUT]: string;
[ComponentTypes.STRING_SELECT]: string[];
[ComponentTypes.USER_SELECT]: User[];
[ComponentTypes.CHANNEL_SELECT]: AnyInteractionChannel[];
[ComponentTypes.ROLE_SELECT]: Role[];
[ComponentTypes.MENTIONABLE_SELECT]: (User | Role)[];
[ComponentTypes.FILE_UPLOAD]: Attachment[];
}
type ModalInputComponentFromSchemaItem<S> =
S extends { type: ComponentTypes.ACTION_ROW; components: readonly (infer C)[] } ? C
: S extends { type: ComponentTypes.LABEL; component: infer C } ? C
: never;
type ModalFieldEntry<S> =
ModalInputComponentFromSchemaItem<S> extends infer C
? C extends { customID: infer ID extends string; type: infer CT extends keyof ModalInputValueMap }
? {
customID: ID;
required: C extends { required: false } ? false : true;
value: ModalInputValueMap[CT];
}
: never
: never;
export type ModalValuesFromSchema<S extends readonly ModalComponent[]> = {
[Field in ModalFieldEntry<S[number]> as Field extends { required: true } ? Field["customID"] : never]: Field["value"];
} & {
[Field in ModalFieldEntry<S[number]> as Field extends { required: false } ? Field["customID"] : never]: Field["value"] | undefined;
};
export interface ModalComponentHandler<
S extends readonly ModalComponent[] | undefined = readonly ModalComponent[] | undefined
> {
match: RegExp;
type: ComponentHandlerTypes.MODAL;
schema?: S;
handle: (ctx: ModalSubmitInteraction,
values: S extends readonly ModalComponent[] ? ModalValuesFromSchema<S> : Record<string, any>) => Promise<any>;
}
export type ComponentHandler = ButtonComponentHandler | StringSelectComponentHandler | ModalComponentHandler;
export interface Bang {
title: string;
names: NonEmptyArray<string>;
predicate?: () => boolean;
ignoreIfBlank?: boolean;
shortExecute?: boolean;
takesParameters?: boolean;
paramSuggestions?: Record<string, string>;
exampleQueries?: string[];
autocomplete?: (input?: string) => Promise<{ content: string; parameter?: string }[]>;
restrict?: PermissionTier[];
componentHandlers?: ComponentHandler[];
execute: (content: string,
attachments: Attachment[],
ctx: Context,
parameter?: string
) => Promise<BangResult>;
}
export interface PromptOptions {
systemPrompt?: string;
model?: string;
imageGeneration?: boolean;
history?: PromptHistoryItem[];
reasoning?: boolean;
}
export interface InlineData {
mime_type: string;
data: string;
}
export type PromptHistoryItemUserParts = { text: string }
| { inline_data: InlineData }
| { functionResponse: { name: string; response: any } };
export type PromptHistoryItemModelParts = { text: string }
| { inline_data: InlineData }
| { functionCall: { name: string; args: any } };
export type PromptHistoryItemParts = PromptHistoryItemUserParts | PromptHistoryItemModelParts;
export interface PromptHistoryUserItem {
role: "user";
parts: PromptHistoryItemUserParts[];
}
export interface PromptHistoryModelItem {
role: "model";
parts: PromptHistoryItemModelParts[];
}
export type PromptHistoryItem = PromptHistoryUserItem | PromptHistoryModelItem;
export interface PromptResult {
response: {
text: string;
images: Buffer[];
};
history: PromptHistoryItem[];
}
export type PromptFunctions = Exclude<Parameters<typeof prompt>["2"], "all">;
export type RemindersItem = {
uid: string;
at: number;
duration: number;
content: string;
guildID?: string;
channelID?: string;
}[];
export interface OptionTypeMapping {
[ApplicationCommandOptionTypes.STRING]: string;
[ApplicationCommandOptionTypes.INTEGER]: number;
[ApplicationCommandOptionTypes.NUMBER]: number;
[ApplicationCommandOptionTypes.BOOLEAN]: boolean;
[ApplicationCommandOptionTypes.USER]: User;
[ApplicationCommandOptionTypes.CHANNEL]: AnyInteractionChannel;
[ApplicationCommandOptionTypes.ROLE]: Role;
[ApplicationCommandOptionTypes.MENTIONABLE]: User | Role;
[ApplicationCommandOptionTypes.ATTACHMENT]: Attachment;
}
export type OptionsToArgs<T extends readonly ApplicationCommandOptionsWithValue[]> = {
[K in keyof T]: T[K] extends ApplicationCommandOptionsWithValue
? T[K]["type"] extends keyof OptionTypeMapping
? T[K]["required"] extends false
? OptionTypeMapping[T[K]["type"]] | undefined
: OptionTypeMapping[T[K]["type"]]
: never
: never
};
export type OptionsToObject<T extends readonly ApplicationCommandOptionsWithValue[]> =
T extends readonly [infer First, ...infer Rest]
? First extends ApplicationCommandOptionsWithValue
? Rest extends readonly ApplicationCommandOptionsWithValue[]
? {
[K in First["name"]]: First["type"] extends keyof OptionTypeMapping
? First["required"] extends false
? OptionTypeMapping[First["type"]] | undefined
: OptionTypeMapping[First["type"]]
: never
} & OptionsToObject<Rest>
: never
: never
: Record<string, never>;
export interface ChatInputCommand<
C extends typeof ApplicationCommandTypes.CHAT_INPUT,
O extends readonly ApplicationCommandOptionsWithValue[]
> {
name: string;
type: C;
description: string;
predicate?: () => boolean;
options?: O;
execute: (ctx: CommandInteraction<AnyInteractionChannel | Uncached, C>,
options: OptionsToObject<O>) => Promise<any>;
componentHandlers?: ComponentHandler[];
autocomplete?: (ctx: AutocompleteInteraction<AnyInteractionChannel | Uncached>,
options: Partial<OptionsToObject<O>>) => Promise<any>;
}
export interface ContextMenuCommand<
C extends typeof ApplicationCommandTypes.USER | typeof ApplicationCommandTypes.MESSAGE
> {
name: string;
type: C;
predicate?: () => boolean;
execute: (ctx: CommandInteraction<AnyInteractionChannel | Uncached, C>) => Promise<any>;
componentHandlers?: ComponentHandler[];
}
export type Command<C extends ApplicationCommandTypes, O extends readonly ApplicationCommandOptionsWithValue[]> =
C extends typeof ApplicationCommandTypes.CHAT_INPUT ? ChatInputCommand<C, O>
: C extends typeof ApplicationCommandTypes.USER | typeof ApplicationCommandTypes.MESSAGE
? ContextMenuCommand<C>
: never;
export type ExecuteFn = (...args: any[]) => Promise<any>;
export type CommandList = (CreateApplicationCommandOptions & {
execute: Record<string | symbol, ExecuteFn>;
componentHandlers: ComponentHandler[];
autocomplete?: Record<string | symbol, ExecuteFn>;
})[];