|
1 | 1 | import { google } from "@ai-sdk/google"; |
2 | 2 | import { generateText, ModelMessage, StepResult } from "ai"; |
3 | 3 | import console, { error } from "console"; |
4 | | -import { Message, OmitPartialGroupDMChannel } from "discord.js"; |
| 4 | +import { Message, OmitPartialGroupDMChannel, ThreadChannel } from "discord.js"; |
5 | 5 | import type { ArgsOf, Client } from "discordx"; |
6 | 6 | import { Discord, On } from "discordx"; |
7 | 7 | import { ConfigValidator } from "../../lib/config-validator"; |
@@ -60,10 +60,7 @@ export class AiChat { |
60 | 60 | const messages = channelMessages.get(message.channel.id) || []; |
61 | 61 |
|
62 | 62 | // 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); |
67 | 64 | const fullMessage = `${userMsg}${replyContext}${userContext}`; |
68 | 65 |
|
69 | 66 | try { |
@@ -124,21 +121,42 @@ export class AiChat { |
124 | 121 |
|
125 | 122 | private async getUserContext( |
126 | 123 | memberId: string, |
127 | | - guildId: string |
| 124 | + message: Message |
128 | 125 | ): Promise<string> { |
129 | 126 | try { |
130 | | - const userStats = await StatsService.getUserStatsEmbed(memberId, guildId); |
| 127 | + const userStats = await StatsService.getUserStatsEmbed( |
| 128 | + memberId, |
| 129 | + message.guildId! |
| 130 | + ); |
131 | 131 |
|
132 | 132 | if (!userStats) { |
133 | 133 | return "\n\n[User Context: New member, no stats available]"; |
134 | 134 | } |
135 | 135 |
|
136 | 136 | const { embed, roles } = userStats; |
137 | 137 |
|
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 | + |
139 | 156 | const contextData = { |
140 | 157 | embed, |
141 | 158 | roles: roles?.filter(Boolean) || [], |
| 159 | + location: channelInfo, |
142 | 160 | }; |
143 | 161 |
|
144 | 162 | return `\n\n[User Context: ${JSON.stringify(contextData, null, 2)}]`; |
@@ -186,7 +204,8 @@ export class AiChat { |
186 | 204 | // Get context for the replied user too |
187 | 205 | const repliedUserContext = await this.getUserContext( |
188 | 206 | repliedUser.id, |
189 | | - message.guildId! |
| 207 | + |
| 208 | + message |
190 | 209 | ); |
191 | 210 |
|
192 | 211 | return { |
|
0 commit comments