-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
161 lines (146 loc) Β· 4.61 KB
/
Copy pathbot.js
File metadata and controls
161 lines (146 loc) Β· 4.61 KB
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
const fs = require('fs');
const path = require('path');
const { Client, Collection, GatewayIntentBits, Partials, REST, Routes } = require('discord.js');
const { loadModules } = require('./utils/loader');
const readline = require('readline');
const configPath = path.resolve(__dirname, 'config.json');
if (!fs.existsSync(configPath)) {
const defaultConfig = { token: '', clientId: '', };
fs.writeFileSync(configPath, JSON.stringify(defaultConfig, null, 2));
console.log('config.json not found. Created default config.json. Please fill it in and restart the bot.');
console.log('Process exiting...');
process.exit(0);
}
const config = require(configPath);
const bot = new Client({
intents: [
GatewayIntentBits.GuildMembers,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildPresences,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
bot.commands = new Collection();
bot.modules = new Collection();
bot.roleMenuReactions = new Map();
// Store cleanup functions for each module
const moduleCleanups = new Map();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', input => {
if (input.trim().toLowerCase() === 'stop') {
console.log('Stopping bot...');
for (const cleanup of moduleCleanups.values()) {
try {
cleanup();
} catch (err) {
console.error(`Error in cleanup: ${err.message}`);
}
}
process.exit(0);
}
});
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('Received SIGINT: Cleaning up...');
for (const cleanup of moduleCleanups.values()) {
try {
cleanup();
} catch (err) {
console.error(`Error in cleanup: ${err.message}`);
}
}
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('Received SIGTERM: Cleaning up...');
for (const cleanup of moduleCleanups.values()) {
try {
cleanup();
} catch (err) {
console.error(`Error in cleanup: ${err.message}`);
}
}
process.exit(0);
});
(async () => {
await loadModules(bot, moduleCleanups);
const registered = new Set();
const commands = [];
for (const cmd of bot.commands.values()) {
if (cmd.data && typeof cmd.data.toJSON === 'function' && !registered.has(cmd.data.name)) {
commands.push(cmd.data.toJSON());
registered.add(cmd.data.name);
}
}
const rest = new REST({ version: '10' }).setToken(config.token);
try {
console.log('π Registering slash commands...');
await rest.put(
Routes.applicationCommands(config.clientId),
{ body: commands }
);
console.log('β
Slash commands registered.');
} catch (error) {
console.error('β Error registering slash commands:', error);
process.exit(1);
}
try {
await bot.login(config.token);
console.log(`β
Logged in as ${bot.user.tag}`);
} catch (loginError) {
console.error('β Login failed:', loginError);
process.exit(1);
}
})();
// Unified interaction handler
bot.on('interactionCreate', async interaction => {
try {
if (interaction.isCommand()) {
const command = bot.commands.get(interaction.commandName);
if (command && typeof command.execute === 'function') {
await command.execute(interaction, bot);
}
} else if (interaction.isStringSelectMenu() || interaction.isButton()) {
for (const cmd of bot.commands.values()) {
if (typeof cmd.handle === 'function') {
await cmd.handle(interaction, bot);
}
}
}
} catch (err) {
console.error('β Interaction Error:', err);
if (!interaction.replied && !interaction.deferred) {
await interaction.reply({ content: 'β An error occurred.', ephemeral: true });
}
}
});
// Legacy message-based dash commands
bot.on('messageCreate', async message => {
if (message.author.bot || !message.content.startsWith('-')) return;
const args = message.content.slice(1).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = bot.commands.get(commandName);
if (command && typeof command.execute === 'function') {
try {
await command.execute({
user: message.author,
guild: message.guild,
channel: message.channel,
reply: msg => message.reply(msg),
deferReply: () => Promise.resolve(),
editReply: msg => message.channel.send(msg),
member: message.member,
content: message.content
}, bot);
} catch (error) {
console.error(error);
message.reply('β Error executing command.');
}
}
});