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-09 13:23:41 +02:00
|
|
|
import { COMMANDS, COMPONENTS, MODALS, REDIS, setRedis } from "./things";
|
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-08 22:08:38 +02:00
|
|
|
import { Env } from "./types";
|
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-09 13:23:41 +02:00
|
|
|
if (!REDIS) setRedis(env.redisApiClientKey, env.redisApiClientHost);
|
|
|
|
|
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: {
|
2023-04-08 22:08:38 +02:00
|
|
|
const context = new ModalContext(interaction, env);
|
2023-04-09 13:23:41 +02:00
|
|
|
const modal = MODALS.find((md) =>
|
|
|
|
context.interaction.data.custom_id.startsWith(md.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 {
|
2023-04-08 22:08:38 +02:00
|
|
|
if (modal.acknowledge)
|
|
|
|
return respond({
|
|
|
|
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
|
|
data: {
|
|
|
|
flags: modal.flags,
|
|
|
|
},
|
|
|
|
});
|
2023-04-08 14:05:40 +02:00
|
|
|
} finally {
|
2023-04-08 22:08:38 +02:00
|
|
|
if (modal.acknowledge) modal.run(context);
|
|
|
|
// rome-ignore lint/correctness/noUnsafeFinally: it works, must do better typings etc...
|
|
|
|
else return modal.run(context);
|
2023-04-08 14:05:40 +02:00
|
|
|
}
|
2023-04-08 22:08:38 +02:00
|
|
|
|
|
|
|
break;
|
2023-04-08 14:05:40 +02:00
|
|
|
}
|
|
|
|
case InteractionType.MessageComponent: {
|
2023-04-08 22:08:38 +02:00
|
|
|
const context = new ComponentContext(interaction, env);
|
2023-04-09 13:23:41 +02:00
|
|
|
const component = COMPONENTS.find((cmp) =>
|
|
|
|
context.interaction.data.custom_id.startsWith(cmp.id),
|
2023-04-08 14:05:40 +02:00
|
|
|
);
|
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 22:08:38 +02:00
|
|
|
if (component.acknowledge) component.run(context);
|
2023-04-08 14:15:13 +02:00
|
|
|
// rome-ignore lint/correctness/noUnsafeFinally: it works, must do better typings etc...
|
2023-04-08 22:08:38 +02:00
|
|
|
else return component.run(context);
|
2023-04-08 14:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-07 21:51:25 +02:00
|
|
|
}
|
2023-04-03 16:33:23 +02:00
|
|
|
},
|
|
|
|
};
|