roles-bot/packages/bot/src/index.ts

128 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-04-05 14:44:17 +02:00
import "./commands/setup";
2023-04-09 17:02:15 +02:00
import "./commands/info";
2023-04-08 14:05:40 +02:00
import "./components/setup";
2023-04-09 15:36:53 +02:00
import "./components/select";
2023-04-08 14:05:40 +02:00
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";
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,
},
});
} catch (e) {
console.log(e);
2023-04-08 14:05:40 +02:00
} 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 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 {
if (modal.acknowledge)
return respond({
type: InteractionResponseType.DeferredChannelMessageWithSource,
data: {
flags: modal.flags,
},
});
} catch (e) {
console.log(e);
2023-04-08 14:05:40 +02:00
} finally {
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
}
break;
2023-04-08 14:05:40 +02:00
}
case InteractionType.MessageComponent: {
const context = new ComponentContext(interaction, env);
2023-04-09 15:36:53 +02:00
const component =
COMPONENTS.find((cmp) =>
context.interaction.data.custom_id.startsWith(cmp.id),
) ?? COMPONENTS.find((cmp) => cmp.default);
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 {
if (component.acknowledge)
return respond({
type: InteractionResponseType.DeferredChannelMessageWithSource,
data: {
flags: component.flags,
},
});
} catch (e) {
console.log(e);
2023-04-08 14:05:40 +02:00
} finally {
if (component.acknowledge) component.run(context);
// rome-ignore lint/correctness/noUnsafeFinally: it works, must do better typings etc...
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
},
};