@cicord/sdk 0.0.0-bootstrap.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +59 -0
- package/dist/index.d.ts +734 -0
- package/dist/index.mjs +34 -0
- package/dist/index.mjs.map +1 -0
- package/dist/testing.d.ts +304 -0
- package/dist/testing.mjs +982 -0
- package/dist/testing.mjs.map +1 -0
- package/package.json +57 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cicord/sdk — plugin authoring contract (v1alpha1).
|
|
3
|
+
*
|
|
4
|
+
* This file is the normative API of Cicord plugins. The runtime implements it on top of the
|
|
5
|
+
* plugin host (QuickJS-ng on WebAssembly); the published `@cicord/sdk` package re-exports these types.
|
|
6
|
+
*
|
|
7
|
+
* Conventions:
|
|
8
|
+
* - Server resources (channels, roles, categories) are referenced by their stable **key**
|
|
9
|
+
* (as declared in servers/<server>.yaml), or by `{ id }` for resources Cicord does not manage.
|
|
10
|
+
* - Every call that touches Discord or the runtime requires a capability, noted as
|
|
11
|
+
* `@capability`. Calling it without the grant throws `CapabilityError`.
|
|
12
|
+
* - Handlers that need gateway data note `@intent`. Declaring a handler without its intent in
|
|
13
|
+
* plugin.yaml is a build error.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// ───────────────────────────────────────────────────────────── basics
|
|
17
|
+
|
|
18
|
+
export type Snowflake = string;
|
|
19
|
+
export type Awaitable<T> = T | Promise<T>;
|
|
20
|
+
export type Json = string | number | boolean | null | Json[] | { [key: string]: Json };
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Typed plugin config. Empty by default; `cicord plugin typegen` generates a module
|
|
24
|
+
* augmentation from config.schema.json:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* declare module "@cicord/sdk" {
|
|
28
|
+
* interface PluginConfig { channel: string; message: string }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export interface PluginConfig {}
|
|
33
|
+
|
|
34
|
+
/** Key of a managed resource, or an explicit Discord ID for unmanaged ones. */
|
|
35
|
+
export type ResourceRef = string | { id: Snowflake };
|
|
36
|
+
export type ChannelRef = ResourceRef;
|
|
37
|
+
export type RoleRef = ResourceRef;
|
|
38
|
+
|
|
39
|
+
export type Permission =
|
|
40
|
+
| "CreateInstantInvite" | "KickMembers" | "BanMembers" | "Administrator" | "ManageChannels"
|
|
41
|
+
| "ManageServer" | "AddReactions" | "ViewAuditLog" | "PrioritySpeaker" | "Stream" | "ViewChannel"
|
|
42
|
+
| "SendMessages" | "SendTTSMessages" | "ManageMessages" | "EmbedLinks" | "AttachFiles"
|
|
43
|
+
| "ReadMessageHistory" | "MentionEveryone" | "UseExternalEmojis" | "ViewServerInsights"
|
|
44
|
+
| "Connect" | "Speak" | "MuteMembers" | "DeafenMembers" | "MoveMembers" | "UseVAD"
|
|
45
|
+
| "ChangeNickname" | "ManageNicknames" | "ManageRoles" | "ManageWebhooks" | "ManageExpressions"
|
|
46
|
+
| "UseApplicationCommands" | "RequestToSpeak" | "ManageEvents" | "ManageThreads"
|
|
47
|
+
| "CreatePublicThreads" | "CreatePrivateThreads" | "UseExternalStickers"
|
|
48
|
+
| "SendMessagesInThreads" | "UseEmbeddedActivities" | "ModerateMembers"
|
|
49
|
+
| "ViewCreatorMonetizationAnalytics" | "UseSoundboard" | "CreateExpressions" | "CreateEvents"
|
|
50
|
+
| "UseExternalSounds" | "SendVoiceMessages" | "SendPolls" | "UseExternalApps";
|
|
51
|
+
|
|
52
|
+
/** Thrown when a call needs a capability that is not granted in bot.yaml. */
|
|
53
|
+
export declare class CapabilityError extends Error {
|
|
54
|
+
readonly capability: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ───────────────────────────────────────────────────────────── messages
|
|
58
|
+
|
|
59
|
+
export interface Embed {
|
|
60
|
+
title?: string;
|
|
61
|
+
description?: string;
|
|
62
|
+
url?: string;
|
|
63
|
+
/** #RRGGBB */
|
|
64
|
+
color?: string;
|
|
65
|
+
timestamp?: Date | string;
|
|
66
|
+
fields?: { name: string; value: string; inline?: boolean }[];
|
|
67
|
+
author?: { name: string; url?: string; iconUrl?: string };
|
|
68
|
+
footer?: { text: string; iconUrl?: string };
|
|
69
|
+
image?: { url: string };
|
|
70
|
+
thumbnail?: { url: string };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** `id` values are local to the plugin instance; the runtime namespaces them. */
|
|
74
|
+
export type Button =
|
|
75
|
+
| { type: "button"; id: string; label?: string; emoji?: string; style?: "primary" | "secondary" | "success" | "danger"; disabled?: boolean }
|
|
76
|
+
| { type: "link"; url: string; label?: string; emoji?: string; disabled?: boolean };
|
|
77
|
+
|
|
78
|
+
export interface SelectMenu {
|
|
79
|
+
type: "select";
|
|
80
|
+
id: string;
|
|
81
|
+
placeholder?: string;
|
|
82
|
+
minValues?: number;
|
|
83
|
+
maxValues?: number;
|
|
84
|
+
options: { value: string; label: string; description?: string; emoji?: string; default?: boolean }[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface ActionRow {
|
|
88
|
+
type: "row";
|
|
89
|
+
components: (Button | SelectMenu)[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Who a message may notify (Discord's `allowed_mentions`). Without it, every mention in the
|
|
94
|
+
* content notifies. `true` allows every mention of that type, `false` none, a list only
|
|
95
|
+
* those users or roles: ids (snowflakes, not keys) or objects with an `id` (a User, Member
|
|
96
|
+
* or Role), at most 100 per list. Even when allowed, a role that is not mentionable (and
|
|
97
|
+
* `@everyone` / `@here`) only notifies if the bot has MentionEveryone in the channel.
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* // Pings exactly this role, nobody else, whatever else the content mentions.
|
|
101
|
+
* await channel.send({ content: `${role.mention} new poll`, allowedMentions: { roles: [role.id], users: [] } });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export interface AllowedMentions {
|
|
105
|
+
users?: boolean | readonly (Snowflake | { readonly id: Snowflake })[];
|
|
106
|
+
roles?: boolean | readonly (Snowflake | { readonly id: Snowflake })[];
|
|
107
|
+
everyone?: boolean;
|
|
108
|
+
/** Notify the author of the message replied to (`message.reply`). Default false once allowedMentions is set. */
|
|
109
|
+
repliedUser?: boolean;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface MessagePayload {
|
|
113
|
+
content?: string;
|
|
114
|
+
embeds?: Embed[];
|
|
115
|
+
components?: ActionRow[];
|
|
116
|
+
allowedMentions?: AllowedMentions;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export type MessageInput = string | MessagePayload;
|
|
120
|
+
|
|
121
|
+
/** Interaction replies can additionally be ephemeral (visible only to the invoking user). */
|
|
122
|
+
export type ReplyInput = string | (MessagePayload & { ephemeral?: boolean });
|
|
123
|
+
|
|
124
|
+
export interface Attachment {
|
|
125
|
+
id: Snowflake;
|
|
126
|
+
filename: string;
|
|
127
|
+
url: string;
|
|
128
|
+
contentType?: string;
|
|
129
|
+
size: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface Message {
|
|
133
|
+
readonly id: Snowflake;
|
|
134
|
+
/**
|
|
135
|
+
* Empty unless the plugin declares the MessageContent intent in plugin.yaml, or the
|
|
136
|
+
* message is the bot's own, mentions the bot or is a DM. The gating is per plugin (other
|
|
137
|
+
* plugins of the same bot declaring the intent do not lift it) and also applies to
|
|
138
|
+
* `channel.messages()`; `attachments` are emptied the same way.
|
|
139
|
+
*/
|
|
140
|
+
readonly content: string;
|
|
141
|
+
readonly author: User;
|
|
142
|
+
readonly member?: Member;
|
|
143
|
+
readonly channel: TextChannel;
|
|
144
|
+
readonly attachments: readonly Attachment[];
|
|
145
|
+
readonly createdAt: Date;
|
|
146
|
+
/** @capability discord.messages.send */
|
|
147
|
+
reply(message: MessageInput): Promise<Message>;
|
|
148
|
+
/** @capability discord.messages.manage (when not authored by this bot) */
|
|
149
|
+
edit(message: MessageInput): Promise<Message>;
|
|
150
|
+
/** @capability discord.messages.manage */
|
|
151
|
+
delete(reason?: string): Promise<void>;
|
|
152
|
+
/** @capability discord.reactions.manage */
|
|
153
|
+
react(emoji: string): Promise<void>;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ───────────────────────────────────────────────────────────── server entities
|
|
157
|
+
|
|
158
|
+
export interface User {
|
|
159
|
+
readonly id: Snowflake;
|
|
160
|
+
readonly username: string;
|
|
161
|
+
readonly displayName: string;
|
|
162
|
+
readonly avatarUrl?: string;
|
|
163
|
+
readonly bot: boolean;
|
|
164
|
+
/** `<@id>` */
|
|
165
|
+
readonly mention: string;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface Role {
|
|
169
|
+
readonly id: Snowflake;
|
|
170
|
+
/** Key in servers/<server>.yaml when the role is managed by Cicord. */
|
|
171
|
+
readonly key?: string;
|
|
172
|
+
readonly name: string;
|
|
173
|
+
readonly color?: string;
|
|
174
|
+
readonly mention: string;
|
|
175
|
+
/**
|
|
176
|
+
* Position in the role hierarchy: higher is higher, `@everyone` is 0. Two roles may share
|
|
177
|
+
* a position (Discord then orders them by id). `undefined` when the role is not known to
|
|
178
|
+
* the runtime (not cached yet, or an older engine): treat it as unknown, never as 0.
|
|
179
|
+
*/
|
|
180
|
+
readonly position?: number;
|
|
181
|
+
/** Whether anyone may mention the role. `undefined` when the role is not known. */
|
|
182
|
+
readonly mentionable?: boolean;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface Channel {
|
|
186
|
+
readonly id: Snowflake;
|
|
187
|
+
/** Key in servers/<server>.yaml when the channel is managed by Cicord. */
|
|
188
|
+
readonly key?: string;
|
|
189
|
+
readonly name: string;
|
|
190
|
+
readonly type: "text" | "voice" | "announcement" | "stage" | "forum" | "media" | "thread" | "category" | "dm";
|
|
191
|
+
readonly mention: string;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface TextChannel extends Channel {
|
|
195
|
+
/** @capability discord.messages.send */
|
|
196
|
+
send(message: MessageInput): Promise<Message>;
|
|
197
|
+
/** @capability discord.messages.read */
|
|
198
|
+
messages(options?: { limit?: number; before?: Snowflake; after?: Snowflake }): Promise<Message[]>;
|
|
199
|
+
/**
|
|
200
|
+
* One message by id, `undefined` when it does not exist (or was deleted). Same content
|
|
201
|
+
* gating as `messages()`. @capability discord.messages.read
|
|
202
|
+
*/
|
|
203
|
+
message(id: Snowflake): Promise<Message | undefined>;
|
|
204
|
+
/** @capability discord.threads.manage */
|
|
205
|
+
createThread(name: string, options?: { private?: boolean; fromMessage?: Snowflake }): Promise<TextChannel>;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface Member {
|
|
209
|
+
readonly user: User;
|
|
210
|
+
readonly id: Snowflake;
|
|
211
|
+
readonly nickname?: string;
|
|
212
|
+
readonly displayName: string;
|
|
213
|
+
readonly mention: string;
|
|
214
|
+
readonly roles: readonly Role[];
|
|
215
|
+
readonly joinedAt: Date;
|
|
216
|
+
/**
|
|
217
|
+
* Server-level permissions: those of `@everyone` and of every role of the member; all of
|
|
218
|
+
* them for the server owner and for Administrator. Channel overwrites (and timeouts) are
|
|
219
|
+
* NOT applied: this is not what the member may do in a given channel. Roles the runtime
|
|
220
|
+
* does not know count for nothing. `undefined` on an older engine.
|
|
221
|
+
*/
|
|
222
|
+
readonly permissions?: readonly Permission[];
|
|
223
|
+
/**
|
|
224
|
+
* Cicord access level, as for portal routes: `"admin"` for the server owner and
|
|
225
|
+
* `spec.access.admins` of servers/<server>.yaml, `"moderator"` for
|
|
226
|
+
* `spec.access.moderators`, else `"member"`. `undefined` on an older engine.
|
|
227
|
+
*/
|
|
228
|
+
readonly access?: "member" | "moderator" | "admin";
|
|
229
|
+
hasRole(role: RoleRef): boolean;
|
|
230
|
+
/** @capability discord.roles.assign */
|
|
231
|
+
addRole(role: RoleRef, reason?: string): Promise<void>;
|
|
232
|
+
/** @capability discord.roles.assign */
|
|
233
|
+
removeRole(role: RoleRef, reason?: string): Promise<void>;
|
|
234
|
+
/** @capability discord.members.manage */
|
|
235
|
+
setNickname(nickname: string | null, reason?: string): Promise<void>;
|
|
236
|
+
/** @capability discord.members.moderate */
|
|
237
|
+
timeout(durationMs: number, reason?: string): Promise<void>;
|
|
238
|
+
/** @capability discord.members.moderate */
|
|
239
|
+
kick(reason?: string): Promise<void>;
|
|
240
|
+
/** @capability discord.members.moderate */
|
|
241
|
+
ban(options?: { reason?: string; deleteMessagesSeconds?: number }): Promise<void>;
|
|
242
|
+
/** Direct message. @capability discord.dm.send */
|
|
243
|
+
send(message: MessageInput): Promise<Message>;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export interface CreateChannelOptions {
|
|
247
|
+
name: string;
|
|
248
|
+
type?: "text" | "voice" | "forum";
|
|
249
|
+
category?: ResourceRef;
|
|
250
|
+
topic?: string;
|
|
251
|
+
/**
|
|
252
|
+
* Permission overwrites keyed by target: `"@everyone"`, a role key, `"role:<id>"` or
|
|
253
|
+
* `"member:<id>"` (e.g. the ticket opener, or `member:${ctx.bot.user.id}` so the bot keeps
|
|
254
|
+
* access without Administrator).
|
|
255
|
+
*/
|
|
256
|
+
overwrites?: Record<string, { allow?: Permission[]; deny?: Permission[] }>;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export interface Server {
|
|
260
|
+
readonly id: Snowflake;
|
|
261
|
+
/** metadata.name of the Server resource. */
|
|
262
|
+
readonly key: string;
|
|
263
|
+
readonly name: string;
|
|
264
|
+
readonly iconUrl?: string;
|
|
265
|
+
/** User id of the server owner, read live; `undefined` when not known (or an older engine). */
|
|
266
|
+
readonly ownerId?: Snowflake;
|
|
267
|
+
/** Resolve a channel by key or id. Throws if it does not exist. @capability discord.channels.read */
|
|
268
|
+
channel(ref: ChannelRef): TextChannel;
|
|
269
|
+
/** Resolve a role by key or id. Throws if it does not exist. */
|
|
270
|
+
role(ref: RoleRef): Role;
|
|
271
|
+
/** @capability discord.members.read */
|
|
272
|
+
member(userId: Snowflake): Promise<Member | undefined>;
|
|
273
|
+
/**
|
|
274
|
+
* Create a channel owned by this plugin instance (e.g. a ticket). The reconciler never
|
|
275
|
+
* prunes plugin-owned channels. @capability discord.channels.manage
|
|
276
|
+
*/
|
|
277
|
+
createChannel(options: CreateChannelOptions): Promise<TextChannel>;
|
|
278
|
+
/** Delete a channel owned by this plugin instance. @capability discord.channels.manage */
|
|
279
|
+
deleteChannel(ref: ChannelRef, reason?: string): Promise<void>;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ───────────────────────────────────────────────────────────── interactions
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Discord requires an answer within 3 seconds. If a handler has not replied after ~2 s, the
|
|
286
|
+
* runtime defers automatically (ephemeral if the command/component says so) and a later
|
|
287
|
+
* `reply()` edits the deferred response transparently.
|
|
288
|
+
*/
|
|
289
|
+
export interface Interaction {
|
|
290
|
+
readonly id: Snowflake;
|
|
291
|
+
readonly user: User;
|
|
292
|
+
readonly member?: Member;
|
|
293
|
+
readonly channel?: TextChannel;
|
|
294
|
+
readonly replied: boolean;
|
|
295
|
+
readonly deferred: boolean;
|
|
296
|
+
reply(message: ReplyInput): Promise<void>;
|
|
297
|
+
defer(options?: { ephemeral?: boolean }): Promise<void>;
|
|
298
|
+
followUp(message: ReplyInput): Promise<Message>;
|
|
299
|
+
showModal(modal: Modal): Promise<void>;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export type CommandOptionType =
|
|
303
|
+
| "string" | "integer" | "number" | "boolean" | "user" | "channel" | "role" | "mentionable" | "attachment";
|
|
304
|
+
|
|
305
|
+
/** A suggestion returned by an autocomplete handler (value: string for string options). */
|
|
306
|
+
export interface AutocompleteChoice {
|
|
307
|
+
/** 1–100 characters, shown to the user. */
|
|
308
|
+
name: string;
|
|
309
|
+
/** What the option receives when picked (strings: at most 100 characters). */
|
|
310
|
+
value: string | number;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** The option being typed: `value` is the raw text so far (a string for every option type). */
|
|
314
|
+
export interface FocusedOption {
|
|
315
|
+
readonly name: string;
|
|
316
|
+
readonly value: string;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Sent while the user types an option that has `autocomplete`. It cannot reply: the handler
|
|
321
|
+
* returns the choices.
|
|
322
|
+
*/
|
|
323
|
+
export interface AutocompleteInteraction {
|
|
324
|
+
readonly id: Snowflake;
|
|
325
|
+
readonly user: User;
|
|
326
|
+
readonly member?: Member;
|
|
327
|
+
readonly channel?: TextChannel;
|
|
328
|
+
readonly command: string;
|
|
329
|
+
readonly group?: string;
|
|
330
|
+
readonly subcommand?: string;
|
|
331
|
+
/** Other options already filled in: raw values (ids for users, roles and channels). */
|
|
332
|
+
readonly options: Readonly<Record<string, string | number | boolean>>;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface CommandOption {
|
|
336
|
+
type: CommandOptionType;
|
|
337
|
+
description: string;
|
|
338
|
+
required?: boolean;
|
|
339
|
+
choices?: readonly { name: string; value: string | number }[];
|
|
340
|
+
/** integer / number options. */
|
|
341
|
+
min?: number;
|
|
342
|
+
max?: number;
|
|
343
|
+
/** string options: length bounds (0–6000, maxLength ≥ 1). */
|
|
344
|
+
minLength?: number;
|
|
345
|
+
maxLength?: number;
|
|
346
|
+
/**
|
|
347
|
+
* Suggestions while the user types (string, integer and number options; not with
|
|
348
|
+
* `choices`). Answer within ~2.5 s with at most 25 choices: an autocomplete cannot be
|
|
349
|
+
* deferred, and a handler that fails or is late shows an empty list.
|
|
350
|
+
*/
|
|
351
|
+
autocomplete?(
|
|
352
|
+
ctx: Context,
|
|
353
|
+
interaction: AutocompleteInteraction,
|
|
354
|
+
focused: FocusedOption,
|
|
355
|
+
): Awaitable<readonly AutocompleteChoice[]>;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
interface OptionTypeMap {
|
|
359
|
+
string: string;
|
|
360
|
+
integer: number;
|
|
361
|
+
number: number;
|
|
362
|
+
boolean: boolean;
|
|
363
|
+
user: User;
|
|
364
|
+
channel: Channel;
|
|
365
|
+
role: Role;
|
|
366
|
+
mentionable: User | Role;
|
|
367
|
+
attachment: Attachment;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export type OptionValues<O extends Record<string, CommandOption>> = {
|
|
371
|
+
[K in keyof O as O[K]["required"] extends true ? K : never]: OptionTypeMap[O[K]["type"]];
|
|
372
|
+
} & {
|
|
373
|
+
[K in keyof O as O[K]["required"] extends true ? never : K]?: OptionTypeMap[O[K]["type"]];
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
export interface CommandInteraction<O extends Record<string, CommandOption> = Record<string, CommandOption>>
|
|
377
|
+
extends Interaction {
|
|
378
|
+
/** Top-level command name. */
|
|
379
|
+
readonly command: string;
|
|
380
|
+
/** Group of the invoked subcommand (`/polls admin purge` → `"admin"`). */
|
|
381
|
+
readonly group?: string;
|
|
382
|
+
/** Invoked subcommand (`/polls close` → `"close"`). */
|
|
383
|
+
readonly subcommand?: string;
|
|
384
|
+
readonly options: OptionValues<O>;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export interface ComponentInteraction extends Interaction {
|
|
388
|
+
/** Local component id (as given in Button/SelectMenu). */
|
|
389
|
+
readonly componentId: string;
|
|
390
|
+
/** Selected values for select menus. */
|
|
391
|
+
readonly values: readonly string[];
|
|
392
|
+
readonly message: Message;
|
|
393
|
+
/** Edit the message the component belongs to. */
|
|
394
|
+
update(message: MessageInput): Promise<void>;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export interface Modal {
|
|
398
|
+
id: string;
|
|
399
|
+
title: string;
|
|
400
|
+
fields: {
|
|
401
|
+
id: string;
|
|
402
|
+
label: string;
|
|
403
|
+
style?: "short" | "paragraph";
|
|
404
|
+
required?: boolean;
|
|
405
|
+
placeholder?: string;
|
|
406
|
+
value?: string;
|
|
407
|
+
minLength?: number;
|
|
408
|
+
maxLength?: number;
|
|
409
|
+
}[];
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
export interface ModalInteraction extends Interaction {
|
|
413
|
+
readonly modalId: string;
|
|
414
|
+
readonly fields: Readonly<Record<string, string>>;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export interface CommandDefinition<O extends Record<string, CommandOption> = Record<string, CommandOption>> {
|
|
418
|
+
description: string;
|
|
419
|
+
/** Mapped so that `autocomplete` handlers get typed parameters. */
|
|
420
|
+
options?: { [K in keyof O]: O[K] & CommandOption };
|
|
421
|
+
/** Ephemeral replies and auto-defer by default. */
|
|
422
|
+
ephemeral?: boolean;
|
|
423
|
+
/** Default member permissions required to see/use the command. */
|
|
424
|
+
permissions?: Permission[];
|
|
425
|
+
run(ctx: Context, interaction: CommandInteraction<O>): Awaitable<void>;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** A subcommand: `/polls close poll:…`. Its options are typed like a command's. */
|
|
429
|
+
export interface SubcommandDefinition<O extends Record<string, CommandOption> = Record<string, CommandOption>> {
|
|
430
|
+
description: string;
|
|
431
|
+
/** Mapped so that `autocomplete` handlers get typed parameters. */
|
|
432
|
+
options?: { [K in keyof O]: O[K] & CommandOption };
|
|
433
|
+
run(ctx: Context, interaction: CommandInteraction<O>): Awaitable<void>;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** A group of subcommands: `/polls admin purge`. A group holds only subcommands. */
|
|
437
|
+
export interface SubcommandGroupDefinition {
|
|
438
|
+
description: string;
|
|
439
|
+
subcommands: Record<string, SubcommandDefinition<any>>;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* A command made of subcommands (and groups of subcommands): at most 25 of them in all. The
|
|
444
|
+
* command itself cannot be invoked: it has no `run` and no `options`. `ephemeral` and
|
|
445
|
+
* `permissions` apply to every subcommand.
|
|
446
|
+
*/
|
|
447
|
+
export interface CommandWithSubcommandsDefinition {
|
|
448
|
+
description: string;
|
|
449
|
+
ephemeral?: boolean;
|
|
450
|
+
permissions?: Permission[];
|
|
451
|
+
subcommands?: Record<string, SubcommandDefinition<any>>;
|
|
452
|
+
groups?: Record<string, SubcommandGroupDefinition>;
|
|
453
|
+
options?: never;
|
|
454
|
+
run?: never;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Helper that infers option types for `interaction.options`.
|
|
459
|
+
*
|
|
460
|
+
* ```ts
|
|
461
|
+
* command({ description: "Ping", run: (ctx, i) => i.reply("pong") });
|
|
462
|
+
* command({
|
|
463
|
+
* description: "Manage polls",
|
|
464
|
+
* subcommands: {
|
|
465
|
+
* close: subcommand({
|
|
466
|
+
* description: "Close a poll",
|
|
467
|
+
* options: { poll: { type: "string", description: "Poll", required: true, autocomplete: suggestPolls } },
|
|
468
|
+
* async run(ctx, interaction) { … interaction.options.poll … },
|
|
469
|
+
* }),
|
|
470
|
+
* },
|
|
471
|
+
* });
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
export declare function command<const O extends Record<string, CommandOption> = {}>(
|
|
475
|
+
definition: CommandDefinition<O>,
|
|
476
|
+
): CommandDefinition<O>;
|
|
477
|
+
export declare function command(definition: CommandWithSubcommandsDefinition): CommandWithSubcommandsDefinition;
|
|
478
|
+
|
|
479
|
+
/** Helper that infers option types for a subcommand (`command({ subcommands: { … } })`). */
|
|
480
|
+
export declare function subcommand<const O extends Record<string, CommandOption> = {}>(
|
|
481
|
+
definition: SubcommandDefinition<O>,
|
|
482
|
+
): SubcommandDefinition<O>;
|
|
483
|
+
|
|
484
|
+
// ───────────────────────────────────────────────────────────── runtime services
|
|
485
|
+
|
|
486
|
+
export interface Logger {
|
|
487
|
+
debug(message: string, fields?: Record<string, Json>): void;
|
|
488
|
+
info(message: string, fields?: Record<string, Json>): void;
|
|
489
|
+
warn(message: string, fields?: Record<string, Json>): void;
|
|
490
|
+
error(message: string, fields?: Record<string, Json>): void;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Key-value storage scoped to the plugin instance. Values are JSON. @capability storage.kv */
|
|
494
|
+
export interface KV {
|
|
495
|
+
get<T extends Json = Json>(key: string): Promise<T | undefined>;
|
|
496
|
+
set(key: string, value: Json, options?: { ttlSeconds?: number }): Promise<void>;
|
|
497
|
+
delete(key: string): Promise<void>;
|
|
498
|
+
/** Atomic increment; creates the key at 0 if missing. Returns the new value. */
|
|
499
|
+
increment(key: string, by?: number): Promise<number>;
|
|
500
|
+
list<T extends Json = Json>(options?: { prefix?: string; limit?: number; cursor?: string }):
|
|
501
|
+
Promise<{ items: { key: string; value: T }[]; cursor?: string }>;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** @capability scheduler */
|
|
505
|
+
export interface Scheduler {
|
|
506
|
+
/** Run the task declared under `tasks[name]` once after `delayMs`. Survives restarts. */
|
|
507
|
+
after(delayMs: number, task: string, payload?: Json): Promise<{ id: string }>;
|
|
508
|
+
cancel(id: string): Promise<void>;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** Subset of WHATWG fetch, limited to hosts allowed in plugin.yaml. @capability http.fetch */
|
|
512
|
+
export interface Http {
|
|
513
|
+
fetch(url: string, init?: {
|
|
514
|
+
method?: string;
|
|
515
|
+
headers?: Record<string, string>;
|
|
516
|
+
body?: string;
|
|
517
|
+
timeoutMs?: number;
|
|
518
|
+
}): Promise<{
|
|
519
|
+
status: number;
|
|
520
|
+
headers: Record<string, string>;
|
|
521
|
+
text(): Promise<string>;
|
|
522
|
+
json<T = unknown>(): Promise<T>;
|
|
523
|
+
}>;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** Cross-plugin events within the same (bot, server). */
|
|
527
|
+
export interface Events {
|
|
528
|
+
/**
|
|
529
|
+
* Topic is namespaced by the publisher plugin name: `publish("levelUp", …)` from plugin
|
|
530
|
+
* `xp` is delivered to `subscriptions["xp.levelUp"]`. @capability events.publish
|
|
531
|
+
*/
|
|
532
|
+
publish(topic: string, payload: Json): Promise<void>;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
export interface Portal {
|
|
536
|
+
/**
|
|
537
|
+
* Signed, short-lived URL to a page of the community portal. When `member` is given, the
|
|
538
|
+
* portal session is pre-authenticated for that member. @capability portal.links
|
|
539
|
+
*/
|
|
540
|
+
link(path: string, options?: { member?: Member | User; expiresInSeconds?: number }): Promise<string>;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export interface Context {
|
|
544
|
+
readonly plugin: { readonly name: string; readonly version: string; readonly instance: string };
|
|
545
|
+
readonly bot: { readonly key: string; readonly user: User };
|
|
546
|
+
readonly server: Server;
|
|
547
|
+
/** Effective config: base config merged with the server override. */
|
|
548
|
+
readonly config: Readonly<PluginConfig>;
|
|
549
|
+
readonly log: Logger;
|
|
550
|
+
readonly kv: KV;
|
|
551
|
+
readonly schedule: Scheduler;
|
|
552
|
+
readonly http: Http;
|
|
553
|
+
readonly events: Events;
|
|
554
|
+
readonly portal: Portal;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// ───────────────────────────────────────────────────────────── portal routes
|
|
558
|
+
|
|
559
|
+
export type Access = "public" | "member" | "moderator" | "admin";
|
|
560
|
+
|
|
561
|
+
export interface RouteRequest {
|
|
562
|
+
readonly params: Readonly<Record<string, string>>;
|
|
563
|
+
readonly query: Readonly<Record<string, string>>;
|
|
564
|
+
readonly body: Json | undefined;
|
|
565
|
+
/** Authenticated viewer, present unless access is "public" and the viewer is anonymous. */
|
|
566
|
+
readonly viewer?: { readonly user: User; readonly member?: Member };
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export type RouteResponse = Json | { status: number; body?: Json; headers?: Record<string, string> };
|
|
570
|
+
|
|
571
|
+
export interface RouteDefinition {
|
|
572
|
+
access: Access;
|
|
573
|
+
handler(ctx: Context, request: RouteRequest): Awaitable<RouteResponse>;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** Keys are "<METHOD> <path>", e.g. "GET /leaderboard" or "POST /answers/:id". */
|
|
577
|
+
export type RouteKey = `${"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} /${string}`;
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Declared types of one route: what the portal sends (`query`, `body`) and receives
|
|
581
|
+
* (`response`, the JSON of a `200`; omit it for routes that answer `204`). A plugin lists
|
|
582
|
+
* them in a type-only contract file (plugin.yaml `spec.routes.types`, e.g.
|
|
583
|
+
* `src/routes.ts`: `export interface Routes { "GET /polls/:id": { response: Poll } }`),
|
|
584
|
+
* checks its handlers against it with `defineRoutes<Routes>()`, and `cicord portal typegen`
|
|
585
|
+
* copies it into the portals, which get a typed client for the routes.
|
|
586
|
+
*/
|
|
587
|
+
export interface RouteTypes {
|
|
588
|
+
/** Query parameters sent by the portal. They arrive as strings: see `TypedRouteRequest`. */
|
|
589
|
+
query?: object;
|
|
590
|
+
/** JSON body sent by the portal (POST, PUT, PATCH). */
|
|
591
|
+
body?: unknown;
|
|
592
|
+
/** JSON answered with status 200. */
|
|
593
|
+
response?: unknown;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** `{ id: string }` for "GET /polls/:id": the `:name` segments of a route key. */
|
|
597
|
+
export type RouteParams<K extends string> = K extends `${string}/:${infer Rest}`
|
|
598
|
+
? Rest extends `${infer Name}/${infer Tail}`
|
|
599
|
+
? { [P in Name]: string } & RouteParams<`/${Tail}`>
|
|
600
|
+
: { [P in Rest]: string }
|
|
601
|
+
: {};
|
|
602
|
+
|
|
603
|
+
/** An explicit response: `{ status: 404, body: { error: … } }`. */
|
|
604
|
+
export interface RouteEnvelope {
|
|
605
|
+
status: number;
|
|
606
|
+
body?: unknown;
|
|
607
|
+
headers?: Record<string, string>;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* The request of a route declared with `defineRoutes<Routes>()`. `query` values are the
|
|
612
|
+
* strings of the URL and `body` is the JSON sent: the runtime does not validate them
|
|
613
|
+
* against the declared types, so check them like any input.
|
|
614
|
+
*/
|
|
615
|
+
export interface TypedRouteRequest<K extends string, T extends RouteTypes> {
|
|
616
|
+
readonly params: Readonly<RouteParams<K>>;
|
|
617
|
+
readonly query: { readonly [Q in keyof T["query"]]?: string } & Readonly<Record<string, string>>;
|
|
618
|
+
readonly body: (T extends { body?: infer B } ? B : Json) | undefined;
|
|
619
|
+
/** Authenticated viewer, present unless access is "public" and the viewer is anonymous. */
|
|
620
|
+
readonly viewer?: { readonly user: User; readonly member?: Member };
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** A route whose handler is checked against its declared types (`defineRoutes`). */
|
|
624
|
+
export interface TypedRouteDefinition<K extends string, T extends RouteTypes> {
|
|
625
|
+
access: Access;
|
|
626
|
+
handler(
|
|
627
|
+
ctx: Context,
|
|
628
|
+
request: TypedRouteRequest<K, T>,
|
|
629
|
+
): Awaitable<(T extends { response: infer R } ? R : void) | RouteEnvelope>;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Routes checked against a contract: every route of `Routes` must be implemented, with the
|
|
634
|
+
* declared parameters, query, body and response. Identity at run time.
|
|
635
|
+
*
|
|
636
|
+
* ```ts
|
|
637
|
+
* import type { Routes } from "./routes.ts";
|
|
638
|
+
* routes: defineRoutes<Routes>({
|
|
639
|
+
* "GET /polls/:id": { access: "public", async handler(ctx, request) { … request.params.id … } },
|
|
640
|
+
* }),
|
|
641
|
+
* ```
|
|
642
|
+
*/
|
|
643
|
+
export declare function defineRoutes<Routes extends { [K in keyof Routes]: RouteTypes }>(routes: {
|
|
644
|
+
[K in keyof Routes & RouteKey]: TypedRouteDefinition<K, Routes[K]>;
|
|
645
|
+
}): { [K in RouteKey]?: RouteDefinition };
|
|
646
|
+
|
|
647
|
+
// ───────────────────────────────────────────────────────────── plugin definition
|
|
648
|
+
|
|
649
|
+
export interface EventHandlers {
|
|
650
|
+
/** @intent ServerMembers */
|
|
651
|
+
memberJoin?(ctx: Context, member: Member): Awaitable<void>;
|
|
652
|
+
/** @intent ServerMembers */
|
|
653
|
+
memberLeave?(ctx: Context, user: User): Awaitable<void>;
|
|
654
|
+
/** @intent ServerMembers */
|
|
655
|
+
memberUpdate?(ctx: Context, member: Member, previous: Member | undefined): Awaitable<void>;
|
|
656
|
+
/** @intent ServerMessages (+ MessageContent to read message.content) */
|
|
657
|
+
messageCreate?(ctx: Context, message: Message): Awaitable<void>;
|
|
658
|
+
/** @intent ServerMessages */
|
|
659
|
+
messageUpdate?(ctx: Context, message: Message): Awaitable<void>;
|
|
660
|
+
/** @intent ServerMessages */
|
|
661
|
+
messageDelete?(ctx: Context, deleted: { id: Snowflake; channel: TextChannel }): Awaitable<void>;
|
|
662
|
+
/**
|
|
663
|
+
* @intent ServerMessageReactions
|
|
664
|
+
* `message` is partial: only `id`, `channel` and the methods are guaranteed (author/content
|
|
665
|
+
* may be missing); fetch it with `message.channel.message(message.id)` if you need more.
|
|
666
|
+
*/
|
|
667
|
+
reactionAdd?(ctx: Context, reaction: { message: Message; user: User; emoji: string }): Awaitable<void>;
|
|
668
|
+
/** @intent ServerMessageReactions — same partial `message` as reactionAdd. */
|
|
669
|
+
reactionRemove?(ctx: Context, reaction: { message: Message; user: User; emoji: string }): Awaitable<void>;
|
|
670
|
+
/**
|
|
671
|
+
* The bot joined the server (invited again after a removal, for instance) while
|
|
672
|
+
* connected; not sent for servers loading after a connection or an outage. Like every
|
|
673
|
+
* event, it only reaches instances of servers declared for the bot. No intent to declare.
|
|
674
|
+
*/
|
|
675
|
+
serverJoin?(ctx: Context, server: { id: Snowflake; name: string; iconUrl?: string }): Awaitable<void>;
|
|
676
|
+
/**
|
|
677
|
+
* The bot left the server: kicked, banned, removed or the server was deleted (not when
|
|
678
|
+
* Discord has an outage). Discord API calls for the server fail from now on. No intent to
|
|
679
|
+
* declare.
|
|
680
|
+
*/
|
|
681
|
+
serverLeave?(ctx: Context, server: { id: Snowflake }): Awaitable<void>;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
export interface PluginDefinition {
|
|
685
|
+
/** Called when the instance starts (new revision, restart, plugin enabled). */
|
|
686
|
+
onStart?(ctx: Context): Awaitable<void>;
|
|
687
|
+
/** Called before the instance stops; must finish quickly. */
|
|
688
|
+
onStop?(ctx: Context): Awaitable<void>;
|
|
689
|
+
/** Gateway event handlers. */
|
|
690
|
+
events?: EventHandlers;
|
|
691
|
+
/** Slash commands, keyed by command name (lowercase, 1–32 chars). Synced per server. */
|
|
692
|
+
commands?: Record<string, CommandDefinition<any> | CommandWithSubcommandsDefinition>;
|
|
693
|
+
/**
|
|
694
|
+
* Component handlers keyed by local component id. A component id may carry data after a
|
|
695
|
+
* colon: `"vote:42"` is routed to `components.vote` (`componentId` stays `"vote:42"`).
|
|
696
|
+
*/
|
|
697
|
+
components?: Record<string, (ctx: Context, interaction: ComponentInteraction) => Awaitable<void>>;
|
|
698
|
+
/** Modal submit handlers keyed by modal id. */
|
|
699
|
+
modals?: Record<string, (ctx: Context, interaction: ModalInteraction) => Awaitable<void>>;
|
|
700
|
+
/** Recurring jobs (cron syntax, server time zone UTC). @capability scheduler */
|
|
701
|
+
schedules?: Record<string, { cron: string; run(ctx: Context): Awaitable<void> }>;
|
|
702
|
+
/** One-shot tasks started with ctx.schedule.after(). @capability scheduler */
|
|
703
|
+
tasks?: Record<string, (ctx: Context, payload: Json | undefined) => Awaitable<void>>;
|
|
704
|
+
/** Cross-plugin event subscriptions, keyed by "<plugin>.<topic>". @capability events.subscribe */
|
|
705
|
+
subscriptions?: Record<string, (ctx: Context, payload: Json) => Awaitable<void>>;
|
|
706
|
+
/** HTTP routes exposed to the community portal. @capability portal.routes */
|
|
707
|
+
routes?: { [K in RouteKey]?: RouteDefinition };
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** Entry point of a plugin: `export default definePlugin({ ... })`. */
|
|
711
|
+
export declare function definePlugin(definition: PluginDefinition): PluginDefinition;
|
|
712
|
+
|
|
713
|
+
// ───────────────────────────────────────────────────────────── globals
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* Globals provided by the plugin host (no Node.js / DOM APIs). Timers are bound to the
|
|
717
|
+
* handler that created them and cancelled when it times out.
|
|
718
|
+
*/
|
|
719
|
+
declare global {
|
|
720
|
+
function setTimeout(callback: (...args: any[]) => void, ms?: number, ...args: any[]): number;
|
|
721
|
+
function clearTimeout(id: number | undefined): void;
|
|
722
|
+
function setInterval(callback: (...args: any[]) => void, ms?: number, ...args: any[]): number;
|
|
723
|
+
function clearInterval(id: number | undefined): void;
|
|
724
|
+
function queueMicrotask(callback: () => void): void;
|
|
725
|
+
/** Mapped to the instance logger. Declared by interface merging so it composes with lib.dom. */
|
|
726
|
+
interface Console {
|
|
727
|
+
log(...args: any[]): void;
|
|
728
|
+
info(...args: any[]): void;
|
|
729
|
+
warn(...args: any[]): void;
|
|
730
|
+
error(...args: any[]): void;
|
|
731
|
+
debug(...args: any[]): void;
|
|
732
|
+
}
|
|
733
|
+
var console: Console;
|
|
734
|
+
}
|