From 4d5f6f0704797506bd7be38078a1fc4c2ae69fa6 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Wed, 13 Jul 2022 08:00:18 +0200 Subject: [PATCH] fix: pong respond --- src/index.ts | 100 +++++++++++----------- src/structures/contexts/CommandContext.ts | 15 ++-- 2 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/index.ts b/src/index.ts index a8305fc..7afef2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,61 +24,57 @@ const app = new Hono(); app.get('*', (c) => c.redirect('https://www.youtube.com/watch?v=FMhScnY0dME')); app.post('/interaction', bodyParse(), async(c) => { - try { - const signature = c.req.headers.get('X-Signature-Ed25519'); - const timestamp = c.req.headers.get('X-Signature-Timestamp'); - if (!signature || !timestamp) return c.redirect('https://www.youtube.com/watch?v=FMhScnY0dME'); // fireship :D - if (!await verifyKey(JSON.stringify(c.req.parsedBody), signature, timestamp, config.client.public_key)) return c.redirect('https://www.youtube.com/watch?v=FMhScnY0dME'); // fireship :D - - const interaction = c.req.parsedBody as unknown as APIPingInteraction | APIApplicationCommandInteraction | APIMessageComponentInteraction | APIApplicationCommandAutocompleteInteraction; - - if (interaction.type === InteractionType.Ping) { - return new CommandContext(c).respond({ - type: InteractionResponseType.Pong - }); - } - - if (interaction.type === InteractionType.ApplicationCommandAutocomplete && interaction.data.type === ApplicationCommandType.ChatInput) { - const command = Commands.get(interaction.data.name); - let options = command.options; - const subCommandGroup = interaction.data.options.find(option => option.type === ApplicationCommandOptionType.SubcommandGroup) - const subCommand = interaction.data.options.find(option => option.type === ApplicationCommandOptionType.Subcommand); - - // @ts-expect-error ?? find - if (subCommandGroup) options = options.find(option => option.name === subCommandGroup.name)?.options; - // @ts-expect-error ?? find - if (subCommand) options = options.find(option => option.name === subCommand.name)?.options; - - // @ts-expect-error i dont want waste time - const focused: APIApplicationCommandBasicOption = interaction.data.options.find(option => option.focused === true); - // @ts-expect-error ?? find - const option: Option | OptionOptions = options.find(option => option.name === focused.name); - - return option.run(new AutocompleteContext( - c, - option, - focused.value - )); - } - - if (interaction.type === InteractionType.ApplicationCommand && interaction.data.type === ApplicationCommandType.ChatInput) { - const commands = Commands.get(interaction.data.name); - return await commands.run(new CommandContext( - c, - commands, - interaction - )); - } - + const signature = c.req.headers.get('X-Signature-Ed25519'); + const timestamp = c.req.headers.get('X-Signature-Timestamp'); + if (!signature || !timestamp) return c.redirect('https://www.youtube.com/watch?v=FMhScnY0dME'); // fireship :D + if (!await verifyKey(JSON.stringify(c.req.parsedBody), signature, timestamp, config.client.public_key)) return c.redirect('https://www.youtube.com/watch?v=FMhScnY0dME'); // fireship :D + + const interaction = c.req.parsedBody as unknown as APIPingInteraction | APIApplicationCommandInteraction | APIMessageComponentInteraction | APIApplicationCommandAutocompleteInteraction; + + if (interaction.type === InteractionType.Ping) { return new CommandContext(c).respond({ - type: InteractionResponseType.ChannelMessageWithSource, - data: { - content: 'Beep boop. Boop beep?' - } + type: InteractionResponseType.Pong }); - } catch(e) { - console.log(e); } + + if (interaction.type === InteractionType.ApplicationCommandAutocomplete && interaction.data.type === ApplicationCommandType.ChatInput) { + const command = Commands.get(interaction.data.name); + let options = command.options; + const subCommandGroup = interaction.data.options.find(option => option.type === ApplicationCommandOptionType.SubcommandGroup) + const subCommand = interaction.data.options.find(option => option.type === ApplicationCommandOptionType.Subcommand); + + // @ts-expect-error ?? find + if (subCommandGroup) options = options.find(option => option.name === subCommandGroup.name)?.options; + // @ts-expect-error ?? find + if (subCommand) options = options.find(option => option.name === subCommand.name)?.options; + + // @ts-expect-error i dont want waste time + const focused: APIApplicationCommandBasicOption = interaction.data.options.find(option => option.focused === true); + // @ts-expect-error ?? find + const option: Option | OptionOptions = options.find(option => option.name === focused.name); + + return option.run(new AutocompleteContext( + c, + option, + focused.value + )); + } + + if (interaction.type === InteractionType.ApplicationCommand && interaction.data.type === ApplicationCommandType.ChatInput) { + const commands = Commands.get(interaction.data.name); + return await commands.run(new CommandContext( + c, + commands, + interaction + )); + } + + return new CommandContext(c).respond({ + type: InteractionResponseType.ChannelMessageWithSource, + data: { + content: 'Beep boop. Boop beep?' + } + }); }) await Bun.serve({ diff --git a/src/structures/contexts/CommandContext.ts b/src/structures/contexts/CommandContext.ts index f1be496..61fea54 100644 --- a/src/structures/contexts/CommandContext.ts +++ b/src/structures/contexts/CommandContext.ts @@ -14,15 +14,16 @@ export class CommandContext { public resolved?: APIChatInputApplicationCommandInteractionDataResolved; public constructor(c: Context, command?: Command, interaction?: APIApplicationCommandInteraction) { - if (interaction.data.type != ApplicationCommandType.ChatInput) return; - this.context = c; this.command = command; - this.interaction = interaction as APIChatInputApplicationCommandInteraction; - this.user = interaction.member.user; - this.member = interaction.member; - this.options = interaction.data.options; - this.resolved = interaction.data.resolved; + + if (interaction) { + this.interaction = interaction as APIChatInputApplicationCommandInteraction; + this.user = this.interaction.member.user; + this.member = this.interaction.member; + this.options = this.interaction.data.options; + this.resolved = this.interaction.data.resolved; + } } public respond(response: APIInteractionResponse | string) {