-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
119 lines (97 loc) · 2.88 KB
/
bot.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
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
const {Client, GatewayIntentBits} = require("discord.js");
const fetch = require("node-fetch");
const client = new Client({intents:[
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]});
const DiscordToken = "discord-token";
const MaxChars = 8200;
let OpenAIToken;
let ChatGPTParentID;
client.on("ready", () => {
client.application.commands.create({
name: "config",
description: "Sets OpenAI token and playground category ID",
options: [
{
name: "token",
description: "OpenAI token",
type: 3
},
{
name: "id",
description: "Playground category ID",
type: 3
}]
});
console.log("ChatGPT Bot online!");
});
client.on("interactionCreate", async (interaction) => {
OpenAIToken = interaction.options.getString("token")?.trim() || OpenAIToken;
ChatGPTParentID = interaction.options.getString("id")?.trim() || ChatGPTParentID;
interaction.reply({
content: "The new settings have been saved.",
ephemeral: true
});
});
client.on("messageCreate", async (message) => {
let IsPlaygroundChat = false;
if (message.author.bot) return;
if (message.content.startsWith("!")) return;
if (!(message.mentions.has(client.user) ||
(message.channel.parent && message.channel.parent.id == ChatGPTParentID))) return;
if (message.channel.parent && message.channel.parent.id == ChatGPTParentID) {
IsPlaygroundChat = true;
}
const messages = await message.channel.messages.fetch();
const chat = [];
let counter = 0;
messages.forEach(message => {
counter += message.content.length;
if (counter > MaxChars) return;
if (message.content.length == 0) return;
if (message.content.startsWith("!")) return;
chat.push({
role: message.author.id == client.user.id ? "assistant" : "user",
content: IsPlaygroundChat ? message.content : `${message.author.username}: ${message.content}`
});
});
chat[0].content = chat[0].content.replace(`<@${client.user.id}>`, "");
if (chat.length > 1) chat.reverse();
if (!IsPlaygroundChat) chat.push({
role: "assistant",
content: "ChatGPT Bot:"
});
const interval = setInterval(() => message.channel.sendTyping(), 5000);
message.channel.sendTyping();
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${OpenAIToken}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: chat
})
});
if (response.status != 200) {
throw "Erro interno: API retornou status code diferente de 200.";
}
let r = await response.json();
r = r.choices[0].message.content;
r = r.match(/[\s\S]{1,2000}/g);
clearInterval(interval);
r.forEach(chunk => {
message.channel.send(chunk);
});
}
catch(e) {
console.log(e);
clearInterval(interval);
message.channel.send("Erro interno!");
}
});
client.login(DiscordToken);