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

### Explanation

_No response_

### Issue

_No response_

### Attestations

- [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/)
- [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/).
- [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/).

### Dependencies

- [ ] I have pinned the dependencies to a specific patch version.

### Style

- [ ] I have run the linter and resolved any errors.
- [ ] My pull request uses an appropriate title, matching the conventional commit standards.
- [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request.

### Tests

- [ ] My contribution adds new code, and I have added tests to cover it.
- [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes.
- [ ] All new and existing tests pass locally with my changes.
- [ ] Code coverage remains at or above the configured threshold.

### Documentation

_No response_

### Versioning

_No response_

Reviewed-on: #7
Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com>
Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #7.
This commit is contained in:
2025-09-27 16:58:54 -07:00
committed by Naomi Carrigan
parent ad449e50a4
commit 64d5d4a9b5
21 changed files with 5618 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
TextDisplayBuilder,
SeparatorBuilder,
SeparatorSpacingSize,
ContainerBuilder,
ButtonBuilder,
ButtonStyle,
ActionRowBuilder,
} from "discord.js";
export const about = [
new ContainerBuilder().
addTextDisplayComponents(
new TextDisplayBuilder().setContent("# About Veluna"),
).
addTextDisplayComponents(
new TextDisplayBuilder().setContent(
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"Hi there~! I am Veluna, a bot that allows your community to ask you questions anonymously!",
),
).
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.
"To get started, a server admin must configure the channel where questions get posted (this should be a private channel to avoid moderation concerns) with `/questions` and the channel where answers get posted (this should be a public channel so the community can see responses) with `/answers`. Then, a server member can ask questions using the `/ask` command, and the admins can answer them!",
),
).
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 or on the forum, 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"),
),
];
+46
View File
@@ -0,0 +1,46 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { PrismaClient } from "@prisma/client";
import { Client, GatewayIntentBits, Events } from "discord.js";
import { handleButton } from "./modules/handleButton.js";
import { handleChatCommand } from "./modules/handleChatCommand.js";
import { handleModalSubmit } from "./modules/handleModalSubmit.js";
import { instantiateServer } from "./server/serve.js";
import { logger } from "./utils/logger.js";
import type { Veluna } from "./interfaces/veluna.js";
const veluna: Veluna = {
db: new PrismaClient(),
discord: new Client({
intents: [ GatewayIntentBits.Guilds ],
}),
};
veluna.discord.once(Events.ClientReady, () => {
void logger.log(
"debug",
`Client authenticated as ${veluna.discord.user?.username ?? "unknown user"}`,
);
});
veluna.discord.on(Events.InteractionCreate, (interaction) => {
if (!interaction.inCachedGuild()) {
return;
}
if (interaction.isButton()) {
void handleButton(interaction);
}
if (interaction.isModalSubmit()) {
void handleModalSubmit(veluna, interaction);
}
if (interaction.isChatInputCommand()) {
void handleChatCommand(veluna, interaction);
}
});
await veluna.discord.login(process.env.BOT_TOKEN);
instantiateServer();
+13
View File
@@ -0,0 +1,13 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import type { PrismaClient } from "@prisma/client";
import type { Client } from "discord.js";
export interface Veluna {
discord: Client;
db: PrismaClient;
}
+27
View File
@@ -0,0 +1,27 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
ModalBuilder,
TextInputBuilder,
ActionRowBuilder,
TextInputStyle,
} from "discord.js";
const row = new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder().
setCustomId("textinput").
setLabel("What is your answer?").
setStyle(TextInputStyle.Paragraph).
setMaxLength(1000).
setMinLength(1).
setRequired(true),
);
export const answer = new ModalBuilder().
setCustomId("answer").
setTitle("Provide an Answer!").
addComponents(row);
+27
View File
@@ -0,0 +1,27 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
ModalBuilder,
TextInputBuilder,
ActionRowBuilder,
TextInputStyle,
} from "discord.js";
const row = new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder().
setCustomId("textinput").
setLabel("What do you want to ask?").
setStyle(TextInputStyle.Paragraph).
setMaxLength(1000).
setMinLength(1).
setRequired(true),
);
export const ask = new ModalBuilder().
setCustomId("ask").
setTitle("Ask a Question!").
addComponents(row);
+27
View File
@@ -0,0 +1,27 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { answer } from "../modals/answer.js";
import { ask } from "../modals/ask.js";
import type { ButtonInteraction } from "discord.js";
/**
* Displays the appropriate modal when a button is pressed.
* @param interaction - The button interaction payload from Discord.
*/
export const handleButton = async(
interaction: ButtonInteraction,
): Promise<void> => {
const { customId } = interaction;
if (customId === "ask") {
await interaction.showModal(ask);
}
if (customId === "answer") {
await interaction.showModal(answer);
}
};
+107
View File
@@ -0,0 +1,107 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
MessageFlags,
PermissionFlagsBits,
ChannelType,
type ChatInputCommandInteraction,
} from "discord.js";
import { about } from "../components/about.js";
import { ask } from "../modals/ask.js";
import type { Veluna } from "../interfaces/veluna.js";
interface Query {
questionChannelId?: string;
answerChannelId?: string;
}
const buildQuery = (type: string, id: string): Query => {
if (type === "question") {
return { questionChannelId: id };
}
if (type === "answer") {
return { answerChannelId: id };
}
return {};
};
/**
* Handles the logic for slash commands. Identifies the command name
* and responds accordingly.
* @param veluna - Veluna's instance.
* @param interaction - The interaction payload from Discord.
*/
// eslint-disable-next-line max-lines-per-function -- Big boi function.
export const handleChatCommand = async(
veluna: Veluna,
interaction: ChatInputCommandInteraction<"cached">,
): Promise<void> => {
const { commandName, options, guild, member } = interaction;
if (commandName === "about") {
await interaction.reply({
components: about,
flags: [ MessageFlags.IsComponentsV2 ],
});
return;
}
if (commandName === "ask") {
await interaction.showModal(ask);
return;
}
// Permission gated commands w/ DB queries...
await interaction.deferReply({
flags: [ MessageFlags.Ephemeral ],
});
if (!member.permissions.has(PermissionFlagsBits.ManageGuild)) {
await interaction.editReply({
content: "You must be a server administrator to use this command.",
});
return;
}
// These both require a channel
const channel = options.
getChannel("channel", true, [ ChannelType.GuildText ]);
const me = await guild.members.fetchMe().
catch(() => {
return null;
});
if (me?.permissions.has([
PermissionFlagsBits.ViewChannel,
PermissionFlagsBits.SendMessages,
PermissionFlagsBits.EmbedLinks,
]) !== true) {
await interaction.editReply({
content:
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"I do not have permission to view and send messages in the channel provided. Please adjust my permissions and try again.",
});
return;
}
const query = buildQuery(commandName, channel.id);
await veluna.db.servers.upsert({
create: {
answerChannelId: query.answerChannelId ?? "",
blockedUsers: [],
questionChannelId: query.questionChannelId ?? "",
serverId: guild.id,
},
update: buildQuery(commandName, channel.id),
where: {
serverId: guild.id,
},
});
};
+163
View File
@@ -0,0 +1,163 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { MessageFlags, type ModalSubmitInteraction } from "discord.js";
import type { Veluna } from "../interfaces/veluna.js";
/**
* Handles modal submissions.
* @param veluna - Veluna's instance.
* @param interaction - The modal submit interaction payload from Discord.
*/
// eslint-disable-next-line max-lines-per-function, complexity, max-statements -- Big boi function.
export const handleModalSubmit = async(
veluna: Veluna,
interaction: ModalSubmitInteraction,
): Promise<void> => {
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
if (!interaction.inCachedGuild()) {
await interaction.editReply({
content: "This interaction can only be used in a server.",
});
return;
}
const record = await veluna.db.servers.findUnique({
where: {
serverId: interaction.guildId,
},
});
if (!record) {
await interaction.editReply({
content:
// eslint-disable-next-line stylistic/max-len -- Big boi string.
"This server is not registered in the database. Please configure your settings.",
});
return;
}
if (interaction.customId === "ask") {
if (record.questionChannelId === "") {
await interaction.editReply({
content: "This server has not set a question channel.",
});
return;
}
const question = interaction.fields.getTextInputValue("textinput");
const channel
= veluna.discord.channels.cache.get(record.questionChannelId)
?? await veluna.discord.channels.fetch(record.questionChannelId).
catch(() => {
return null;
});
if (channel?.isSendable() !== true) {
await interaction.editReply({
content: "The question channel set is not a text-based channel.",
});
return;
}
await channel.send({
components: [
{
components: [
{
// eslint-disable-next-line @typescript-eslint/naming-convention -- Discord API.
custom_id: "answer",
label: "Answer this question",
style: 3,
type: 2,
},
],
type: 1,
},
],
content: question,
});
}
if (interaction.customId === "answer") {
if (record.answerChannelId === "") {
await interaction.editReply({
content: "This server has not set an answer channel.",
});
return;
}
const { message, fields } = interaction;
if (!message) {
await interaction.editReply({
content: "An error occurred while fetching the message.",
});
return;
}
const { content: question } = message;
const answer = fields.getTextInputValue("textinput");
if (question === "") {
await interaction.editReply({
content: "An error occurred while fetching the question.",
});
}
const channel
= veluna.discord.channels.cache.get(record.answerChannelId)
?? await veluna.discord.channels.fetch(record.answerChannelId).
catch(() => {
return null;
});
if (channel?.isSendable() !== true) {
await interaction.editReply({
content: "The answer channel set is not a text-based channel.",
});
return;
}
await channel.send({
components: [
{
components: [
{
content: `# ${question}`,
type: 10,
},
{
divider: true,
spacing: 1,
type: 14,
},
{
content: answer,
type: 10,
},
{
divider: true,
spacing: 1,
type: 14,
},
{
content: `-# Brought to you by [NHCarrigan](<https://chat.nhcarrigan.com>)`,
type: 10,
},
],
spoiler: false,
type: 17,
},
{
components: [
{
// eslint-disable-next-line @typescript-eslint/naming-convention -- Discord API.
custom_id: "ask",
disabled: false,
label: "Ask your own?",
style: 3,
type: 2,
},
],
type: 1,
},
],
});
}
};
+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>Veluna</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="A Discord bot that allows your community to ask you anonymous questions!" />
<script src="https://cdn.nhcarrigan.com/headers/index.js" async defer></script>
</head>
<body>
<main>
<h1>Veluna</h1>
<img src="https://cdn.nhcarrigan.com/new-avatars/veluna-full.png" width="250" alt="Veluna" />
<section>
<p>A Discord bot that allows your community to ask you anonymous questions!</p>
<a href="https://discord.com/oauth2/authorize?client_id=1391505285465509978" 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/umbrelle">
<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: 6099 }, (error) => {
if (error) {
void logger.error("instantiate server", error);
return;
}
void logger.log("debug", "Server listening on port 6099.");
});
} 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(
"Veluna",
process.env.LOG_TOKEN ?? "",
);