forked from IvanCampos/discord-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (46 loc) · 1.57 KB
/
index.js
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
import dotenv from "dotenv";
import { Client, GatewayIntentBits } from "discord.js";
import { Configuration, OpenAIApi } from "openai";
dotenv.config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY,
})
);
async function getConversation (message) {
const formatted = {
role: message.author.bot ? 'assistant' : 'user',
content: message.content.replace(`<@${process.env.DISCORD_CLIENT_ID}> `, ''),
}
if (!(message.reference && message.reference.messageId)) {
return [formatted]
}
const repliedMessage = await message.channel.messages.fetch(message.reference.messageId);
const restOfTheConversation = await getConversation(repliedMessage)
return [...restOfTheConversation, formatted]
}
client.on("messageCreate", async function (message) {
if (message.author.bot) return;
if (!message.mentions.users.has(process.env.DISCORD_CLIENT_ID)) return;
const conversation = await getConversation(message)
try {
const response = await openai.createChatCompletion({
model: "gpt-4",
messages: [
{role: "system", content: "You are a helpful assistant who responds succinctly"},
...conversation,
],
});
const content = response.data.choices[0].message;
return message.reply(content);
} catch (err) {
return message.reply(`As an AI robot, I errored out.\n${err}`);
}
});
client.login(process.env.BOT_TOKEN);