-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (123 loc) · 4.38 KB
/
index.js
File metadata and controls
145 lines (123 loc) · 4.38 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
/**
* Discord Amazon Rekognition Bot
*
* A powerful Discord bot that integrates AWS Rekognition for advanced image analysis,
* including object detection, text extraction, face analysis, celebrity recognition,
* content moderation, and face comparison between images.
*
* Version: 1.0.0
* Author: gl0bal01
*/
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits, ActivityType } = require('discord.js');
require('dotenv').config();
// Create Discord client with necessary intents
const client = new Client({
intents: [
GatewayIntentBits.Guilds
]
});
// Create commands collection
client.commands = new Collection();
// Load commands from the commands directory
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
console.log('🔄 Loading commands...');
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
console.log(`✅ Loaded command: ${command.data.name}`);
} else {
console.log(`⚠️ Command at ${filePath} is missing required "data" or "execute" property.`);
}
}
// Create temp directory for file operations
const tempDir = path.join(__dirname, 'temp');
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
console.log('📁 Created temporary files directory');
} else {
// Clean up orphaned temp files from previous runs
const orphanedFiles = fs.readdirSync(tempDir);
for (const file of orphanedFiles) {
try { fs.unlinkSync(path.join(tempDir, file)); } catch {}
}
if (orphanedFiles.length > 0) {
console.log(`🧹 Cleaned up ${orphanedFiles.length} orphaned temp file(s)`);
}
}
// Bot ready event
client.once(Events.ClientReady, readyClient => {
console.log('🎯 Discord Amazon Rekognition Bot is ready!');
console.log(`📊 Logged in as: ${readyClient.user.tag}`);
console.log(`🌐 Serving ${readyClient.guilds.cache.size} servers`);
console.log(`👥 Connected to ${readyClient.users.cache.size} users`);
// Set bot activity status
client.user.setActivity('images with AWS Rekognition | /rekognition', {
type: ActivityType.Watching
});
});
// Handle slash command interactions
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`❌ No command matching ${interaction.commandName} found.`);
return;
}
try {
console.log(`📝 Executing command: ${interaction.commandName} by ${interaction.user.tag}`);
await command.execute(interaction);
} catch (error) {
console.error(`❌ Error executing command ${interaction.commandName}:`, error.message || error);
const errorResponse = {
content: '⚠️ There was an error while executing this command! Please try again or contact support.',
ephemeral: true
};
try {
if (interaction.replied || interaction.deferred) {
await interaction.followUp(errorResponse);
} else {
await interaction.reply(errorResponse);
}
} catch (replyError) {
console.error('❌ Failed to send error message:', replyError);
}
}
});
// Handle bot errors
client.on('error', error => {
console.error('❌ Discord client error:', error);
});
client.on('warn', warning => {
console.warn('⚠️ Discord client warning:', warning);
});
// Handle process errors
process.on('unhandledRejection', error => {
console.error('❌ Unhandled promise rejection:', error);
});
process.on('uncaughtException', error => {
console.error('❌ Uncaught exception:', error);
client.destroy();
process.exit(1);
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('🛑 Received SIGINT, shutting down gracefully...');
client.destroy();
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('🛑 Received SIGTERM, shutting down gracefully...');
client.destroy();
process.exit(0);
});
// Login to Discord
console.log('🚀 Starting Discord Amazon Rekognition Bot...');
client.login(process.env.DISCORD_TOKEN).catch(error => {
console.error('❌ Failed to login to Discord:', error);
process.exit(1);
});