-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
73 lines (58 loc) · 2.07 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const Discord = require('discord.js');
const AI = require('./ai');
const config = require('./config');
const client = new Discord.Client();
const ai = new AI();
client.on('ready', ready);
client.on('message', onMessage);
client.login(config.DISCORD_TOKEN);
function ready() {
console.log(`Logged in as ${client.user.tag}!`);
}
let processing = false;
async function onMessage(msg) {
const message = msg.content.trim();
// Output channel id of message
if (message === "!channel") {
console.log("Text Channel: ", msg.channel.id);
return;
}
if (msg.channel.id !== config.TEXT_CHANNEL || msg.author.bot || processing)
return;
// Start a new conversation with the bot
if (message === "!reset") {
ai.conversation = [];
msg.reply("Memory erased successfully!\n\n Hello, I am an AI created by OpenAI. How can I help you today?");
return;
}
// Show current conversation
if (message === "!memory") {
let mem = "\n";
for (const m of ai.conversation) {
mem += `\n${m.author ? "You" : "AI"}: ${m.prompt}`;
}
msg.reply(mem);
return;
}
// Show help informations
if (message === "!help") {
msg.reply(
new Discord.MessageEmbed()
.setColor("#ff496a")
.setDescription("A very simple discord chat bot powered by GPT-3 from OpenAI")
.setURL('https://github.com/MindLaborDev/gpt3-discord-chatbot')
.addField("**!reset**", "Starts a new conversation")
.addField("**!memory**", "Shows current conversation")
.setAuthor("GPT-3 Chat Bot", 'https://openai.com/content/images/2019/05/openai-avatar.png', 'https://github.com/MindLaborDev/gpt3-discord-chatbot')
.setFooter("Created by MindLabor")
);
return;
}
// Chat with human
processing = true;
ai.human(message);
const answer = await ai.completion();
ai.ai(answer.choices[0].text.trim());
msg.channel.send(answer.choices[0].text.trim());
processing = false;
}