-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (44 loc) · 1.57 KB
/
index.js
File metadata and controls
58 lines (44 loc) · 1.57 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
require('dotenv').config();
const { Client, IntentsBitField } = require('discord.js');
const OpenAI = require('openai');
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});
client.on('ready', () => {
console.log('Le Bot est en ligne');
});
client.on('messageCreate', async (message) => {
if (message.author.bot || message.channel.id !== process.env.CHANNEL_ID || message.content.startsWith('!')) {
return;
}
let conversationLog = [{ role: 'system', content: 'You are a friendly chatbot.' }];
try {
await message.channel.sendTyping();
const prevMessages = await message.channel.messages.fetch({ limit: 15 });
prevMessages.reverse().forEach((msg) => {
if (msg.content.startsWith('!') || (msg.author.bot && msg.author.id !== client.user.id)) {
return;
}
const role = msg.author.id === client.user.id ? 'assistant' : 'user';
const name = msg.author.username.replace(/\s+/g, '_').replace(/[^\w\s]/gi, '');
conversationLog.push({ role, content: msg.content, name });
});
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: conversationLog
});
if (completion.choices.length > 0 && completion.choices[0].message) {
await message.reply(completion.choices[0].message);
}
} catch (error) {
console.error(`Error: ${error.message}`);
}
});
client.login(process.env.TOKEN);