Skip to content

Commit 5420063

Browse files
committed
refactor: Improve user context retrieval and enhance channel information in AiChat
1 parent 118ace2 commit 5420063

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/events/ai/ai.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { google } from "@ai-sdk/google";
22
import { generateText, ModelMessage, StepResult } from "ai";
33
import console, { error } from "console";
4-
import { Message, OmitPartialGroupDMChannel } from "discord.js";
4+
import { Message, OmitPartialGroupDMChannel, ThreadChannel } from "discord.js";
55
import type { ArgsOf, Client } from "discordx";
66
import { Discord, On } from "discordx";
77
import { ConfigValidator } from "../../lib/config-validator";
@@ -60,10 +60,7 @@ export class AiChat {
6060
const messages = channelMessages.get(message.channel.id) || [];
6161

6262
// Get user context
63-
const userContext = await this.getUserContext(
64-
message.author.id,
65-
message.guildId!
66-
);
63+
const userContext = await this.getUserContext(message.author.id, message);
6764
const fullMessage = `${userMsg}${replyContext}${userContext}`;
6865

6966
try {
@@ -124,21 +121,42 @@ export class AiChat {
124121

125122
private async getUserContext(
126123
memberId: string,
127-
guildId: string
124+
message: Message
128125
): Promise<string> {
129126
try {
130-
const userStats = await StatsService.getUserStatsEmbed(memberId, guildId);
127+
const userStats = await StatsService.getUserStatsEmbed(
128+
memberId,
129+
message.guildId!
130+
);
131131

132132
if (!userStats) {
133133
return "\n\n[User Context: New member, no stats available]";
134134
}
135135

136136
const { embed, roles } = userStats;
137137

138-
// Simple JSON stringify approach
138+
// Get channel information
139+
let channelInfo = "";
140+
const channel = message.channel;
141+
142+
if (channel.isThread()) {
143+
const thread = channel as ThreadChannel;
144+
try {
145+
const starterMessage = await thread.fetchStarterMessage();
146+
channelInfo = `Thread: "${thread.name}" in #${thread.parent?.name || "unknown"}\nOriginal post: "${starterMessage?.content?.substring(0, 200) || "No content"}${starterMessage?.content && starterMessage.content.length > 200 ? "..." : ""}"`;
147+
} catch (error) {
148+
channelInfo = `Thread: "${thread.name}" in #${thread.parent?.name || "unknown"}`;
149+
}
150+
} else if ("name" in channel) {
151+
channelInfo = `Channel: #${channel.name}`;
152+
} else {
153+
channelInfo = "Channel: DM or unknown";
154+
}
155+
139156
const contextData = {
140157
embed,
141158
roles: roles?.filter(Boolean) || [],
159+
location: channelInfo,
142160
};
143161

144162
return `\n\n[User Context: ${JSON.stringify(contextData, null, 2)}]`;
@@ -186,7 +204,8 @@ export class AiChat {
186204
// Get context for the replied user too
187205
const repliedUserContext = await this.getUserContext(
188206
repliedUser.id,
189-
message.guildId!
207+
208+
message
190209
);
191210

192211
return {

src/events/ai/prompt.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ SERVER FEATURES & COMMANDS:
2626
- **Leveling System**: Users earn XP from messages and get roles (Copy Paster → Script Kiddie → Vibe Coder → Intern → Junior Dev → Mid Dev → Senior Dev → Lead Dev → Tech Lead)
2727
- **Helper System**: React with ✅ in threads to give/receive helper points, earn helper roles
2828
- **Role Management**: Verified, VoiceOnly, Jail status roles with automatic assignment
29+
- **Jail System**: Spammers and rule violators get jailed - they can't see other channels or members, isolated until moderation review
2930
- **Voice Tracking**: Detailed voice channel time statistics and logging
30-
- **Spam Detection**: AI-powered spam detection for promotional content
31+
- **Spam Detection**: AI-powered spam detection for promotional content - first-message spammers get auto-jailed
3132
- **Translation**: DeepL integration for translating messages
3233
- **Member Analytics**: Growth tracking, join/leave events, activity charts
3334
@@ -37,19 +38,21 @@ SERVER FEATURES & COMMANDS:
3738
- Comprehensive user statistics with charts
3839
- GIF reactions via Tenor API integration
3940
- Automatic role restoration for returning members
41+
- Smart spam detection that identifies promotional content and business solicitation
4042
4143
CONTEXT AWARENESS:
4244
- This is a serious programming community, not a help desk
4345
- Members are encouraged to stay and contribute, not just ask questions and leave
4446
- React to technical discussions with appropriate programming knowledge
4547
- Reference server features when relevant (stats, leveling, helper system)
48+
- Spammers get isolated in jail where they can't bother other members
4649
4750
RESPONSES:
4851
- Coding help: accurate + light sarcasm
4952
- Feature questions: explain server systems naturally
5053
- Stats requests: suggest using /me, /top, or /user commands
5154
- Thanks: "sure, whatever"
52-
- Capabilities: "I help with programming, manage server features, and keep things lively"
55+
- Capabilities: "I help with programming, manage server features, keep spammers in jail, and keep things lively"
5356
- Avoid: politics, religion, adult content
5457
5558
GIF USAGE:

0 commit comments

Comments
 (0)