@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.
Files changed (38) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-dev.log +168 -5
  3. package/.turbo/turbo-docs.log +3 -1
  4. package/CHANGELOG.md +14 -0
  5. package/dist/package.json +2 -2
  6. package/dist/src/abstracts/BaseCommand.d.ts +1 -2
  7. package/dist/src/abstracts/BaseCommand.d.ts.map +1 -1
  8. package/dist/src/abstracts/BaseCommand.js.map +1 -1
  9. package/dist/src/classes/Client.d.ts +12 -0
  10. package/dist/src/classes/Client.d.ts.map +1 -1
  11. package/dist/src/classes/Client.js +45 -46
  12. package/dist/src/classes/Client.js.map +1 -1
  13. package/dist/src/classes/Command.d.ts +1 -3
  14. package/dist/src/classes/Command.d.ts.map +1 -1
  15. package/dist/src/classes/Command.js +1 -1
  16. package/dist/src/classes/Command.js.map +1 -1
  17. package/dist/src/internals/OptionsHandler.d.ts +1 -1
  18. package/dist/src/internals/OptionsHandler.d.ts.map +1 -1
  19. package/dist/src/internals/OptionsHandler.js +1 -1
  20. package/dist/src/internals/OptionsHandler.js.map +1 -1
  21. package/dist/tsconfig.tsbuildinfo +1 -1
  22. package/docs/classes/AnySelectMenuInteraction.mdx +14 -0
  23. package/docs/classes/AutocompleteInteraction.mdx +14 -0
  24. package/docs/classes/BaseComponentInteraction.mdx +14 -0
  25. package/docs/classes/BaseInteraction.mdx +10 -0
  26. package/docs/classes/ButtonInteraction.mdx +14 -0
  27. package/docs/classes/ChannelSelectMenuInteraction.mdx +14 -0
  28. package/docs/classes/CommandInteraction.mdx +14 -0
  29. package/docs/classes/Guild.mdx +18 -0
  30. package/docs/classes/MentionableSelectMenuInteraction.mdx +14 -0
  31. package/docs/classes/RoleSelectMenuInteraction.mdx +14 -0
  32. package/docs/classes/StringSelectMenuInteraction.mdx +14 -0
  33. package/docs/classes/UserSelectMenuInteraction.mdx +14 -0
  34. package/package.json +2 -2
  35. package/src/abstracts/BaseCommand.ts +6 -2
  36. package/src/classes/Client.ts +65 -60
  37. package/src/classes/Command.ts +5 -3
  38. 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 { ApplicationIntegrationType, InteractionContextType } from "../index.js"
6
- import type { BaseComponent } from "./BaseComponent.js"
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
@@ -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("/interaction", async (req, ctx?: ExecutionContext) => {
154
- const isValid = await this.validateInteraction(req)
155
- if (!isValid) {
156
- return new Response("Invalid request signature", { status: 401 })
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
- const rawInteraction = (await req.json()) as unknown as APIInteraction
160
- if (rawInteraction.type === InteractionType.Ping) {
161
- return json({
162
- type: InteractionResponseType.Pong
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
- if (rawInteraction.type === InteractionType.ApplicationCommand) {
167
- if (ctx?.waitUntil) {
168
- ctx.waitUntil(
169
- (async () => {
170
- await this.commandHandler.handleCommandInteraction(rawInteraction)
171
- })()
172
- )
173
- } else {
174
- await this.commandHandler.handleCommandInteraction(rawInteraction)
175
- }
176
- }
177
- if (
178
- rawInteraction.type === InteractionType.ApplicationCommandAutocomplete
179
- ) {
180
- if (ctx?.waitUntil) {
181
- ctx.waitUntil(
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
- if (rawInteraction.type === InteractionType.ApplicationCommand) {
195
- if (ctx?.waitUntil) {
196
- ctx.waitUntil(
197
- (async () => {
198
- await this.commandHandler.handleCommandInteraction(rawInteraction)
199
- })()
200
- )
201
- } else {
202
- await this.commandHandler.handleCommandInteraction(rawInteraction)
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
- if (rawInteraction.type === InteractionType.MessageComponent) {
206
- if (ctx?.waitUntil) {
207
- ctx.waitUntil(
208
- (async () => {
209
- await this.componentHandler.handleInteraction(rawInteraction)
210
- })()
211
- )
212
- } else {
213
- await this.componentHandler.handleInteraction(rawInteraction)
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
- return new Response(null, { status: 202 })
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: IRequestStrict) {
229
+ private async validateInteraction(req: Request) {
225
230
  if (req.method !== "POST") {
226
231
  throw new StatusError(405)
227
232
  }
@@ -2,9 +2,11 @@ import {
2
2
  type APIApplicationCommandBasicOption,
3
3
  ApplicationCommandType
4
4
  } from "discord-api-types/v10"
5
- import { BaseCommand } from "../abstracts/BaseCommand.js"
6
- import type { AutocompleteInteraction } from "../internals/AutocompleteInteraction.js"
7
- import type { CommandInteraction } from "../internals/CommandInteraction.js"
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)) return undefined
60
+ if (!num || typeof num !== "number" || !Number.isSafeInteger(num))
61
+ return undefined
61
62
  return num
62
63
  }
63
64