Remove overly broad AI conversation window that answered unrelated chat

This commit is contained in:
justin committed 2026-09-06 16:51:20 -07:00
1 parent 37c55abcbd
commit 86a81e5a64
1 file changed
+20 -25
+20 -25
View File
@@ -96,8 +96,6 @@ const aiCooldowns = new Map();
const aiHistories = new Map(); const aiHistories = new Map();
const AI_HISTORY_EXCHANGES = 3; const AI_HISTORY_EXCHANGES = 3;
const AI_HISTORY_TTL_MS = 30 * 60 * 1000; const AI_HISTORY_TTL_MS = 30 * 60 * 1000;
const activeAiConversations = new Map();
const AI_ACTIVE_CONVO_MS = 2 * 60 * 1000;
function canUseAI(userId) { function canUseAI(userId) {
const now = Date.now(); const now = Date.now();
@@ -107,20 +105,6 @@ function canUseAI(userId) {
return true; return true;
} }
function isActiveAiConversation(userId, channelId) {
const entry = activeAiConversations.get(userId);
if (!entry) return false;
if (Date.now() > entry.expiresAt) {
activeAiConversations.delete(userId);
return false;
}
return entry.channelId === channelId;
}
function markActiveAiConversation(userId, channelId) {
activeAiConversations.set(userId, { channelId: channelId, expiresAt: Date.now() + AI_ACTIVE_CONVO_MS });
}
async function isReplyToBot(message) { async function isReplyToBot(message) {
if (!message.reference || !message.reference.messageId) return false; if (!message.reference || !message.reference.messageId) return false;
try { try {
@@ -248,7 +232,11 @@ function saveSteamLinks(links) {
} }
function loadScores() { function loadScores() {
try {
return JSON.parse(fs.readFileSync(SCORES_PATH, "utf8")); return JSON.parse(fs.readFileSync(SCORES_PATH, "utf8"));
} catch (err) {
return {};
}
} }
function saveScores(scores) { function saveScores(scores) {
@@ -1370,7 +1358,7 @@ async function handleShops(message) {
} }
} }
const TRIVIA_TIME_LIMIT_MS = 30 * 1000; const TRIVIA_TIME_LIMIT_MS = 60 * 1000;
const TRIVIA_LETTERS = ["A", "B", "C", "D"]; const TRIVIA_LETTERS = ["A", "B", "C", "D"];
const activeTrivia = new Map(); const activeTrivia = new Map();
@@ -1419,12 +1407,12 @@ async function handleTrivia(message) {
} }
const picked = pickTriviaQuestion(); const picked = pickTriviaQuestion();
const entry = { question: picked, timeout: null }; const entry = { question: picked, timeout: null, answered: new Set() };
activeTrivia.set(channelId, entry); activeTrivia.set(channelId, entry);
const lines = ["PC gaming trivia! " + picked.question]; const lines = ["PC gaming trivia! " + picked.question];
picked.choices.forEach((choice, i) => lines.push(TRIVIA_LETTERS[i] + ") " + choice)); picked.choices.forEach((choice, i) => lines.push(TRIVIA_LETTERS[i] + ") " + choice));
lines.push("You have 30 seconds, first correct answer wins!"); lines.push("You have 60 seconds, first correct answer wins!");
try { try {
await message.reply(lines.join("\n")); await message.reply(lines.join("\n"));
@@ -1455,6 +1443,10 @@ async function handleTriviaAnswer(message, content) {
const cleaned = content.trim().toUpperCase().replace(/[).:]/g, ""); const cleaned = content.trim().toUpperCase().replace(/[).:]/g, "");
const guessIndex = TRIVIA_LETTERS.indexOf(cleaned); const guessIndex = TRIVIA_LETTERS.indexOf(cleaned);
if (guessIndex === -1) return false; if (guessIndex === -1) return false;
if (entry.answered.has(message.author.id)) return false;
entry.answered.add(message.author.id);
if (guessIndex !== entry.question.answer) return false; if (guessIndex !== entry.question.answer) return false;
clearTimeout(entry.timeout); clearTimeout(entry.timeout);
@@ -1723,8 +1715,14 @@ client.on("messageCreate", async (message) => {
const content = message.content.trim(); const content = message.content.trim();
const lower = content.toLowerCase(); const lower = content.toLowerCase();
if (!content.startsWith("!") && (await handleTriviaAnswer(message, content))) { if (!content.startsWith("!")) {
return; let triviaHandled = false;
try {
triviaHandled = await handleTriviaAnswer(message, content);
} catch (err) {
console.error("Trivia answer check failed:", err.message);
}
if (triviaHandled) return;
} }
if (lower.startsWith("!watch ")) { if (lower.startsWith("!watch ")) {
@@ -1772,9 +1770,7 @@ client.on("messageCreate", async (message) => {
} else if (lower === "!help") { } else if (lower === "!help") {
await handleHelp(message); await handleHelp(message);
} else if (!content.startsWith("!") && AI_ENABLED && } else if (!content.startsWith("!") && AI_ENABLED &&
(TRIGGER_MENTION.test(content) || (TRIGGER_MENTION.test(content) || await isReplyToBot(message))) {
isActiveAiConversation(message.author.id, message.channelId) ||
await isReplyToBot(message))) {
if (canUseAI(message.author.id)) { if (canUseAI(message.author.id)) {
try { try {
const history = getAiHistory(message.author.id); const history = getAiHistory(message.author.id);
@@ -1786,7 +1782,6 @@ client.on("messageCreate", async (message) => {
if (aiReply) { if (aiReply) {
await message.reply(aiReply); await message.reply(aiReply);
rememberAiExchange(message.author.id, content, aiReply); rememberAiExchange(message.author.id, content, aiReply);
markActiveAiConversation(message.author.id, message.channelId);
} }
} catch (err) { } catch (err) {
console.error(PERSONALITY.name + " AI reply failed:", err.message); console.error(PERSONALITY.name + " AI reply failed:", err.message);