-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (158 loc) · 4.88 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Initialization */
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const { Client, Partials, GatewayIntentBits, Collection, Routes } = require("discord.js");
const Utils = require("./utils");
const { REST } = require("@discordjs/rest");
const utils = new Utils();
const settings = utils.getSetting();
const dbPath = path.join(__dirname, "db.json");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
],
partials: [Partials.Channel],
});
client.commands = new Collection();
/* Command Handler */
function loadCommands() {
client.commands.clear();
const commandFiles = fs.readdirSync(path.join(__dirname, "commands")).filter(
(file) => file.endsWith(".js"),
);
const commands = [];
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
if (command.data && typeof command.data.toJSON === "function") {
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
if (settings.logCommandsInit) {
utils.log(`Loaded command: ${command.data.name}`, "info");
}
} else {
utils.error(
`Command ${file} is missing 'data' property or 'toJSON' method.`,
);
}
}
return commands;
}
const commands = loadCommands();
client.once("ready", async () => {
if (!client.user) {
utils.error("Client user is null.");
return;
}
try {
if (settings.logCommandsInit) {
utils.log("Started refreshing application (/) commands.", "info");
}
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
await rest.put(Routes.applicationCommands(client.user.id), {
body: commands,
});
if (settings.logCommandsInit) {
utils.log("Successfully reloaded application (/) commands.", "info");
}
} catch (error) {
utils.error(`Error reloading commands: ${error}`);
}
if (settings.initMessage.enabled) {
utils.log(settings.initMessage.message, "info");
}
setInterval(checkBans, settings.banCheckTime * 1000);
});
/* Ban Check Function */
async function checkBans() {
if (!fs.existsSync(dbPath)) return;
let db;
try {
db = JSON.parse(fs.readFileSync(dbPath, "utf8"));
} catch (error) {
utils.error(`Error reading db file: ${error}`);
return;
}
for (const guildId of Object.keys(db)) {
const guild = client.guilds.cache.get(guildId);
if (!guild) continue;
for (const userId of Object.keys(db[guildId])) {
const banInfo = db[guildId][userId];
if (banInfo.unbanTimestamp && Date.now() > banInfo.unbanTimestamp) {
try {
await guild.members.unban(userId);
utils.log(`Unbanned user ${userId} from guild ${guildId}`, "success");
delete db[guildId][userId];
} catch (error) {
utils.error(
`Failed to unban user ${userId} from guild ${guildId}: ${error.message}`,
);
}
}
}
}
try {
fs.writeFileSync(dbPath, JSON.stringify(db, null, 2));
} catch (error) {
utils.error(`Error writing to db file: ${error}`);
}
}
/* Event Listener */
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) {
await utils.tempReply(interaction, {
content: settings.invalidCommand.message,
ephemeral: false,
time: settings.invalidCommand.timeout,
showTime: true,
});
return;
}
try {
await command.execute(interaction, utils, client);
} catch (error) {
utils.error(`Error executing command ${interaction.commandName}: ${error}`);
if (!interaction.replied && !interaction.deferred) {
try {
await interaction.reply({
content: "There was an error executing this command!",
ephemeral: false,
});
} catch (replyError) {
utils.error(`Error sending error reply: ${replyError}`);
}
}
}
});
/* AI DM Integration */
client.on("messageCreate", async (message) => {
if (!message.author.bot && !message.guild) {
try {
// typing
await message.channel.sendTyping();
const response = await utils.generateXAIResponse(message.content);
await message.channel.send(response);
} catch (error) {
utils.error(`Error processing message: ${error}`);
await message.channel.send("There was an error processing your message.");
}
}
});
/* Log Command Initialization */
if (settings.logCommandsInit) {
utils.log("Commands have been initialized.", "info");
}
/* Login or Exit */
if (process.argv.includes("noToken")) {
utils.log("noToken flag detected. Exiting...", "info");
setTimeout(() => process.exit(0), 1000);
} else {
client.login(process.env.TOKEN).catch((error) => {
utils.error(`Failed to login: ${error}`);
process.exit(1);
});
}