wip medication and db (sqlite)

This commit is contained in:
Moritz Riese
2024-12-28 20:09:43 +01:00
parent 06ef16581c
commit 03d36358bf
19 changed files with 191 additions and 46 deletions

View File

@@ -0,0 +1,14 @@
import config from "config";
import client from "lib/client";
export class GreetingService {
async greet() {
const channels = client.channels;
const channel = channels.cache.get(config.discord.channelId);
if (channel?.isTextBased && channel?.isSendable()) {
await channel.send({ content: "HALLOOOO" });
}
}
}

View File

View File

@@ -0,0 +1,2 @@
export const greetContent = ["HALLOOOO", "guten morgen! ich hoffe es geht euch gut <3"];
export const sleepContent = ["gute nacht! ich muss jetzt schlafen gehen :c", "zzzzZZ..", "*schnarch*"];

View File

@@ -0,0 +1,34 @@
import config from "config";
import client from "lib/client";
import { getRandomInt } from "lib/utils";
import { greetContent, sleepContent } from "./greeting.components.ts";
export class GreetingService {
async greet() {
const channels = client.channels;
const channel = channels.cache.get(config.discord.channelId);
if (channel?.isTextBased && channel?.isSendable()) {
await channel.send({ content: this.getContent(false) });
}
}
async sleep() {
const channels = client.channels;
const channel = channels.cache.get(config.discord.channelId);
if (channel?.isTextBased && channel?.isSendable()) {
await channel.send({ content: this.getContent(true) });
}
}
getContent(asleep: boolean) {
if (asleep) {
return sleepContent[getRandomInt(0, sleepContent.length - 1)];
}
return greetContent[getRandomInt(0, greetContent.length - 1)];
}
}

View File

@@ -0,0 +1,26 @@
import { CronJob } from "cron";
import { GreetingService } from "actions/greeting/greeting.service";
const greetingService = new GreetingService();
new CronJob(
"0 30 6 * * *", // cronTime
async () => {
console.log("good morning");
await greetingService.greet();
}, // onTick
null, // onComplete
true, // start
"Europe/Berlin", // timeZone
);
new CronJob(
"0 30 22 * * *",
async () => {
console.log("good night");
await greetingService.sleep();
},
null,
true,
"Europe/Berlin",
);

View File

@@ -0,0 +1,10 @@
import { ButtonBuilder, ButtonStyle } from "discord.js";
export const yesButton = new ButtonBuilder()
.setCustomId("yesMedication")
.setLabel("ja")
.setStyle(ButtonStyle.Primary);
export const noButton = new ButtonBuilder()
.setCustomId("noMedication")
.setLabel("noch nicht")
.setStyle(ButtonStyle.Secondary);

View File

@@ -0,0 +1,94 @@
import { CronJob } from "cron";
import { getRandomInt } from "lib/utils";
import config from "config";
import client from "lib/client";
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
type CacheType,
type Interaction,
} from "discord.js";
import { yesButton, noButton } from "./medication.components";
export class MedicationService {
medication: string;
tookMedication: boolean;
constructor() {
this.medication = "";
this.tookMedication = false;
}
async askMedication() {
const channels = client.channels;
const channel = channels.cache.get(config.discord.channelId);
// funkt noch nicht, beides
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] });
}
}
getMedication() {
throw new Error("Method not implemented.");
}
setMedication() {
const reply = this.getReply();
console.log("medication");
// this.medication = input von user:in hier rein;
return {
reply,
}
}
getReply() {
return "medication reminder";
}
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.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") {
interaction.reply({
content: "das hast du toll gemacht <3 mach weiter so :3",
});
} else if (interaction.customId == "noMedication") {
interaction.reply({
content: "das passiert mal... aber versuch sie heute noch zu nehmen, oki? :)",
});
}
}
}
}

View File

@@ -0,0 +1,15 @@
import { CronJob } from "cron";
import { MedicationService } from "actions/medication/medication.service";
const medicationService = new MedicationService();
new CronJob(
"0 0 19 * * *", // cronTime
async () => {
console.log("askMedication()");
await medicationService.askMedication();
}, // onTick
null, // onComplete
true, // start
"Europe/Berlin", // timeZone
);

View File

@@ -0,0 +1,99 @@
import { CronJob } from "cron";
import { getRandomInt } from "lib/utils";
import config from "config";
import client from "lib/client";
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
type CacheType,
type Interaction,
} from "discord.js";
export class WaterMeService {
waterLevel: number;
private thirsty = 3 as const;
private enough = 10 as const;
constructor() {
this.waterLevel = 0;
}
getReply() {
const thirstyReplies = [
"... wow das wars schon??? ich brauche noch mehr wasser :(",
"dankeeeee!!!! ich waer fast verdurstet :(((",
"*roelpssssss*",
];
const fullReplies = [
"langsam reicht es :o",
"poah, das hat gut getan",
"das ist krass :3",
];
const tooMuchReplies = [
"ES REICHT!!!!",
"bitte hoer auf, ich platze gleich :(",
];
if (this.waterLevel <= this.thirsty) {
return thirstyReplies[getRandomInt(0, thirstyReplies.length - 1)];
}
if (this.waterLevel > this.thirsty && this.waterLevel <= this.enough) {
return fullReplies[getRandomInt(0, fullReplies.length - 1)];
}
if (this.waterLevel > this.enough) {
return tooMuchReplies[getRandomInt(0, tooMuchReplies.length - 1)];
}
}
async isThirsty() {
const channels = client.channels;
const channel = channels.cache.get(config.discord.channelId);
if (
channel?.isTextBased &&
channel?.isSendable() &&
this.waterLevel <= this.thirsty
) {
await channel.send({ content: "ich brauche wasser :(" });
}
}
waterMe() {
const reply = this.getReply();
this.waterLevel++;
console.log(this.waterLevel);
return {
reply,
};
}
async handleInteraction(interaction: Interaction<CacheType>) {
const result = this.waterMe();
const moreButton = new ButtonBuilder()
.setCustomId("moreWater")
.setLabel("mehr")
.setStyle(ButtonStyle.Secondary);
const row = new ActionRowBuilder().addComponents(moreButton);
if (interaction.isChatInputCommand()) {
await interaction.reply({
content: result.reply,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
components: [row as any],
});
} else if (interaction.isButton()) {
await interaction.reply({
content: result.reply,
});
}
}
}

View File

@@ -0,0 +1,16 @@
import { CronJob } from "cron";
import { WaterMeService } from "actions/waterMe/waterMe.service";
const waterMeService = new WaterMeService();
new CronJob(
"0 0 0 */1 * *", // cronTime
async () => {
console.log("isThirsty()");
await waterMeService.isThirsty();
}, // onTick
null, // onComplete
true, // start
"Europe/Berlin", // timeZone
);
// job.start() is optional here because of the fourth parameter set to true.