diff --git a/bun.lockb b/bun.lockb index 0dfbcb6..f93e771 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/drizzle.config.ts b/drizzle.config.ts new file mode 100644 index 0000000..5330d22 --- /dev/null +++ b/drizzle.config.ts @@ -0,0 +1,17 @@ +import "dotenv/config"; +import { defineConfig } from "drizzle-kit"; + +const dbFileName = process.env.DB_FILE_NAME; + +if (!dbFileName) { + throw new Error("Die Umgebungsvariable DB_FILE_NAME ist nicht gesetzt."); +} + +export default defineConfig({ + out: "./drizzle", + schema: "./src/db/schema.ts", + dialect: "sqlite", + dbCredentials: { + url: dbFileName, + }, +}); diff --git a/drizzle/0000_needy_nightshade.sql b/drizzle/0000_needy_nightshade.sql new file mode 100644 index 0000000..ecc34d5 --- /dev/null +++ b/drizzle/0000_needy_nightshade.sql @@ -0,0 +1,8 @@ +CREATE TABLE `users_table` ( + `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, + `name` text NOT NULL, + `discord_id` integer NOT NULL, + `took_medication_today` integer DEFAULT 0 NOT NULL +); +--> statement-breakpoint +CREATE UNIQUE INDEX `users_table_discord_id_unique` ON `users_table` (`discord_id`); \ No newline at end of file diff --git a/drizzle/meta/0000_snapshot.json b/drizzle/meta/0000_snapshot.json new file mode 100644 index 0000000..138f2cb --- /dev/null +++ b/drizzle/meta/0000_snapshot.json @@ -0,0 +1,65 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "ddb7170b-7f66-4e3c-ab64-9069e760e09a", + "prevId": "00000000-0000-0000-0000-000000000000", + "tables": { + "users_table": { + "name": "users_table", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "discord_id": { + "name": "discord_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "took_medication_today": { + "name": "took_medication_today", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + } + }, + "indexes": { + "users_table_discord_id_unique": { + "name": "users_table_discord_id_unique", + "columns": [ + "discord_id" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json new file mode 100644 index 0000000..3c45250 --- /dev/null +++ b/drizzle/meta/_journal.json @@ -0,0 +1,13 @@ +{ + "version": "7", + "dialect": "sqlite", + "entries": [ + { + "idx": 0, + "version": "6", + "when": 1735431085265, + "tag": "0000_needy_nightshade", + "breakpoints": true + } + ] +} \ No newline at end of file diff --git a/mydb.sqlite b/mydb.sqlite new file mode 100644 index 0000000..6a8733d Binary files /dev/null and b/mydb.sqlite differ diff --git a/package.json b/package.json index e500e56..8d2c927 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,19 @@ "drizzle-kit": "^0.30.1" }, "peerDependencies": { - "typescript": "^5.0.0" + "typescript": "^5.7.2" }, "dependencies": { "@discordjs/rest": "^2.4.0", + "better-sqlite3": "^11.7.0", "cron": "^3.3.1", "discord.js": "^14.16.3", "dotenv": "^16.4.7", "drizzle-orm": "^0.38.3", "zod": "^3.24.1" - } + }, + "trustedDependencies": [ + "better-sqlite3", + "esbuild" + ] } \ No newline at end of file diff --git a/process.env b/process.env index d7e2499..e69de29 100644 --- a/process.env +++ b/process.env @@ -1 +0,0 @@ -DB_FILE_NAME=mydb.sqlite diff --git a/src/actions/medication/medication.service.ts b/src/actions/medication/medication.service.ts index 7abab37..2136f06 100644 --- a/src/actions/medication/medication.service.ts +++ b/src/actions/medication/medication.service.ts @@ -5,11 +5,17 @@ import client from "lib/client"; import { ActionRowBuilder, ButtonBuilder, + ButtonInteraction, ButtonStyle, + ChatInputCommandInteraction, + ModalSubmitInteraction, type CacheType, type Interaction, } from "discord.js"; import { yesButton, noButton } from "./medication.components"; +import { db } from "db"; +import { usersTable } from "db/schema" +import { eq } from "drizzle-orm"; export class MedicationService { medication: string; @@ -57,38 +63,125 @@ export class MedicationService { } async handleInteraction(interaction: Interaction) { - const result = this.setMedication(); - - const yesButton = new ButtonBuilder() - .setCustomId("yesMedication") - .setLabel("ja") - .setStyle(ButtonStyle.Primary); - const noButton = new ButtonBuilder() - .setCustomId("noMedication") - .setLabel("noch nicht") - .setStyle(ButtonStyle.Secondary); - - const row = new ActionRowBuilder().addComponents(yesButton); - row.addComponents(noButton); + if (interaction.isModalSubmit()) { + await this.handleModalSubmit(interaction); + return; + } if (interaction.isChatInputCommand()) { - await interaction.reply({ - content: result.reply, - // biome-ignore lint/suspicious/noExplicitAny: - components: [row as any], - }); - } else if (interaction.isButton()) { - console.log("button interaction"); - if (interaction.customId == "yesMedication") { + await this.handleChatInputCommand(interaction); + return; + } + if (interaction.isButton()) { + await this.handleButton(interaction); + return; + } + } + + async handleModalSubmit(interaction: ModalSubmitInteraction) { + switch (interaction.customId) { + default: + break; + } + } + + async handleButton(interaction: ButtonInteraction) { + console.log("button interaction"); + + const result = this.setMedication(); + const discordId = parseInt(interaction.user.id); + const id = this.getIdByDiscordId(discordId); + console.log("userid: " + discordId); + + + switch (interaction.customId) { + case "yesMedication": interaction.reply({ content: "das hast du toll gemacht <3 mach weiter so :3", }); - } else if (interaction.customId == "noMedication") { + return; + case "noMedication": interaction.reply({ content: "das passiert mal... aber versuch sie heute noch zu nehmen, oki? :)", }); - } - + return; + default: + break; } } + + async handleChatInputCommand(interaction: ChatInputCommandInteraction) { + const result = this.setMedication(); + + const row = new ActionRowBuilder().addComponents(yesButton); + row.addComponents(noButton); + await interaction.reply({ + content: result.reply, + // biome-ignore lint/suspicious/noExplicitAny: + components: [row as any], + }); + + } + + /** + * Methode, um die Benutzerdaten in die Datenbank zu schreiben. + * @param discordId unique user id + * @param name name how the user wants to get called by avocadi + * @param tookMedication if user took medication + */ + async logMedication(discordId: string, name: string, tookMedication: boolean) { + try { + // Versuche, den Benutzer zu speichern oder zu aktualisieren + await db.insert(usersTable).values({ + name: name, + discord_id: parseInt(discordId), + took_medication_today: Number(tookMedication), + }); + + console.log( + `Benutzer mit ID ${discordId} wurde in der Datenbank gespeichert.` + ); + } catch (error) { + console.error("Fehler beim Speichern in der Datenbank:", error); + } + } + + async getNameByDiscordId(discordId: number): Promise { + const result = await db + .select({ + name: usersTable.name, + }) + .from(usersTable) + .where(eq(usersTable.discord_id, discordId)) + .limit(1); + + if (result.length > 0) { + console.log("user found"); + return result[0].name; + } else { + console.log("name not found"); + return ""; + } + } + + async getIdByDiscordId(discordId: number): Promise { + const result = await db + .select({ + id: usersTable.id, + }) + .from(usersTable) + .where(eq(usersTable.discord_id, discordId)) + .limit(1); + + if (result.length > 0) { + console.log("id found"); + return result[0].id; + } else { + console.log("id not found"); + return null; + } + } + + async newEntry(discordId: number) { + } } \ No newline at end of file diff --git a/src/db/index.ts b/src/db/index.ts new file mode 100644 index 0000000..934d492 --- /dev/null +++ b/src/db/index.ts @@ -0,0 +1,3 @@ +import { drizzle } from "drizzle-orm/bun-sqlite"; + +export const db = drizzle(process.env.DB_FILE_NAME!); \ No newline at end of file diff --git a/src/db/schema.ts b/src/db/schema.ts index e69de29..23930b3 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -0,0 +1,8 @@ +import { int, sqliteTable, text } from "drizzle-orm/sqlite-core"; + +export const usersTable = sqliteTable("users_table", { + id: int().primaryKey({ autoIncrement: true }), + name: text().notNull(), + discord_id: int().notNull().unique(), + took_medication_today: int().notNull().default(0), +}); diff --git a/src/index.ts b/src/index.ts index 84aedfe..b1d2464 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,12 +5,46 @@ import "actions/drink/drink.task"; import DiscordController from "controllers/discord.controller"; import 'dotenv/config'; -import { drizzle } from 'drizzle-orm/bun-sqlite'; -import { Database } from 'bun:sqlite'; +/*import { drizzle } from 'drizzle-orm/bun-sqlite'; +import { eq } from 'drizzle-orm'; +import { usersTable } from './db/schema'; +import { db } from "./db" // from index -const sqlite = new Database(process.env.DB_FILE_NAME!); -const db = drizzle({ client: sqlite }); +async function main() { + const user: typeof usersTable.$inferInsert = { + name: 'John', + age: 30, + email: 'john@example.com', + }; + + await db.insert(usersTable).values(user); + console.log('New user created!') + + const users = await db.select().from(usersTable); + console.log('Getting all users from the database: ', users) + /* + const users: { + id: number; + name: string; + age: number; + email: string; + }[] + */ +/* +await db + .update(usersTable) + .set({ + age: 31, + }) + .where(eq(usersTable.email, user.email)); +console.log('User info updated!') + +await db.delete(usersTable).where(eq(usersTable.email, user.email)); +console.log('User deleted!') +} + +main();*/ // = main file