-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
142 lines (119 loc) · 4.4 KB
/
bot.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
const Discord = require('discord.js');
const { DiscordAPIError } = require('discord.js');
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const getFiles = require('./lib/getFiles.js');
const clearDir = require('./lib/clearDir.js');
const loadReminders = require('./lib/loadReminders.js');
const YT_DLP_PATH = 'yt-dlp.exe';
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_VOICE_STATES
]
});
// Error handling utility function
const handleCommandError = (error, message, commandName) => {
console.error(`Error executing command ${commandName}:`, error);
let errorMessage = '❌ An unexpected error occurred. Please try again later.';
if (error instanceof DiscordAPIError) {
// Handle common Discord API errors
switch (error.code) {
case 50001:
errorMessage = '❌ I don\'t have permission to do that!';
break;
case 50006:
errorMessage = '❌ Cannot send an empty message.';
break;
case 50007:
errorMessage = '❌ I cannot send you direct messages. Please enable DMs from server members.';
break;
case 50013:
errorMessage = '❌ I don\'t have the required permissions to perform this action.';
break;
case 50034:
errorMessage = '❌ You cannot edit this message.';
break;
case 50035:
errorMessage = '❌ Invalid form body or invalid data provided.';
break;
case 10003:
case 10004:
case 10008:
errorMessage = '❌ The target channel or message was not found.';
break;
case 30005:
errorMessage = '❌ Maximum number of server roles reached.';
break;
case 40005:
errorMessage = '❌ Request entity too large. Try with a smaller file.';
break;
default:
errorMessage = `❌ Discord API Error: ${error.message}`;
}
// Log detailed API error information
console.error('Discord API Error Details:', {
code: error.code,
status: error.httpStatus,
method: error.method,
path: error.path,
requestData: error.requestData
});
} else {
// For non-Discord API errors, log the stack trace
console.error('Stack trace:', error.stack);
}
// Send error message to user
message.reply({
content: errorMessage,
allowedMentions: { repliedUser: true }
}).catch(err => {
console.error('Failed to send error message to user:', err);
});
};
dotenv.config();
const TOKEN = process.env.BOT_TOKEN;
exec(`${YT_DLP_PATH} -U`);
if (!fs.existsSync('./temp/')) {
fs.mkdirSync('./temp/');
} else {
clearDir('./temp/');
}
client.commands = new Discord.Collection();
const commandFiles = getFiles('./commands');
for (const file of commandFiles) {
const command = require(`./${file}`);
client.commands.set(command.name, command);
};
client.once('ready', () => {
console.log('Bot is online!');
// Make sure the remind command is loaded before loading reminders
if (client.commands.has('!remind')) {
loadReminders(client).catch(error => {
console.error('Failed to load reminders:', error);
});
} else {
console.error('Warning: !remind command not found, skipping reminder loading');
}
});
client.on('messageCreate', async message => {
if (!message.content.startsWith('!') || message.author.bot) return;
const args = message.content.split(' ');
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
const command = client.commands.get(commandName);
try {
// Handle both Promise-based and regular commands
await Promise.resolve(command.execute(message, args));
} catch (error) {
handleCommandError(error, message, commandName);
}
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (error) => {
console.error('Unhandled promise rejection:', error);
});
client.login(TOKEN);