-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (58 loc) · 2.11 KB
/
index.js
File metadata and controls
69 lines (58 loc) · 2.11 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
require('dotenv/config');
const { Client, GatewayIntentBits } = require('discord.js');
const { Configuration, OpenAIApi } = require('openai');
const client = new Client({
intents: [
// What its allowed to see
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
]
})
// Message when bot is online
client.on('ready', () => {
console.log("Client is online!")
});
// Get the API Key
const configuration = new Configuration({
apiKey: process.env.API_KEY_OPEN_AI,
})
const openai = new OpenAIApi(configuration);
// Get message data in terminal
client.on('messageCreate', async (message) => {
// Dont reply to itself
if (message.author.bot) return;
// If chatting in wrong channel, dont send message
if (message.channel.id !== process.env.CHANNEL_ID) return;
// Ignore messages started with such prefix (.env)
if (message.content.startsWith(process.env.MESSAGE_EXCEPTION)) return;
// What the Bot is based on
let conversationLog = [{ role: 'system', content: process.env.PROMPT}]
// Bot Typing Effect
await message.channel.sendTyping();
// Limit of previous messages it can remember
let prevMessages = await message.channel.messages.fetch({ limit: process.env.MESSAGE_MEMORY});
prevMessages.reverse();
// Conditions
prevMessages.forEach((msg) => {
if (message.content.startsWith(process.env.MESSAGE_EXCEPTION)) return;
// Only answer people, not other bots
if (message.author.id !== client.user.id && message.author.bot) return;
// Only converse with such person instead of all
if (message.author.id !== message.author.id) return;
conversationLog.push({
role: 'user',
content: message.content,
});
});
// Model Stuff
const result = await openai.createChatCompletion({
// Model Type
model: 'gpt-3.5-turbo',
messages: conversationLog,
});
//[0] is the first choice to pick
message.reply(result.data.choices[0].message);
});
// Get data from .env
client.login(process.env.BOT_TOKEN)