refactor water me service

remove "text-based-feature"
improve messages service interface
implement more features for fluxer
This commit is contained in:
2026-02-18 18:38:09 +01:00
parent 84b851f60f
commit 0a460800b6
20 changed files with 202 additions and 56 deletions

View File

@@ -43,9 +43,8 @@
"./features/dynamic-voice-channels/dynamic-voice-channels.schema": "./dist/features/dynamic-voice-channels/dynamic-voice-channels.schema.js",
"./features/dynamic-voice-channels/dynamic-voice-channels.service": "./dist/features/dynamic-voice-channels/dynamic-voice-channels.service.js",
"./features/greeting/greeting.service": "./dist/features/greeting/greeting.service.js",
"./features/text-based-feature/text-based-feature": "./dist/features/text-based-feature/text-based-feature.js",
"./features/text-based-feature/text-based-feature.schema": "./dist/features/text-based-feature/text-based-feature.schema.js",
"./features/water-me/water-me.service": "./dist/features/water-me/water-me.service.js",
"./lib/common": "./dist/lib/common.js",
"./lib/logger": "./dist/lib/logger.js",
"./lib/utils": "./dist/lib/utils.js",
"./lib/utils.test": "./dist/lib/utils.test.js",

View File

@@ -1,5 +1,19 @@
export interface MessagesServiceInterface<U = unknown, M = unknown> {
sendToUser(user: U, message: string): Promise<void>;
import type {
BaseChannel,
BaseCreateMessage,
BaseMessage,
BaseUser,
} from "lib/common";
export interface MessagesServiceInterface<
U extends BaseUser = BaseUser,
M extends BaseMessage = BaseMessage,
C extends BaseChannel = BaseChannel,
CM extends BaseCreateMessage = BaseCreateMessage,
> {
sendToUser(user: U, createMessageInput: CM): Promise<void>;
sendToChannel(channel: C, createMessageInput: CM): Promise<void>;
logMessage(message: M): Promise<void>;
}

View File

@@ -1,9 +0,0 @@
export type TextBasedFeatureHandleMessageSend = (
message: string,
channelId: string,
) => void | Promise<void>;
export type TextBasedFeatureInput = {
channelId: string;
messagesService: TextBasedFeatureHandleMessageSend;
};

View File

@@ -1,18 +0,0 @@
import type {
TextBasedFeatureHandleMessageSend,
TextBasedFeatureInput,
} from "./text-based-feature.schema";
export class TextBasedFeature {
channelId: string;
handleMessageSend: TextBasedFeatureHandleMessageSend;
constructor(input: TextBasedFeatureInput) {
this.channelId = input.channelId;
this.handleMessageSend = input.handleMessageSend;
}
async sendMessage(input: { content: string }) {
this.handleMessageSend(input.content, this.channelId);
}
}

View File

@@ -1,19 +1,19 @@
import { TextBasedFeature } from "features/text-based-feature/text-based-feature";
import type { TextBasedFeatureInput } from "features/text-based-feature/text-based-feature.schema";
import type { MessagesServiceInterface } from "entities/messages/messages.service";
import { createLogger } from "lib/logger";
import { getRandomInt } from "lib/utils";
export class WaterMeService extends TextBasedFeature {
export class WaterMeService {
waterLevel: number;
private logger = createLogger("WaterMeService");
private thirsty = 3 as const;
private enough = 10 as const;
messagesService: MessagesServiceInterface;
constructor(input: TextBasedFeatureInput) {
super(input);
constructor(messagesService: MessagesServiceInterface) {
this.waterLevel = 0;
this.messagesService = messagesService;
}
getReply() {
@@ -47,7 +47,10 @@ export class WaterMeService extends TextBasedFeature {
async notifyIfThirsty() {
if (this.waterLevel <= this.thirsty) {
await this.sendMessage({ content: "ich brauche wasser :(" });
await this.messagesService.sendToChannel(
{ id: "channelId" },
{ content: "ich brauche wasser :(" },
);
}
}

20
core/src/lib/common.ts Normal file
View File

@@ -0,0 +1,20 @@
export type BaseMessage = {
content: string;
};
export type BaseChannel = {
id: string;
};
export type BaseUser = {
id: string;
};
export type BaseCreateMessage =
| {
content?: string;
embeds?: unknown[] | null;
attachments?: unknown[] | null;
files?: unknown[] | null;
}
| string;