generated from nhcarrigan/template
feat: initial prototype #7
@@ -0,0 +1 @@
|
||||
import { Naomi
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
prod
|
||||
@@ -0,0 +1,30 @@
|
||||
import { SlashCommandBuilder, ChannelType } from "discord.js";
|
||||
|
||||
const about = new SlashCommandBuilder()
|
||||
.setName("about")
|
||||
.setDescription("Get information about the bot.");
|
||||
|
||||
const question = new SlashCommandBuilder()
|
||||
.setName("questions")
|
||||
.setDescription("Configure the PRIVATE text channel where anonymous questions should be sent.")
|
||||
.addChannelOption(
|
||||
option => option.setName("channel").setDescription("Your question channel.").setRequired(true).addChannelTypes(ChannelType.GuildText)
|
||||
);
|
||||
|
||||
const answer = new SlashCommandBuilder()
|
||||
.setName("answers")
|
||||
.setDescription("Configure the PUBLIC text channel where answers should be posted.")
|
||||
.addChannelOption(
|
||||
option => option.setName("channel").setDescription("Your question channel.").setRequired(true).addChannelTypes(ChannelType.GuildText)
|
||||
);
|
||||
|
||||
const ask = new SlashCommandBuilder()
|
||||
.setName("ask")
|
||||
.setDescription("Ask a question!");
|
||||
|
||||
console.log(JSON.stringify([
|
||||
about.toJSON(),
|
||||
question.toJSON(),
|
||||
answer.toJSON(),
|
||||
ask.toJSON()
|
||||
]));
|
||||
@@ -0,0 +1,5 @@
|
||||
import NaomisConfig from "@nhcarrigan/eslint-config";
|
||||
|
||||
export default [
|
||||
...NaomisConfig
|
||||
];
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "veluna",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "prisma generate && tsc",
|
||||
"lint": "eslint src --max-warnings 0",
|
||||
"start": "op run --env-file=prod.env -- node prod/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 0"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"packageManager": "pnpm@10.15.1",
|
||||
"devDependencies": {
|
||||
"@nhcarrigan/eslint-config": "5.2.0",
|
||||
"@nhcarrigan/typescript-config": "4.0.0",
|
||||
"@types/node": "24.3.1",
|
||||
"eslint": "9.34.0",
|
||||
"prisma": "6.15.0",
|
||||
"typescript": "5.9.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nhcarrigan/logger": "1.0.0",
|
||||
"@prisma/client": "6.15.0",
|
||||
"discord.js": "14.22.1",
|
||||
"fastify": "5.5.0"
|
||||
}
|
||||
}
|
||||
Generated
+4912
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "mongodb"
|
||||
url = env("MONGO_URI")
|
||||
}
|
||||
|
||||
model Servers {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
serverId String @unique
|
||||
questionChannelId String
|
||||
answerChannelId String
|
||||
blockedUsers String[]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
LOG_TOKEN="op://Environment Variables - Naomi/Alert Server/api_auth"
|
||||
BOT_TOKEN="op://Environment Variables - Naomi/Veluna/bot token"
|
||||
MONGO_URI="op://Environment Variables - Naomi/Veluna/mongo"
|
||||
@@ -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"),
|
||||
),
|
||||
];
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { Client, GatewayIntentBits, Events } from "discord.js";
|
||||
import { handleChatCommand } from "./modules/handleChatCommand.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()) {
|
||||
// Do button stuff
|
||||
}
|
||||
if (interaction.isModalSubmit()) {
|
||||
// Do modal stuff
|
||||
}
|
||||
if (interaction.isChatInputCommand()) {
|
||||
void handleChatCommand(veluna, interaction);
|
||||
}
|
||||
});
|
||||
|
||||
await veluna.discord.login(process.env.BOT_TOKEN);
|
||||
instantiateServer();
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @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().
|
||||
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);
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
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 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,
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -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)));
|
||||
}
|
||||
};
|
||||
@@ -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 ?? "",
|
||||
);
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "@nhcarrigan/typescript-config",
|
||||
"compilerOptions": {
|
||||
"rootDir": "./src",
|
||||
"outDir": "./prod"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user