feat: initial prototype

This commit is contained in:
2025-08-22 15:44:26 -07:00
parent 3dcfa8db3f
commit 09ab66d3e1
13 changed files with 4954 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
type ChatInputCommandInteraction,
ContainerBuilder,
TextDisplayBuilder,
SeparatorBuilder,
SeparatorSpacingSize,
ButtonBuilder,
ButtonStyle,
ActionRowBuilder,
MessageFlags,
} from "discord.js";
import { logger } from "../utils/logger.js";
/**
* Handles a slash command. Only responds with the about information,
* because that's all we need.
* @param interaction - The interaction payload from Discord.
*/
// eslint-disable-next-line max-lines-per-function -- Mostly components.
export const about = async(
interaction: ChatInputCommandInteraction<"cached">,
): Promise<void> => {
try {
const components = [
new ContainerBuilder().
addTextDisplayComponents(
new TextDisplayBuilder().setContent("# About Serenya"),
).
addTextDisplayComponents(
new TextDisplayBuilder().setContent(
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"Hi there~! I am Serenya, a bot that allows you to mute yourself in a server when you need to force yourself to take a break.",
),
).
addSeparatorComponents(
new SeparatorBuilder().
setSpacing(SeparatorSpacingSize.Small).
setDivider(true),
).
addTextDisplayComponents(
new TextDisplayBuilder().setContent("## What can I do?"),
).
addTextDisplayComponents(
new TextDisplayBuilder().setContent(
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"Once a server admin has added me to your community and granted the necessary permissions, you can use my `/break` command to mute yourself for a given duration. You cannot undo the mute, so this will force you to take a break for however long you desire.",
),
).
addSeparatorComponents(
new SeparatorBuilder().
setSpacing(SeparatorSpacingSize.Small).
setDivider(true),
).
addTextDisplayComponents(
new TextDisplayBuilder().setContent("## What if I need help?"),
).
addTextDisplayComponents(
new TextDisplayBuilder().setContent(
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"My deepest apologies if I have made a mistake! Please reach out to us in our Discord server and we will do our best to assist you.",
),
),
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().
setStyle(ButtonStyle.Link).
setLabel("Discord Server").
setURL("https://chat.nhcarrigan.com"),
),
];
await interaction.reply({
components: components,
flags: MessageFlags.IsComponentsV2,
});
} catch (error) {
if (error instanceof Error) {
await logger.error("about command", error);
}
await interaction.editReply({
content: "Oh dear, something went wrong.",
});
}
};
+36
View File
@@ -0,0 +1,36 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/* eslint-disable stylistic/max-len -- We have a few long strings so Naomi bein' lazy. */
import { logger } from "../utils/logger.js";
import type { ChatInputCommandInteraction } from "discord.js";
/**
* Processes the `/break` command. Attempts to mute the user for
* the duration they specify.
* @param interaction - The interaction payload from Discord.
*/
export const breakCommand = async(
interaction: ChatInputCommandInteraction<"cached">,
): Promise<void> => {
try {
await interaction.deferReply();
const minutes = interaction.options.getInteger("minutes", true);
const { member } = interaction;
if (!member.moderatable) {
await interaction.editReply({ content: "Sorry, I was not able to mute you. Please have an admin confirm that I have the necessary permission, and that my role is above yours in the heirarchy." });
return;
}
await member.timeout(minutes * 60 * 1000, "Self-requested mute to enforce a break");
await interaction.editReply({ content: "Okie dokie dearie! Enjoy your break, and we will see you later! 🩵" });
} catch (error) {
if (error instanceof Error) {
await logger.error("break command", error);
}
await interaction.editReply({ content: "Sorry, something went wrong. Please try again later." });
}
};
+49
View File
@@ -0,0 +1,49 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
Client,
GatewayIntentBits,
Events,
} from "discord.js";
import { about } from "./commands/about.js";
import { breakCommand } from "./commands/break.js";
import { instantiateServer } from "./server/serve.js";
import { logger } from "./utils/logger.js";
const serenya = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
serenya.once(Events.ClientReady, () => {
void logger.log(
"debug", `Logged in as ${serenya.user?.username ?? "unknown user"}.`,
);
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- Nah.
serenya.on(Events.InteractionCreate, async(interaction) => {
if (!interaction.isChatInputCommand() || !interaction.inCachedGuild()) {
return;
}
const command = interaction.commandName;
if (command === "about") {
await about(interaction);
return;
}
if (command === "break") {
await breakCommand(interaction);
return;
}
await interaction.reply("Huh?");
});
await serenya.login(process.env.BOT_TOKEN);
instantiateServer();
+79
View File
@@ -0,0 +1,79 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import fastify from "fastify";
import { logger } from "../utils/logger.js";
const html = `<!DOCTYPE html>
<html>
<head>
<title>Serenya</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Discord bot that allows you to force yourself to take a break." />
<script src="https://cdn.nhcarrigan.com/headers/index.js" async defer></script>
</head>
<body>
<main>
<h1>Serenya</h1>
<img src="https://cdn.nhcarrigan.com/new-avatars/serenya.png" width="250" alt="Serenya" />
<section>
<p>Discord bot that allows you to force yourself to take a break.</p>
<a href="https://discord.com/oauth2/authorize?client_id=1408505202004463757" class="social-button discord-button" style="display: inline-block; background-color: #5865F2; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px; margin: 5px;">
<i class="fab fa-discord"></i> Add to Server
</a>
</section>
<section>
<h2>Links</h2>
<p>
<a href="https://git.nhcarrigan.com/nhcarrigan/caelia">
<i class="fa-solid fa-code"></i> Source Code
</a>
</p>
<p>
<a href="https://docs.nhcarrigan.com/">
<i class="fa-solid fa-book"></i> Documentation
</a>
</p>
<p>
<a href="https://chat.nhcarrigan.com">
<i class="fa-solid fa-circle-info"></i> Support
</a>
</p>
</section>
</main>
</body>
</html>`;
/**
* Starts up a web server for health monitoring.
*/
export const instantiateServer = (): void => {
try {
const server = fastify({
logger: false,
});
server.get("/", (_request, response) => {
response.header("Content-Type", "text/html");
response.send(html);
});
server.listen({ port: 7066 }, (error) => {
if (error) {
void logger.error("instantiate server", error);
return;
}
void logger.log("debug", "Server listening on port 7066.");
});
} catch (error) {
if (error instanceof Error) {
void logger.error("instantiate server", error);
return;
}
void logger.error("instantiate server", new Error(String(error)));
}
};
+12
View File
@@ -0,0 +1,12 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { Logger } from "@nhcarrigan/logger";
export const logger = new Logger(
"Caelia",
process.env.LOG_TOKEN ?? "",
);
+12
View File
@@ -0,0 +1,12 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { Logger } from "@nhcarrigan/logger";
export const logger = new Logger(
"Serenya",
process.env.LOG_TOKEN ?? "",
);