-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (75 loc) · 3.13 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
require('dotenv').config(); // Load environment variables from .env file
const {Client, GatewayIntentBits, Partials, Collection} = require('discord.js');
const fs = require('fs');
const path = require('path');
const {REST} = require('@discordjs/rest');
const {Routes} = require('discord-api-types/v9');
// Check if required environment variables are set
if (!process.env.DISCORD_TOKEN || !process.env.CLIENT_ID || !process.env.GUILD_ID) {
console.error('Missing required environment variables. Please ensure DISCORD_TOKEN, CLIENT_ID, and GUILD_ID are set.');
process.exit(1);
}
// Create a new Discord client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember,
Partials.Reaction
]
});
client.commands = new Collection(); // Collection to store commands
client.nsfwTimers = new Map(); // Map to store NSFW timers
client.catTimers = new Map(); // Map to store cat timers
const commandsPath = path.join(__dirname, 'commands'); // Path to commands directory
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); // Get command files from directory
const commands = []; // Array to store command data
// Load commands
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
// When the bot is ready
client.once('ready', () => {
console.log(`Bot is ready!`);
client.user.setStatus('dnd');
});
// Interaction event handler
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return; // Ignore if not a command interaction
const command = client.commands.get(interaction.commandName);
if (!command) return; // If command not found, ignore
try {
await command.execute(interaction, client); // Execute the command
} catch (error) {
console.error(`Error executing command ${interaction.commandName}:`, error);
await interaction.reply({content: 'There was an error while executing this command!', ephemeral: true});
}
});
client.login(process.env.DISCORD_TOKEN); // Login the client with the bot token
const rest = new REST({version: '9'}).setToken(process.env.DISCORD_TOKEN); // Create a REST client
// Refresh application (/) commands
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
{body: commands},
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error('Error while refreshing application (/) commands:', error);
}
})();