i hyperfocused and here is v1

This commit is contained in:
Rose 2024-04-19 00:15:20 -04:00
parent 8f43e6a6a9
commit 4090fd621e
No known key found for this signature in database
34 changed files with 4135 additions and 22 deletions

38
apps/bot/Dockerfile Normal file
View file

@ -0,0 +1,38 @@
FROM node:21-alpine AS base
FROM base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update
RUN yarn set version canary
RUN yarn config set nodeLinker node-modules
WORKDIR /app
COPY . .
RUN yarn dlx turbo prune @datamine/bot --docker
FROM base AS installer
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock
RUN yarn install
COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json
RUN yarn turbo build --filter=bot...
RUN yarn workspace @datamine/database run drizzle
FROM base AS runner
WORKDIR /app
# Don't run production as root
RUN addgroup --system --gid 1001 datamine
RUN adduser --system --uid 1001 datamine
USER datamine
COPY --from=installer /app .
CMD node apps/bot/dist/main.js

1
apps/bot/README.md Normal file
View file

@ -0,0 +1 @@
# @datamine/bot

4
apps/bot/biome.json Normal file
View file

@ -0,0 +1,4 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.4/schema.json",
"extends": ["@datamine/config/biome"]
}

34
apps/bot/package.json Normal file
View file

@ -0,0 +1,34 @@
{
"name": "@datamine/bot",
"packageManager": "yarn@4.1.1",
"private": true,
"type": "module",
"main": "./dist/main.js",
"module": "./dist/main.js",
"types": "./dist/main.d.ts",
"exports": [
"./dist/commands/subscribe.js",
"./dist/commands/unsubscribe.js"
],
"devDependencies": {
"@biomejs/biome": "1.7.0",
"@datamine/config": "workspace:*",
"@types/node": "20.12.7",
"discord-api-types": "0.37.79",
"pkgroll": "2.0.2",
"tsx": "4.7.2",
"typescript": "5.4.5"
},
"scripts": {
"dev": "tsx ./src/main.ts",
"lint": "biome check ./src/**/*",
"build": "pkgroll"
},
"dependencies": {
"@datamine/database": "workspace:*",
"@sapphire/framework": "5.1.0",
"discord.js": "14.x",
"dotenv": "16.4.5",
"drizzle-orm": "0.30.8"
}
}

View file

@ -0,0 +1,83 @@
import { database, Schema } from "@datamine/database";
import { Command } from "@sapphire/framework";
import { ChannelType } from "discord.js";
export class SubscribeCommand extends Command {
public constructor(
context: Command.LoaderContext,
options: Command.Options,
) {
super(context, { ...options });
}
public override registerApplicationCommands(
registry: Command.Registry,
) {
registry.registerChatInputCommand((builder) =>
builder
.setName("subscribe")
.setDescription("Subscribe to the Datamine updates.")
.addChannelOption((option) =>
option
.setName("channel")
.setDescription("The channel to send updates to.")
.addChannelTypes(ChannelType.GuildText),
)
.addRoleOption((option) =>
option
.setName("role")
.setDescription("The role to send updates to."),
),
);
}
public async chatInputRun(
interaction: Command.ChatInputCommandInteraction,
) {
if (!interaction.inGuild()) return;
if (!interaction.memberPermissions.has("ManageWebhooks")) {
return interaction.reply({
content: "You do not have permission to use this command.",
ephemeral: true,
});
}
const channel = interaction.options.getChannel("channel", false, [
ChannelType.GuildText,
]);
const role = interaction.options.getRole("role");
const data = {
channel: channel
? BigInt(channel.id)
: BigInt(interaction.channelId),
role: role ? BigInt(role.id) : undefined,
};
try {
await database
.insert(Schema.servers)
.values({
id: BigInt(interaction.guildId),
...data,
})
.onConflictDoUpdate({
target: Schema.servers.id,
set: data,
});
} catch (error) {
return interaction.reply({
content:
"An error occurred while subscribing to Datamine updates.",
ephemeral: true,
});
}
return interaction.reply({
content: `Datamine posts will now be posted into <#${
data.channel
}> ${data.role ? `and mention <@&${data.role}>` : ""}.`,
ephemeral: true,
});
}
}

View file

@ -0,0 +1,53 @@
import { Drizzle, Schema, database } from "@datamine/database";
import { Command } from "@sapphire/framework";
export class SubscribeCommand extends Command {
public constructor(
context: Command.LoaderContext,
options: Command.Options,
) {
super(context, { ...options });
}
public override registerApplicationCommands(
registry: Command.Registry,
) {
registry.registerChatInputCommand((builder) =>
builder
.setName("unsubscribe")
.setDescription("Unsubscribe from the Datamine updates."),
);
}
public async chatInputRun(
interaction: Command.ChatInputCommandInteraction,
) {
if (!interaction.inGuild()) return;
if (!interaction.memberPermissions.has("ManageWebhooks")) {
return interaction.reply({
content: "You do not have permission to use this command.",
ephemeral: true,
});
}
try {
await database
.delete(Schema.servers)
.where(
Drizzle.eq(Schema.servers.id, BigInt(interaction.guildId)),
);
} catch (error) {
return interaction.reply({
content:
"An error occurred while subscribing to Datamine updates.",
ephemeral: true,
});
}
return interaction.reply({
content:
"Datamine posts will no longer be posted in this server.",
ephemeral: true,
});
}
}

15
apps/bot/src/main.ts Normal file
View file

@ -0,0 +1,15 @@
import "dotenv/config";
import { SapphireClient } from "@sapphire/framework";
import { resolve } from "node:path";
if (!process.env.DISCORD_BOT_TOKEN) {
throw new Error("DISCORD_BOT_TOKEN is not defined");
}
const client = new SapphireClient({
intents: [],
baseUserDirectory: resolve(import.meta.dirname),
});
client.login(process.env.DISCORD_BOT_TOKEN);

5
apps/bot/tsconfig.json Normal file
View file

@ -0,0 +1,5 @@
{
"extends": "@datamine/config/typescript",
"exclude": ["node_modules"],
"include": ["src"]
}