-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscordbot.js
More file actions
141 lines (115 loc) · 4.45 KB
/
discordbot.js
File metadata and controls
141 lines (115 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { OpenAI } = require('openai');
let discordClient = null;
let discordMessageHistory = [];
let discordSettings = null;
// Default Discord system prompt
const DISCORD_CORE_PROMPT = `You are a Discord chatbot. Try to keep messages shorter than 2000 characters.`;
// Timeout for context memory (we could clear periodically or on max message length too)
const MAX_HISTORY = 15;
function initializeDiscord(settings, openaiInstance) {
discordSettings = settings;
if (!discordSettings.enableDiscordBot) {
console.log('Discord bot disabled in settings.');
return;
}
// Discord client
discordClient = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
],
partials: [Partials.Channel]
});
// Prepare the system prompt for Discord
let discordSystemPrompt = DISCORD_CORE_PROMPT;
if (discordSettings.discordSystemPrompt) {
discordSystemPrompt += `\n${discordSettings.discordSystemPrompt}`;
}
// Create a helper to get chat response with Discord context
async function getDiscordChatResponse(userMessage) {
if (!openaiInstance) {
console.error('OpenAI instance missing for Discord.');
return 'Sorry, I am currently unable to respond.';
}
const prompt = discordSystemPrompt;
const context = discordMessageHistory.join('\n');
try {
const response = await openaiInstance.chat.completions.create({
model: discordSettings.openaiModelName || 'gpt-4o-mini',
messages: [
{ role: 'system', content: prompt },
{ role: 'user', content: `Context:\n${context}\n\nUser: ${userMessage}\nBot:` }
],
max_tokens: 300,
});
return response.choices[0].message.content.trim();
} catch (error) {
console.error('Error calling OpenAI API for Discord:', error);
return 'Sorry, I encountered an error while generating a response.';
}
}
discordClient.on('clientReady', () => {
console.log(`Discord bot logged in as ${discordClient.user.tag}`);
});
discordClient.on('messageCreate', async (message) => {
// Ignore messages from the bot itself or from DMs
if (message.author.bot) return;
if (!message.guild) return; // ignore DMs for now
// Check if the bot was mentioned
if (!message.mentions.has(discordClient.user)) return; // only respond when mentioned
// If restricted channels are configured, enforce it
/**
* discordSettings.discordChannels is expected to be an array of channel ids or names
* If empty or missing, respond anywhere bot is mentioned
*/
if (Array.isArray(discordSettings.discordChannels) && discordSettings.discordChannels.length > 0) {
const channelId = message.channel.id;
// Check if the current channel matches any allowed channel
if (!discordSettings.discordChannels.includes(channelId) && !discordSettings.discordChannels.includes(message.channel.name)) {
// Not allowed channel
return;
}
}
// Clean message content by removing mention
const botMentionRegex = new RegExp(`<@!?${discordClient.user.id}>`, 'g');
let userMessage = message.content.replace(botMentionRegex, '').trim();
if (!userMessage) return; // no message after mention
// Add user message to discord context
discordMessageHistory.push(`${message.author.username}: ${userMessage}`);
if (discordMessageHistory.length > MAX_HISTORY) {
discordMessageHistory.shift();
}
// Get bot response
let reply = await getDiscordChatResponse(userMessage);
// Clean up any tags or unwanted responses if necessary (optional)
// Add bot response to history
discordMessageHistory.push(`Bot: ${reply}`);
if (discordMessageHistory.length > MAX_HISTORY) {
discordMessageHistory.shift();
}
// Reply to user in same channel
message.reply(reply).catch(console.error);
});
}
function loginDiscord(token) {
if (!discordClient) {
console.error('Discord client not initialized. Call initializeDiscord first.');
return;
}
discordClient.login(token).catch((err) => console.error('Discord login error:', err));
}
function disconnectDiscord() {
if (discordClient) {
discordClient.destroy();
discordClient = null;
discordMessageHistory = [];
console.log('Discord bot disconnected');
}
}
module.exports = {
initializeDiscord,
loginDiscord,
disconnectDiscord
};