2023-04-05 14:44:17 +02:00
|
|
|
import "./commands/setup";
|
2023-04-08 14:05:40 +02:00
|
|
|
import "./components/setup";
|
|
|
|
import "./modals/setup";
|
|
|
|
|
2023-04-06 21:32:43 +02:00
|
|
|
import {
|
|
|
|
APIApplicationCommandInteraction,
|
|
|
|
APIMessageComponentInteraction,
|
2023-04-08 14:05:40 +02:00
|
|
|
APIModalSubmitInteraction,
|
2023-04-06 21:32:43 +02:00
|
|
|
APIPingInteraction,
|
|
|
|
InteractionResponseType,
|
|
|
|
InteractionType,
|
|
|
|
} from "discord-api-types/v10";
|
2023-04-08 14:05:40 +02:00
|
|
|
import { COMMANDS, COMPONENTS, MODALS } from "./registers";
|
2023-04-06 21:32:43 +02:00
|
|
|
import { verify } from "./utils/verify";
|
|
|
|
import respond from "./utils/respond";
|
2023-04-07 21:51:25 +02:00
|
|
|
import { CommandContext } from "./structs/contexts/CommandContext";
|
|
|
|
import { ComponentContext } from "./structs/contexts/ComponentContext";
|
2023-04-08 14:05:40 +02:00
|
|
|
import { ModalContext } from "./structs/contexts/ModalContext";
|
2023-04-05 14:44:17 +02:00
|
|
|
|
2023-04-03 16:33:23 +02:00
|
|
|
export default {
|
2023-04-07 21:51:25 +02:00
|
|
|
fetch: async (request: Request, env: Env) => {
|
2023-04-06 21:32:43 +02:00
|
|
|
if (
|
|
|
|
!request.headers.get("X-Signature-Ed25519") ||
|
|
|
|
!request.headers.get("X-Signature-Timestamp")
|
|
|
|
)
|
|
|
|
return Response.redirect("https://xhyrom.dev");
|
2023-04-07 21:51:25 +02:00
|
|
|
if (!(await verify(request, env)))
|
2023-04-06 21:32:43 +02:00
|
|
|
return new Response("Invalid request signature", { status: 401 });
|
|
|
|
|
|
|
|
const interaction = (await request.json()) as
|
|
|
|
| APIPingInteraction
|
|
|
|
| APIApplicationCommandInteraction
|
2023-04-08 14:05:40 +02:00
|
|
|
| APIModalSubmitInteraction
|
2023-04-06 21:32:43 +02:00
|
|
|
| APIMessageComponentInteraction;
|
|
|
|
|
|
|
|
if (interaction.type === InteractionType.Ping)
|
|
|
|
return respond({
|
|
|
|
type: InteractionResponseType.Pong,
|
|
|
|
});
|
|
|
|
|
2023-04-08 14:05:40 +02:00
|
|
|
switch (interaction.type) {
|
|
|
|
case InteractionType.ApplicationCommand: {
|
|
|
|
const command = COMMANDS.find(
|
|
|
|
(cmd) => cmd.name === interaction.data.name,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!command) return new Response("Unknown command", { status: 404 });
|
2023-04-06 21:32:43 +02:00
|
|
|
|
2023-04-08 14:05:40 +02:00
|
|
|
try {
|
|
|
|
if (command.acknowledge)
|
|
|
|
return respond({
|
|
|
|
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
|
|
data: {
|
|
|
|
flags: command.flags,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
if (command.acknowledge)
|
|
|
|
command.run(new CommandContext(interaction, env));
|
|
|
|
// rome-ignore lint/correctness/noUnsafeFinally: it works, must do better typings etc...
|
|
|
|
else return command.run(new CommandContext(interaction, env));
|
|
|
|
}
|
2023-04-07 21:51:25 +02:00
|
|
|
|
2023-04-08 14:05:40 +02:00
|
|
|
break;
|
2023-04-07 21:51:25 +02:00
|
|
|
}
|
2023-04-08 14:05:40 +02:00
|
|
|
case InteractionType.ModalSubmit: {
|
|
|
|
const modal = MODALS.find((md) => md.id === interaction.data.custom_id);
|
2023-04-06 21:32:43 +02:00
|
|
|
|
2023-04-08 14:05:40 +02:00
|
|
|
if (!modal) return new Response("Unknown modal", { status: 404 });
|
2023-04-06 21:32:43 +02:00
|
|
|
|
2023-04-08 14:05:40 +02:00
|
|
|
try {
|
|
|
|
return respond({
|
|
|
|
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
|
|
data: {
|
|
|
|
flags: modal.flags,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
modal.run(new ModalContext(interaction, env));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case InteractionType.MessageComponent: {
|
|
|
|
const component = COMPONENTS.find(
|
|
|
|
(cmp) => cmp.id === interaction.data.custom_id,
|
|
|
|
);
|
2023-04-07 21:51:25 +02:00
|
|
|
|
2023-04-08 14:05:40 +02:00
|
|
|
if (!component)
|
|
|
|
return new Response("Unknown component", { status: 404 });
|
|
|
|
|
|
|
|
try {
|
2023-04-08 14:15:13 +02:00
|
|
|
if (component.acknowledge)
|
|
|
|
return respond({
|
|
|
|
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
|
|
data: {
|
|
|
|
flags: component.flags,
|
|
|
|
},
|
|
|
|
});
|
2023-04-08 14:05:40 +02:00
|
|
|
} finally {
|
2023-04-08 14:15:13 +02:00
|
|
|
if (component.acknowledge)
|
|
|
|
component.run(new ComponentContext(interaction, env));
|
|
|
|
// rome-ignore lint/correctness/noUnsafeFinally: it works, must do better typings etc...
|
|
|
|
else return component.run(new ComponentContext(interaction, env));
|
2023-04-08 14:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-07 21:51:25 +02:00
|
|
|
}
|
2023-04-03 16:33:23 +02:00
|
|
|
},
|
|
|
|
};
|