Skip to content

Commit 2e27a58

Browse files
committed
message Archive
1 parent fd8535a commit 2e27a58

11 files changed

Lines changed: 454 additions & 2 deletions

File tree

public/module.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,19 @@ declare module "botModule" {
4747
public permTitle: string | null;
4848
}
4949
export class Namespace {}
50-
export class DB {}
50+
export class DB {
51+
public database: any;
5152

53+
public getOrSetConfig(key: string, defaultValue: string): string;
54+
55+
public setConfig(key: string, value: string): void;
56+
}
57+
58+
export interface latestSub {
59+
name: string;
60+
pfpUrl: string;
61+
time: Date;
62+
}
5263
export class TalkingBot {
5364
/**
5465
* Will say message in every chat.
@@ -67,6 +78,7 @@ declare module "botModule" {
6778
public moduleManager: ModuleManager;
6879

6980
public modtext: string;
81+
public setLatestSub(sub: latestSub): void;
7082
}
7183
export interface MessageData {
7284
/**
@@ -101,6 +113,7 @@ declare module "botModule" {
101113
isCommand: boolean;
102114
rewardName?: string;
103115
isOld: boolean;
116+
timestamp: Date;
104117
reply: (message: string, replyToUser: boolean) => void | Promise<void>;
105118
/*
106119
* if duration is null bans permanently.

src/app.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ app.get("/soundEffects", async (_req, res) => {
3939
);
4040
});
4141

42+
app.get("/messageArchive", async (_, res) => {
43+
res.sendFile(__dirname + "/html/messagearchive.html");
44+
});
45+
46+
app.get("/getMessages", async (req, res) => {
47+
if (!req.query.date) {
48+
res.sendStatus(400);
49+
return;
50+
}
51+
const messages = bot.chatLogger.getMessages(req.query.date.toString());
52+
res.send(messages);
53+
});
54+
4255
app.use("/control", async (req, res) => {
4356
try {
4457
if (!bot.jwtSecret) {

src/botModule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface MessageData {
2525
isOld: boolean;
2626
isAction?: boolean;
2727
isTestRun?: boolean;
28+
timestamp: Date;
2829
reply: (message: string, replyToUser: boolean) => void | Promise<void>;
2930
banUser: (reason: string, duration?: number) => void | Promise<void>;
3031
}

src/chatLogger.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { MessageData } from "botModule";
2+
import { TalkingBot } from "./talkingbot";
3+
import { Database, Statement } from "bun:sqlite";
4+
interface SavedMessageData {
5+
badges: string;
6+
isUserMod: boolean;
7+
isUserVip?: boolean;
8+
isUserSub?: boolean;
9+
message: string;
10+
parsedMessage: string;
11+
username: string;
12+
sender: string;
13+
senderId: string;
14+
color: string;
15+
id: string;
16+
platform: string;
17+
channelId: string;
18+
isFirst: boolean;
19+
replyTo?: string;
20+
replyId?: string;
21+
replyText?: string;
22+
isCommand: boolean;
23+
rewardName?: string;
24+
isOld: boolean;
25+
timestamp: Date;
26+
}
27+
28+
function parseSaved(data: SavedMessageData): MessageData {
29+
return {
30+
...data,
31+
badges: JSON.parse(data.badges),
32+
timestamp: new Date(data.timestamp),
33+
} as MessageData;
34+
}
35+
36+
export class ChatLogger {
37+
private bot: TalkingBot;
38+
private database: Database;
39+
private insertMessage: CallableFunction;
40+
private getMessagesQuery: Statement;
41+
42+
constructor(bot: TalkingBot) {
43+
this.bot = bot;
44+
this.database = this.bot.database.database;
45+
this.database
46+
.query(
47+
"CREATE TABLE IF NOT EXISTS chat_messages (id TEXT PRIMARY KEY, username TEXT NOT NULL, sender TEXT, senderId TEXT, platform TEXT, channelId TEXT, message TEXT, parsedMessage TEXT, badges TEXT, isUserMod INTEGER, isUserVip INTEGER, isUserSub INTEGER, isFirst INTEGER, isCommand INTEGER, rewardName TEXT, replyTo TEXT, replyId TEXT, replyText TEXT,isOld INTEGER,color TEXT,timestamp DATETIME NOT NULL);",
48+
)
49+
.run();
50+
const insertMessageQuery = this.database.prepare(
51+
"INSERT OR REPLACE INTO chat_messages (id, username, sender, senderId, platform, channelId, message, parsedMessage, badges, isUserMod, isUserVip, isUserSub, isFirst, isCommand, rewardName, replyTo, replyId, replyText, isOld, color, timestamp) VALUES ( $id, $username, $sender, $senderId, $platform, $channelId, $message, $parsedMessage, $badges, $isUserMod, $isUserVip, $isUserSub, $isFirst, $isCommand, $rewardName, $replyTo, $replyId, $replyText, $isOld, $color, $timestamp);",
52+
);
53+
this.getMessagesQuery = this.database.prepare(
54+
" SELECT * FROM chat_messages WHERE date(timestamp) = date(?1) ORDER BY timestamp ASC; `); ",
55+
);
56+
this.insertMessage = this.database.transaction((message) => {
57+
insertMessageQuery.run(message);
58+
});
59+
}
60+
61+
// date like "2025-10-26"
62+
public getMessages(date: string): MessageData[] {
63+
const messages = this.getMessagesQuery.all(date) as SavedMessageData[];
64+
return messages.map(parseSaved);
65+
}
66+
67+
public recordMessage(message: MessageData) {
68+
this.insertMessage({
69+
...message,
70+
badges: JSON.stringify(message.badges),
71+
timestamp: message.timestamp.toISOString(),
72+
});
73+
}
74+
}

src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ export class MessageHandler {
12811281

12821282
private sendToChatList(data: MessageData) {
12831283
this.bot.iochat.emit("message", data);
1284+
this.bot.chatLogger.recordMessage(data);
12841285
}
12851286

12861287
public async handleMessage(data: MessageData) {

src/db.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MessageData } from "botModule";
12
import { Database, Statement } from "bun:sqlite";
23

34
interface WatchTime {
@@ -23,7 +24,7 @@ export interface HapbooReaction {
2324
export class DB {
2425
public getHapbooReaction: Statement;
2526

26-
private database: Database;
27+
public database: Database;
2728
private insertWatchTime: CallableFunction;
2829
private getWatchTimeQuery: Statement;
2930
private getTopWatchTimeQuery: Statement;

src/discord.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export class Discord {
267267
isAction: false,
268268
rewardName: "",
269269
username: message.author.username,
270+
timestamp: new Date(message.createdTimestamp),
270271
};
271272

272273
this.bot.commandHandler.handleCommand(data);

0 commit comments

Comments
 (0)