-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
73 lines (61 loc) · 1.78 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
import { createRequire } from "module";
import { ChatGPTAPI } from "chatgpt";
const require = createRequire(import.meta.url);
const config = require("./config.json");
const { Client, GatewayIntentBits } = require("discord.js");
var processing = false;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
],
});
const api = new ChatGPTAPI({
sessionToken: config.OAISession,
});
const conversation = api.getConversation();
client.on("messageCreate", async (message) => {
if (
message.author.bot ||
message.content.includes("@here") ||
message.content.includes("@everyone") ||
(config.channelsWhitelist.length > 0 &&
!config.channelsWhitelist.includes(message.channel.id))
)
return false;
if (message.mentions.has(client.user.id)) {
if (
config.usersWhitelist.length > 0 &&
!config.usersWhitelist.includes(message.author.id)
) {
await message.reply(config.accessMessage);
return false;
}
if (!processing) {
processing = true;
const status = await message.reply(config.processingMessage);
const question = capitalizeFirstLetter(
message.content.replace("<@" + config.botID + ">", "").trim()
);
try {
await api.ensureAuth();
const answer = await conversation.sendMessage(question, {
timeoutMs: 5 * 60 * 1000,
});
await message.reply(answer);
status.delete();
} catch (e) {
status.edit(e);
} finally {
processing = false;
}
} else {
message.reply(config.waitingMessage);
}
}
});
function capitalizeFirstLetter(string) {
return string[0].toUpperCase() + string.slice(1);
}
client.login(config.discordToken);