-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.handler.js
54 lines (41 loc) · 1.43 KB
/
command.handler.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
import checkGuildId from "./config/allowed-servers.js";
import openai from "./config/open-ai.js";
const COMMAND = "!prompt";
export default async function promptHandler(message) {
try {
if (message.author.bot) return;
if (!message.content.startsWith(COMMAND)) return;
const guildId = message.guild?.id;
// check if message is from an authorised server
if (!checkGuildId({ guildId })) {
console.log({ "unauthorised guild": guildId });
message.reply("Unauthorised server!");
return;
}
let conversationContext = [];
let chatHistory = await message.channel.messages.fetch({ limit: 10 });
chatHistory.forEach((msg) => {
conversationContext.push({
role: !msg.author.bot ? "user" : "assistant",
content: msg.content,
});
});
conversationContext.reverse();
let userInput = message.content.substring(COMMAND.length);
console.log({ user: userInput });
conversationContext.push({
role: "user",
content: userInput,
});
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: conversationContext,
max_tokens: parseInt(process.env.MAX_RESPONSE_LENGTH),
});
const completionText = completion.data.choices[0].message.content;
message.reply(completionText);
} catch (error) {
console.error(error);
message.reply("Something went wrong! Please try after sometime.");
}
}