mirror of
https://github.com/xHyroM/bun-discord-bot.git
synced 2024-11-26 00:01:06 +01:00
fix: put /interaction to try catch
This commit is contained in:
parent
7e08e12fec
commit
a6e7afc01d
1 changed files with 54 additions and 50 deletions
104
src/index.ts
104
src/index.ts
|
@ -24,57 +24,61 @@ const app = new Hono();
|
|||
app.get('*', (c) => c.redirect('https://www.youtube.com/watch?v=FMhScnY0dME'));
|
||||
|
||||
app.post('/interaction', bodyParse(), async(c) => {
|
||||
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
|
||||
));
|
||||
}
|
||||
|
||||
return new CommandContext(c).respond({
|
||||
type: InteractionResponseType.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: 'Beep boop. Boop beep?'
|
||||
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
|
||||
));
|
||||
}
|
||||
|
||||
return new CommandContext(c).respond({
|
||||
type: InteractionResponseType.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: 'Beep boop. Boop beep?'
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
console.log(e);
|
||||
}
|
||||
})
|
||||
|
||||
await Bun.serve({
|
||||
|
|
Loading…
Reference in a new issue