feat: initial prototype
Node.js CI / Lint and Test (push) Successful in 32s

This commit is contained in:
2025-11-08 15:26:36 -08:00
parent f3bd566832
commit e2a6a44741
13 changed files with 5004 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { SlashCommandBuilder } from "discord.js";
const roll = new SlashCommandBuilder().setName("roll").
setDescription("Roll the dice!").
addIntegerOption(
(option) => {
return option.setName("count").setDescription("How many dice to roll").
setMinValue(1).
setMaxValue(100).
setRequired(true);
},
).
addStringOption(
(option) => {
return option.setName("die").setDescription("What kind of dice to roll.").
setRequired(true).
addChoices(
[
{
name: "d2",
value: "2",
},
{
name: "d3",
value: "3",
},
{
name: "d4",
value: "4",
},
{
name: "d6",
value: "6",
},
{
name: "d8",
value: "8",
},
{
name: "d10",
value: "10",
},
{
name: "d12",
value: "12",
},
{
name: "d20",
value: "20",
},
{
name: "d100",
value: "100",
},
],
);
},
).
addNumberOption(
(option) => {
return option.setName("modifier").
setDescription("What modifier to apply to the roll").
setRequired(false);
},
).
addStringOption(
(option) => {
return option.setName("type").
setDescription("What type of roll to perform").
setRequired(true).
addChoices(
[
{
name: "Normal Roll",
value: "normal",
},
{
name: "Advantage Roll - roll twice and take the higher result",
value: "advantage",
},
{
name: "Disadvantage Roll - roll twice and take the lower result",
value: "disadvantage",
},
],
);
},
);
// eslint-disable-next-line no-console -- We only use this file to generate the command JSON for the Discord API.
console.log(JSON.stringify(roll.toJSON(), null, 2));
+34
View File
@@ -0,0 +1,34 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { DiscordAnalytics } from "@nhcarrigan/discord-analytics";
import { Client, Events, GatewayIntentBits } from "discord.js";
import { roll } from "./modules/roll.js";
import { instantiateServer } from "./server/serve.js";
import { logger } from "./utils/logger.js";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
],
});
const analytics = new DiscordAnalytics(client, logger);
client.once(Events.ClientReady, () => {
void logger.log("debug", "Authenticated with Discord.");
analytics.startCron();
});
client.on(Events.InteractionCreate, (interaction) => {
if (!interaction.isChatInputCommand()) {
return;
}
void roll(interaction);
});
await client.login(process.env.BOT_TOKEN ?? "");
instantiateServer();
+65
View File
@@ -0,0 +1,65 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { randomInt } from "node:crypto";
import type { ChatInputCommandInteraction } from "discord.js";
/**
* Rolls dice and returns the result.
* @param interaction -- The interaction payload from the Discord API.
*/
export const roll
= async(interaction: ChatInputCommandInteraction): Promise<void> => {
const count = interaction.options.getInteger("count", true);
const die = interaction.options.getString("die", true);
const sides = Number.parseInt(die, 10);
const modifier = interaction.options.getNumber("modifier") ?? 0;
const type = interaction.options.getString("type", true);
const firstResult = Array.from({ length: count }, () => {
return randomInt(1, sides + 1);
});
if (type === "normal") {
const result = firstResult.reduce((a, b) => {
return a + b;
}, 0);
await interaction.reply(`Rolled ${String(count)}d${die}${modifier >= 0
? `+${String(modifier)}`
: String(modifier)}:
${firstResult.join(" + ")}
Modifier: ${modifier >= 0
? `+${String(modifier)}`
: `-${String(modifier)}`}
Total: **${String(result + modifier)}**`);
return;
}
const secondResult = Array.from({ length: count }, () => {
return randomInt(1, sides + 1);
});
const firstTotal = firstResult.reduce((a, b) => {
return a + b;
}, 0);
const secondTotal = secondResult.reduce((a, b) => {
return a + b;
}, 0);
const result = type === "advantage"
? Math.max(firstTotal, secondTotal)
: Math.min(firstTotal, secondTotal);
await interaction.reply(`Rolled ${String(count)}d${die}${modifier >= 0
? `+${String(modifier)}`
: String(modifier)} with ${type}:
${firstResult.join(" + ")}
${secondResult.join(" + ")}
Modifier: ${modifier >= 0
? `+${String(modifier)}`
: String(modifier)}
Total: **${String(result + modifier)}**`);
};
+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>Tyche</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Discord bot that rolls dice." />
<script src="https://cdn.nhcarrigan.com/headers/index.js" async defer></script>
</head>
<body>
<main>
<h1>Tyche</h1>
<img src="https://cdn.nhcarrigan.com/new-avatars/tyche-full.png" width="250" alt="Tyche" />
<section>
<p>Discord bot that rolls dice.</p>
<a href="https://discord.com/oauth2/authorize?client_id=1436837822656020663" 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 Discord
</a>
</section>
<section>
<h2>Links</h2>
<p>
<a href="https://git.nhcarrigan.com/nhcarrigan/tyche">
<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: 8123 }, (error) => {
if (error) {
void logger.error("instantiate server", error);
return;
}
void logger.log("debug", "Server listening on port 8123.");
});
} 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(
"Tyche",
process.env.LOG_TOKEN ?? "",
);