-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
134 lines (109 loc) · 3.77 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require('dotenv').config();
const {
Client,
GatewayIntentBits,
Collection,
ActivityType,
} = require('discord.js');
const { Configuration, OpenAIApi } = require('openai');
const { handleError } = require('./utils/errorHandler');
const fs = require('fs');
const { COMMAND_ALIASES } = require('./utils/constants');
const cron = require('node-cron');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});
const prefix = process.env.PREFIX || 'ai!';
client.commands = new Collection();
const commandFiles = fs
.readdirSync('./commands')
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
const configuration = new Configuration({
organization: process.env.OPENAI_ORG,
apiKey: process.env.OPENAI_KEY,
});
const openai = new OpenAIApi(configuration);
global.messages = [];
// global.systemContent =
// 'The platform is Discord, you can use markup to format your messages to fit with discord.';
client.on('messageCreate', async (message) => {
try {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
let prompt = message.content.slice(prefix.length + command.length + 1);
console.log(`${Date.now()}: ${message.author.username}: ${prompt}`);
if (COMMAND_ALIASES['commands'].includes(command)) {
return client.commands.get('commands').execute(message, client, prefix);
}
if (COMMAND_ALIASES['chat'].includes(command)) {
return await client.commands.get('chat').execute(message, openai, prompt);
}
if (COMMAND_ALIASES['image'].includes(command)) {
return await client.commands
.get('image')
.execute(message, openai, prompt);
}
if (COMMAND_ALIASES['image-variation'].includes(command)) {
return await client.commands
.get('image-variation')
.execute(message, openai);
}
if (COMMAND_ALIASES['image-edit'].includes(command)) {
return await client.commands
.get('image-edit')
.execute(message, openai, prompt);
}
if (COMMAND_ALIASES['random-image'].includes(command)) {
return await client.commands
.get('random-image')
.execute(message, prompt || '1', openai);
}
if (COMMAND_ALIASES['code'].includes(command)) {
return await client.commands.get('code').execute(message, openai, prompt);
}
if (command === 'legacy-chat') {
return await client.commands
.get('legacy-chat')
.execute(message, openai, prompt);
}
if (command === 'staircase') {
return await client.commands.get('staircase').execute(message, args);
}
} catch (error) {
console.log(error?.response?.data?.error?.message || error);
return handleError(
message,
error?.response?.data?.error?.message || JSON.stringify(error)
);
}
});
client.login(process.env.DISCORD_TOKEN);
client.on('ready', () => {
console.log("Beep boop, I'm online!");
const totalUsers = client.guilds.cache.reduce(
(acc, guild) => acc + guild.memberCount,
0
);
client.user.setActivity(`${prefix}help | ${totalUsers} users`, {
type: ActivityType.Listening,
});
});
const everyFiveMinutesCronSyntax = '*/5 * * * *';
cron.schedule(everyFiveMinutesCronSyntax, async () => {
// flush global.messages every 10 minutes to prevent max token errors
console.log('Flushing global.messages');
global.messages = [];
});