@buape/carbon 0.1.1 → 0.1.3
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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-dev.log +168 -5
- package/.turbo/turbo-docs.log +3 -1
- package/CHANGELOG.md +14 -0
- package/dist/package.json +2 -2
- package/dist/src/abstracts/BaseCommand.d.ts +1 -2
- package/dist/src/abstracts/BaseCommand.d.ts.map +1 -1
- package/dist/src/abstracts/BaseCommand.js.map +1 -1
- package/dist/src/classes/Client.d.ts +12 -0
- package/dist/src/classes/Client.d.ts.map +1 -1
- package/dist/src/classes/Client.js +45 -46
- package/dist/src/classes/Client.js.map +1 -1
- package/dist/src/classes/Command.d.ts +1 -3
- package/dist/src/classes/Command.d.ts.map +1 -1
- package/dist/src/classes/Command.js +1 -1
- package/dist/src/classes/Command.js.map +1 -1
- package/dist/src/internals/OptionsHandler.d.ts +1 -1
- package/dist/src/internals/OptionsHandler.d.ts.map +1 -1
- package/dist/src/internals/OptionsHandler.js +1 -1
- package/dist/src/internals/OptionsHandler.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/docs/classes/AnySelectMenuInteraction.mdx +14 -0
- package/docs/classes/AutocompleteInteraction.mdx +14 -0
- package/docs/classes/BaseComponentInteraction.mdx +14 -0
- package/docs/classes/BaseInteraction.mdx +10 -0
- package/docs/classes/ButtonInteraction.mdx +14 -0
- package/docs/classes/ChannelSelectMenuInteraction.mdx +14 -0
- package/docs/classes/CommandInteraction.mdx +14 -0
- package/docs/classes/Guild.mdx +18 -0
- package/docs/classes/MentionableSelectMenuInteraction.mdx +14 -0
- package/docs/classes/RoleSelectMenuInteraction.mdx +14 -0
- package/docs/classes/StringSelectMenuInteraction.mdx +14 -0
- package/docs/classes/UserSelectMenuInteraction.mdx +14 -0
- package/package.json +2 -2
- package/src/abstracts/BaseCommand.ts +6 -2
- package/src/classes/Client.ts +65 -60
- package/src/classes/Command.ts +5 -3
- package/src/internals/OptionsHandler.ts +2 -1
|
@@ -2,8 +2,11 @@ import {
|
|
|
2
2
|
ApplicationCommandType,
|
|
3
3
|
type RESTPostAPIApplicationCommandsJSONBody
|
|
4
4
|
} from "discord-api-types/v10"
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
ApplicationIntegrationType,
|
|
7
|
+
type BaseComponent,
|
|
8
|
+
InteractionContextType
|
|
9
|
+
} from "../index.js"
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
* Represents the base data of a command that the user creates
|
|
@@ -52,6 +55,7 @@ export abstract class BaseCommand {
|
|
|
52
55
|
* You mount these here so the handler can access them
|
|
53
56
|
*/
|
|
54
57
|
components?: BaseComponent[] = []
|
|
58
|
+
|
|
55
59
|
/**
|
|
56
60
|
* All the paginators that the command is able to use.
|
|
57
61
|
* You mount these here so the handler can access them
|
package/src/classes/Client.ts
CHANGED
|
@@ -58,6 +58,11 @@ export type ClientOptions = {
|
|
|
58
58
|
* ```
|
|
59
59
|
*/
|
|
60
60
|
mode: ClientMode
|
|
61
|
+
/**
|
|
62
|
+
* The route to use for interactions on your server.
|
|
63
|
+
* @default "/interaction"
|
|
64
|
+
*/
|
|
65
|
+
interactionRoute?: string
|
|
61
66
|
/**
|
|
62
67
|
* The options used to initialize the request client, if you want to customize it.
|
|
63
68
|
*/
|
|
@@ -150,78 +155,78 @@ export class Client {
|
|
|
150
155
|
return Response.redirect(this.options.redirectUrl, 302)
|
|
151
156
|
throw new StatusError(404)
|
|
152
157
|
})
|
|
153
|
-
this.router.post(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
return
|
|
158
|
+
this.router.post(
|
|
159
|
+
this.options.interactionRoute || "/interaction",
|
|
160
|
+
async (req, ctx?: ExecutionContext) => {
|
|
161
|
+
return await this.handle(req, ctx)
|
|
157
162
|
}
|
|
163
|
+
)
|
|
164
|
+
}
|
|
158
165
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
166
|
+
/**
|
|
167
|
+
* If you want use a custom handler for HTTP requests instead of Carbon's router, you can use this method.
|
|
168
|
+
* @param req The request to handle
|
|
169
|
+
* @param ctx Cloudflare Workers only. The execution context of the request, provided in the fetch handler from CF.
|
|
170
|
+
* @returns A response to send back to the client.
|
|
171
|
+
*/
|
|
172
|
+
public async handle(req: Request, ctx?: ExecutionContext) {
|
|
173
|
+
const isValid = await this.validateInteraction(req)
|
|
174
|
+
if (!isValid) {
|
|
175
|
+
return new Response("Invalid request signature", { status: 401 })
|
|
176
|
+
}
|
|
165
177
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
(async () => {
|
|
183
|
-
await this.commandHandler.handleAutocompleteInteraction(
|
|
184
|
-
rawInteraction
|
|
185
|
-
)
|
|
186
|
-
})()
|
|
187
|
-
)
|
|
188
|
-
} else {
|
|
189
|
-
await this.commandHandler.handleAutocompleteInteraction(
|
|
190
|
-
rawInteraction
|
|
191
|
-
)
|
|
192
|
-
}
|
|
178
|
+
const rawInteraction = (await req.json()) as unknown as APIInteraction
|
|
179
|
+
if (rawInteraction.type === InteractionType.Ping) {
|
|
180
|
+
return json({
|
|
181
|
+
type: InteractionResponseType.Pong
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (rawInteraction.type === InteractionType.ApplicationCommand) {
|
|
186
|
+
if (ctx?.waitUntil) {
|
|
187
|
+
ctx.waitUntil(
|
|
188
|
+
(async () => {
|
|
189
|
+
await this.commandHandler.handleCommandInteraction(rawInteraction)
|
|
190
|
+
})()
|
|
191
|
+
)
|
|
192
|
+
} else {
|
|
193
|
+
await this.commandHandler.handleCommandInteraction(rawInteraction)
|
|
193
194
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
195
|
+
}
|
|
196
|
+
if (
|
|
197
|
+
rawInteraction.type === InteractionType.ApplicationCommandAutocomplete
|
|
198
|
+
) {
|
|
199
|
+
if (ctx?.waitUntil) {
|
|
200
|
+
ctx.waitUntil(
|
|
201
|
+
(async () => {
|
|
202
|
+
await this.commandHandler.handleAutocompleteInteraction(
|
|
203
|
+
rawInteraction
|
|
204
|
+
)
|
|
205
|
+
})()
|
|
206
|
+
)
|
|
207
|
+
} else {
|
|
208
|
+
await this.commandHandler.handleAutocompleteInteraction(rawInteraction)
|
|
204
209
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
)
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
210
|
+
}
|
|
211
|
+
if (rawInteraction.type === InteractionType.MessageComponent) {
|
|
212
|
+
if (ctx?.waitUntil) {
|
|
213
|
+
ctx.waitUntil(
|
|
214
|
+
(async () => {
|
|
215
|
+
await this.componentHandler.handleInteraction(rawInteraction)
|
|
216
|
+
})()
|
|
217
|
+
)
|
|
218
|
+
} else {
|
|
219
|
+
await this.componentHandler.handleInteraction(rawInteraction)
|
|
215
220
|
}
|
|
216
|
-
|
|
217
|
-
})
|
|
221
|
+
}
|
|
222
|
+
return new Response(null, { status: 202 })
|
|
218
223
|
}
|
|
219
224
|
|
|
220
225
|
/**
|
|
221
226
|
* Validate the interaction request
|
|
222
227
|
* @param req The request to validate
|
|
223
228
|
*/
|
|
224
|
-
private async validateInteraction(req:
|
|
229
|
+
private async validateInteraction(req: Request) {
|
|
225
230
|
if (req.method !== "POST") {
|
|
226
231
|
throw new StatusError(405)
|
|
227
232
|
}
|
package/src/classes/Command.ts
CHANGED
|
@@ -2,9 +2,11 @@ import {
|
|
|
2
2
|
type APIApplicationCommandBasicOption,
|
|
3
3
|
ApplicationCommandType
|
|
4
4
|
} from "discord-api-types/v10"
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import {
|
|
6
|
+
type AutocompleteInteraction,
|
|
7
|
+
BaseCommand,
|
|
8
|
+
type CommandInteraction
|
|
9
|
+
} from "../index.js"
|
|
8
10
|
|
|
9
11
|
export type CommandOptions = APIApplicationCommandBasicOption[]
|
|
10
12
|
|
|
@@ -57,7 +57,8 @@ export class OptionsHandler extends Base {
|
|
|
57
57
|
const num = this.raw.find(
|
|
58
58
|
(x) => x.name === key && x.type === ApplicationCommandOptionType.Integer
|
|
59
59
|
)?.value
|
|
60
|
-
if (!num || !Number.isSafeInteger(num))
|
|
60
|
+
if (!num || typeof num !== "number" || !Number.isSafeInteger(num))
|
|
61
|
+
return undefined
|
|
61
62
|
return num
|
|
62
63
|
}
|
|
63
64
|
|