-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
156 lines (131 loc) · 5.15 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
// Import necessary modules and dependencies
const { Client, GatewayIntentBits, Collection } = require('discord.js');
const { config } = require('dotenv');
const fs = require('fs');
const path = require('path');
const { log } = require('./utilities/logger');
const { handleTagCreate } = require('./interaction-handlers/tagcreate');
const { handleTagSelection } = require('./interaction-handlers/tags');
const { handleShowcaseInteraction } = require('./interaction-handlers/showcase');
const { sendWelcomeMessage } = require('./utilities/user-join');
const { sendFarewellMessage } = require('./utilities/user-leave');
// Load environment variables from .env file
config();
// Read package.json file
const packageJson = require('./package.json');
// Log package details
log(`Bot Name: ${packageJson.name}`);
log(`Version: ${packageJson.version}`);
log(`Description: ${packageJson.description}`);
log(`Author: ${packageJson.author}`);
log(`License: ${packageJson.license}`);
// Create a Discord client
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
],
});
// Initialize a Collection to store commands
bot.commands = new Collection();
// Read command files from the "commands" folder
try {
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
// Load commands
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.data.name, command);
}
} catch (error) {
log(`Error loading commands: ${error.message}`, 'error');
}
// Event: Bot is ready
bot.once('ready', async () => {
try {
// Log bot information
await log(`Logged in as ${bot.user.tag}!`);
await log(`Total servers: ${bot.guilds.cache.size}`);
// Log information about each guild the bot is in
bot.guilds.cache.forEach(guild => {
const memberCount = guild.memberCount;
log(`Guild Name: ${guild.name} | Guild ID: ${guild.id} | Member Count: ${memberCount}`);
});
// Decode bot intents
const intents = bot.options.intents;
const decodedIntents = Object.keys(GatewayIntentBits).filter(key => (intents & GatewayIntentBits[key]) !== 0);
log(`Bot Intents: ${decodedIntents.join(', ')}`);
// Get all existing commands registered by the bot
const existingCommands = await bot.application.commands.fetch();
// Delete any old commands that are no longer present
for (const existingCommand of existingCommands.values()) {
if (!bot.commands.has(existingCommand.name)) {
await existingCommand.delete();
log(`Deleted old command: ${existingCommand.name}`);
}
}
// Register slash commands
for (const command of bot.commands.values()) {
await bot.application.commands.create(command.data);
log(`Registered command: ${command.data.name}`);
}
} catch (error) {
log(`Error during bot setup: ${error.message}`, 'error');
}
});
// Event: User joins the server
bot.on('guildMemberAdd', (member) => {
// Call the sendWelcomeMessage function when a user joins
sendWelcomeMessage(member);
});
// Event: User leaves the server
bot.on('guildMemberRemove', (member) => {
// Call the sendFarewellMessage function when a user leaves
sendFarewellMessage(member);
});
// Event: Interaction is created
bot.on('interactionCreate', async (interaction) => {
try {
// Check if the interaction is not a valid interaction type
if (!interaction.isCommand() && !interaction.isStringSelectMenu() && !interaction.isButton() && !interaction.isModalSubmit()) {
return;
}
// Log the interaction type
log(`Interaction type: ${interaction.type}`);
// Handle different interaction types
if (interaction.isCommand()) {
const { commandName } = interaction;
// Log the executed command
log(`Command executed: ${commandName}`);
// Execute the command
if (!bot.commands.has(commandName)) return;
await bot.commands.get(commandName).execute(interaction);
} else if (interaction.isStringSelectMenu()) {
// Log the select menu interaction
log('Select menu interaction detected');
} else if (interaction.isButton()) {
// Log the button interaction
log('Button interaction detected');
if (interaction.customId.startsWith('viewTag_')) {
// Handle tag selection when a tag button is clicked
await handleTagSelection(interaction);
}
} else if (interaction.isModalSubmit()) {
// Check if the submitted modal is from the 'tagCreateModal'
if (interaction.customId === 'tagCreateModal') {
// Handle tag creation based on the modal submission
await handleTagCreate(interaction);
} else if (interaction.customId === 'serverInfoModal') {
// Handle showcase interaction
await handleShowcaseInteraction(interaction);
}
}
} catch (error) {
log(`Error handling interaction: ${error.message}`, 'error');
// Add appropriate error handling or reply to the user with an error message
}
});
// Log in to Discord
bot.login(process.env.BOT_TOKEN);