wip db
This commit is contained in:
@@ -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<CacheType>) {
|
||||
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: <explanation>
|
||||
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<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({
|
||||
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<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) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user