wip db
This commit is contained in:
parent
90341b277c
commit
182ce5af6b
17
drizzle.config.ts
Normal file
17
drizzle.config.ts
Normal file
@ -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,
|
||||||
|
},
|
||||||
|
});
|
8
drizzle/0000_needy_nightshade.sql
Normal file
8
drizzle/0000_needy_nightshade.sql
Normal file
@ -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`);
|
65
drizzle/meta/0000_snapshot.json
Normal file
65
drizzle/meta/0000_snapshot.json
Normal file
@ -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": {}
|
||||||
|
}
|
||||||
|
}
|
13
drizzle/meta/_journal.json
Normal file
13
drizzle/meta/_journal.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"version": "7",
|
||||||
|
"dialect": "sqlite",
|
||||||
|
"entries": [
|
||||||
|
{
|
||||||
|
"idx": 0,
|
||||||
|
"version": "6",
|
||||||
|
"when": 1735431085265,
|
||||||
|
"tag": "0000_needy_nightshade",
|
||||||
|
"breakpoints": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
BIN
mydb.sqlite
Normal file
BIN
mydb.sqlite
Normal file
Binary file not shown.
@ -10,14 +10,19 @@
|
|||||||
"drizzle-kit": "^0.30.1"
|
"drizzle-kit": "^0.30.1"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.7.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@discordjs/rest": "^2.4.0",
|
"@discordjs/rest": "^2.4.0",
|
||||||
|
"better-sqlite3": "^11.7.0",
|
||||||
"cron": "^3.3.1",
|
"cron": "^3.3.1",
|
||||||
"discord.js": "^14.16.3",
|
"discord.js": "^14.16.3",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"drizzle-orm": "^0.38.3",
|
"drizzle-orm": "^0.38.3",
|
||||||
"zod": "^3.24.1"
|
"zod": "^3.24.1"
|
||||||
}
|
},
|
||||||
|
"trustedDependencies": [
|
||||||
|
"better-sqlite3",
|
||||||
|
"esbuild"
|
||||||
|
]
|
||||||
}
|
}
|
@ -1 +0,0 @@
|
|||||||
DB_FILE_NAME=mydb.sqlite
|
|
@ -5,11 +5,17 @@ import client from "lib/client";
|
|||||||
import {
|
import {
|
||||||
ActionRowBuilder,
|
ActionRowBuilder,
|
||||||
ButtonBuilder,
|
ButtonBuilder,
|
||||||
|
ButtonInteraction,
|
||||||
ButtonStyle,
|
ButtonStyle,
|
||||||
|
ChatInputCommandInteraction,
|
||||||
|
ModalSubmitInteraction,
|
||||||
type CacheType,
|
type CacheType,
|
||||||
type Interaction,
|
type Interaction,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { yesButton, noButton } from "./medication.components";
|
import { yesButton, noButton } from "./medication.components";
|
||||||
|
import { db } from "db";
|
||||||
|
import { usersTable } from "db/schema"
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
export class MedicationService {
|
export class MedicationService {
|
||||||
medication: string;
|
medication: string;
|
||||||
@ -57,38 +63,125 @@ export class MedicationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handleInteraction(interaction: Interaction<CacheType>) {
|
async handleInteraction(interaction: Interaction<CacheType>) {
|
||||||
const result = this.setMedication();
|
if (interaction.isModalSubmit()) {
|
||||||
|
await this.handleModalSubmit(interaction);
|
||||||
const yesButton = new ButtonBuilder()
|
return;
|
||||||
.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.isChatInputCommand()) {
|
if (interaction.isChatInputCommand()) {
|
||||||
await interaction.reply({
|
await this.handleChatInputCommand(interaction);
|
||||||
content: result.reply,
|
return;
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
}
|
||||||
components: [row as any],
|
if (interaction.isButton()) {
|
||||||
});
|
await this.handleButton(interaction);
|
||||||
} else if (interaction.isButton()) {
|
return;
|
||||||
console.log("button interaction");
|
}
|
||||||
if (interaction.customId == "yesMedication") {
|
}
|
||||||
|
|
||||||
|
async handleModalSubmit(interaction: ModalSubmitInteraction<CacheType>) {
|
||||||
|
switch (interaction.customId) {
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleButton(interaction: ButtonInteraction<CacheType>) {
|
||||||
|
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({
|
interaction.reply({
|
||||||
content: "das hast du toll gemacht <3 mach weiter so :3",
|
content: "das hast du toll gemacht <3 mach weiter so :3",
|
||||||
});
|
});
|
||||||
} else if (interaction.customId == "noMedication") {
|
return;
|
||||||
|
case "noMedication":
|
||||||
interaction.reply({
|
interaction.reply({
|
||||||
content: "das passiert mal... aber versuch sie heute noch zu nehmen, oki? :)",
|
content: "das passiert mal... aber versuch sie heute noch zu nehmen, oki? :)",
|
||||||
});
|
});
|
||||||
}
|
return;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async handleChatInputCommand(interaction: ChatInputCommandInteraction<CacheType>) {
|
||||||
|
const result = this.setMedication();
|
||||||
|
|
||||||
|
const row = new ActionRowBuilder().addComponents(yesButton);
|
||||||
|
row.addComponents(noButton);
|
||||||
|
await interaction.reply({
|
||||||
|
content: result.reply,
|
||||||
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||||
|
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<string | null> {
|
||||||
|
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<number | null> {
|
||||||
|
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) {
|
||||||
|
}
|
||||||
}
|
}
|
3
src/db/index.ts
Normal file
3
src/db/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { drizzle } from "drizzle-orm/bun-sqlite";
|
||||||
|
|
||||||
|
export const db = drizzle(process.env.DB_FILE_NAME!);
|
@ -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),
|
||||||
|
});
|
42
src/index.ts
42
src/index.ts
@ -5,12 +5,46 @@ import "actions/drink/drink.task";
|
|||||||
import DiscordController from "controllers/discord.controller";
|
import DiscordController from "controllers/discord.controller";
|
||||||
|
|
||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
import { drizzle } from 'drizzle-orm/bun-sqlite';
|
/*import { drizzle } from 'drizzle-orm/bun-sqlite';
|
||||||
import { Database } from '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
|
// = main file
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user