|
|
|
|
@@ -3,185 +3,248 @@ import { getRandomInt } from "lib/utils";
|
|
|
|
|
import config from "config";
|
|
|
|
|
import client from "lib/client";
|
|
|
|
|
import {
|
|
|
|
|
ActionRowBuilder,
|
|
|
|
|
ButtonBuilder,
|
|
|
|
|
ButtonInteraction,
|
|
|
|
|
ButtonStyle,
|
|
|
|
|
ChatInputCommandInteraction,
|
|
|
|
|
ModalSubmitInteraction,
|
|
|
|
|
type CacheType,
|
|
|
|
|
type Interaction,
|
|
|
|
|
ActionRowBuilder,
|
|
|
|
|
ButtonBuilder,
|
|
|
|
|
type ButtonInteraction,
|
|
|
|
|
ButtonStyle,
|
|
|
|
|
type ChatInputCommandInteraction,
|
|
|
|
|
type ModalSubmitInteraction,
|
|
|
|
|
type CacheType,
|
|
|
|
|
type Interaction,
|
|
|
|
|
} from "discord.js";
|
|
|
|
|
import { yesButton, noButton } from "./medication.components";
|
|
|
|
|
import { db } from "db";
|
|
|
|
|
import { usersTable } from "db/schema"
|
|
|
|
|
import { usersTable } from "db/schema";
|
|
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
|
|
|
|
|
export class MedicationService {
|
|
|
|
|
medication: string;
|
|
|
|
|
tookMedication: boolean;
|
|
|
|
|
medication: string;
|
|
|
|
|
tookMedication: boolean;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
this.medication = "";
|
|
|
|
|
this.tookMedication = false;
|
|
|
|
|
}
|
|
|
|
|
constructor() {
|
|
|
|
|
this.medication = "";
|
|
|
|
|
this.tookMedication = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async askMedication() {
|
|
|
|
|
const channels = client.channels;
|
|
|
|
|
async askMedication() {
|
|
|
|
|
const channels = client.channels;
|
|
|
|
|
|
|
|
|
|
const channel = channels.cache.get(config.discord.channelId);
|
|
|
|
|
const channel = channels.cache.get(config.discord.channelId);
|
|
|
|
|
|
|
|
|
|
// funkt noch nicht, beides
|
|
|
|
|
// funkt noch nicht, beides
|
|
|
|
|
|
|
|
|
|
const row = new ActionRowBuilder().addComponents(yesButton);
|
|
|
|
|
row.addComponents(noButton);
|
|
|
|
|
const row = new ActionRowBuilder().addComponents(yesButton);
|
|
|
|
|
row.addComponents(noButton);
|
|
|
|
|
|
|
|
|
|
if (channel?.isTextBased &&
|
|
|
|
|
channel?.isSendable() &&
|
|
|
|
|
this.tookMedication == false) {
|
|
|
|
|
await channel.send({ content: "hast du schon deine medis genommen? :)", components: [row as any] });
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
channel?.isTextBased &&
|
|
|
|
|
channel?.isSendable() &&
|
|
|
|
|
this.tookMedication === false
|
|
|
|
|
) {
|
|
|
|
|
await channel.send({
|
|
|
|
|
content: "hast du schon deine medis genommen? :)",
|
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
|
|
|
components: [row as any],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
getMedication() {
|
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMedication() {
|
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
|
}
|
|
|
|
|
setMedication() {
|
|
|
|
|
const reply = this.getReply();
|
|
|
|
|
console.log("medication");
|
|
|
|
|
|
|
|
|
|
setMedication() {
|
|
|
|
|
const reply = this.getReply();
|
|
|
|
|
console.log("medication");
|
|
|
|
|
// this.medication = input von user:in hier rein;
|
|
|
|
|
return {
|
|
|
|
|
reply,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this.medication = input von user:in hier rein;
|
|
|
|
|
return {
|
|
|
|
|
reply,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
getReply() {
|
|
|
|
|
return "medication reminder";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReply() {
|
|
|
|
|
return "medication reminder";
|
|
|
|
|
}
|
|
|
|
|
async handleInteraction(interaction: Interaction<CacheType>) {
|
|
|
|
|
if (interaction.isModalSubmit()) {
|
|
|
|
|
await this.handleModalSubmit(interaction);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handleInteraction(interaction: Interaction<CacheType>) {
|
|
|
|
|
if (interaction.isModalSubmit()) {
|
|
|
|
|
await this.handleModalSubmit(interaction);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (interaction.isChatInputCommand()) {
|
|
|
|
|
await this.handleChatInputCommand(interaction);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (interaction.isButton()) {
|
|
|
|
|
await this.handleButton(interaction);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (interaction.isChatInputCommand()) {
|
|
|
|
|
await this.handleChatInputCommand(interaction);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (interaction.isButton()) {
|
|
|
|
|
await this.handleButton(interaction);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async handleModalSubmit(interaction: ModalSubmitInteraction<CacheType>) {
|
|
|
|
|
switch (interaction.customId) {
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handleModalSubmit(interaction: ModalSubmitInteraction<CacheType>) {
|
|
|
|
|
switch (interaction.customId) {
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async handleButton(interaction: ButtonInteraction<CacheType>) {
|
|
|
|
|
console.log("button interaction");
|
|
|
|
|
|
|
|
|
|
async handleButton(interaction: ButtonInteraction<CacheType>) {
|
|
|
|
|
console.log("button interaction");
|
|
|
|
|
const discordId = Number.parseInt(interaction.user.id);
|
|
|
|
|
const name = interaction.user.displayName;
|
|
|
|
|
|
|
|
|
|
const result = this.setMedication();
|
|
|
|
|
const discordId = parseInt(interaction.user.id);
|
|
|
|
|
const id = this.getIdByDiscordId(discordId);
|
|
|
|
|
console.log("userid: " + discordId);
|
|
|
|
|
console.log(`userid: ${discordId}`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const userId = await this.ensureUserExists(discordId, name);
|
|
|
|
|
console.log(`userid: ${userId}`);
|
|
|
|
|
|
|
|
|
|
switch (interaction.customId) {
|
|
|
|
|
case "yesMedication":
|
|
|
|
|
interaction.reply({
|
|
|
|
|
content: "das hast du toll gemacht <3 mach weiter so :3",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
case "noMedication":
|
|
|
|
|
interaction.reply({
|
|
|
|
|
content: "das passiert mal... aber versuch sie heute noch zu nehmen, oki? :)",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const tookMedication = interaction.customId === "yesMedication";
|
|
|
|
|
|
|
|
|
|
async handleChatInputCommand(interaction: ChatInputCommandInteraction<CacheType>) {
|
|
|
|
|
const result = this.setMedication();
|
|
|
|
|
interaction.reply({
|
|
|
|
|
content: tookMedication
|
|
|
|
|
? "das hast du toll gemacht <3 mach weiter so :3"
|
|
|
|
|
: "das passiert mal... aber versuch sie heute noch zu nehmen, oki? :)",
|
|
|
|
|
});
|
|
|
|
|
await this.logMedication(userId, tookMedication);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("error ensuring user exists:", error);
|
|
|
|
|
await interaction.reply({
|
|
|
|
|
content:
|
|
|
|
|
"es gab einen fehler beim verarbeiten deiner anfrage :( versuch es bitte spaeter nochmal, oki? c:",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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],
|
|
|
|
|
});
|
|
|
|
|
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),
|
|
|
|
|
});
|
|
|
|
|
/**
|
|
|
|
|
* 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(id: number, tookMedication: boolean) {
|
|
|
|
|
try {
|
|
|
|
|
await db
|
|
|
|
|
.update(usersTable)
|
|
|
|
|
.set({
|
|
|
|
|
took_medication_today: Number(tookMedication),
|
|
|
|
|
})
|
|
|
|
|
.where(eq(usersTable.id, id));
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
`Benutzer mit ID ${discordId} wurde in der Datenbank gespeichert.`
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Fehler beim Speichern in der Datenbank:", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* await db.insert(usersTable).values({
|
|
|
|
|
name: name,
|
|
|
|
|
discord_id: discordId,
|
|
|
|
|
took_medication_today: Number(tookMedication),
|
|
|
|
|
}); */
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
console.log(`Benutzer mit ID ${id} wurde in der Datenbank gespeichert.`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Fehler beim Speichern in der Datenbank:", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.length > 0) {
|
|
|
|
|
console.log("user found");
|
|
|
|
|
return result[0].name;
|
|
|
|
|
} else {
|
|
|
|
|
console.log("name not found");
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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("user found");
|
|
|
|
|
return result[0].name;
|
|
|
|
|
}
|
|
|
|
|
console.log("name not found");
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.length > 0) {
|
|
|
|
|
console.log("id found");
|
|
|
|
|
return result[0].id;
|
|
|
|
|
} else {
|
|
|
|
|
console.log("id not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async findUserIdByDiscordId(discordId: number): Promise<number | null> {
|
|
|
|
|
try {
|
|
|
|
|
const result = await db
|
|
|
|
|
.select({
|
|
|
|
|
id: usersTable.id,
|
|
|
|
|
})
|
|
|
|
|
.from(usersTable)
|
|
|
|
|
.where(eq(usersTable.discord_id, discordId))
|
|
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
|
|
async newEntry(discordId: number) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (result.length > 0) {
|
|
|
|
|
console.log(`ID für Discord-ID ${discordId} gefunden: ${result[0].id}`);
|
|
|
|
|
return result[0].id;
|
|
|
|
|
}
|
|
|
|
|
console.log(`Keine ID für Discord-ID ${discordId} gefunden.`);
|
|
|
|
|
return null;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(
|
|
|
|
|
`Fehler beim Abrufen der ID für Discord-ID ${discordId}:`,
|
|
|
|
|
error,
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async ensureUserExists(discordId: number, name: string): Promise<number> {
|
|
|
|
|
try {
|
|
|
|
|
const userId = await this.findUserIdByDiscordId(discordId);
|
|
|
|
|
|
|
|
|
|
if (userId !== null) {
|
|
|
|
|
console.log(`entry for discordID ${discordId} already exists`);
|
|
|
|
|
return userId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
`found no entry for discordID ${discordId}. creating new entry.`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const result = await db
|
|
|
|
|
.insert(usersTable)
|
|
|
|
|
.values({
|
|
|
|
|
name: name,
|
|
|
|
|
discord_id: discordId,
|
|
|
|
|
})
|
|
|
|
|
.onConflictDoNothing()
|
|
|
|
|
.returning({
|
|
|
|
|
id: usersTable.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result.length > 0) {
|
|
|
|
|
const newUserId = result[0].id;
|
|
|
|
|
console.log(
|
|
|
|
|
`new user with discordId ${discordId} and name ${name} created. id: ${newUserId}`,
|
|
|
|
|
);
|
|
|
|
|
return newUserId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check again if user is now existing
|
|
|
|
|
const newUserId = await this.findUserIdByDiscordId(discordId);
|
|
|
|
|
if (newUserId !== null) {
|
|
|
|
|
console.log(`user created in parallel. fetched id: ${newUserId}`);
|
|
|
|
|
return newUserId;
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`creating a new user for discordId ${discordId} failed`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("error while creating or calling the user:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|