-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
65 lines (51 loc) · 2.21 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
require('dotenv').config();
const { Client, GatewayIntentBits, Partials, ActivityType, Collection, ChannelType } = require('discord.js');
// Check for necessary environment variables
if (!process.env.DISCORD_TOKEN || !process.env.CMD_PREFIX || !process.env.SAMP_SERVER_IP) {
console.error('Missing necessary environment variables. Please check your .env file.');
process.exit(1);
}
const client = new Client({
failIfNotExists: false,
partials: [Partials.Channel],
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
const CMD_PREFIX = process.env.CMD_PREFIX.toLowerCase(); // Define CMD_PREFIX
client.commands = new Collection(); // Initialize client.commands as a Collection
const commandModules = require('./utils/commands.js');
commandModules.forEach(command => {
client.commands.set(command.name, command);
});
client.on('messageCreate', async message => {
if (message.author.bot || !message.content.toLowerCase().startsWith(CMD_PREFIX)) return;
const args = message.content.slice(CMD_PREFIX.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName);
if (!command) return;
try {
await command.execute(client, message, args);
logCommand(message, false);
} catch (error) {
logCommand(message, true);
console.error(`Error executing command ${commandName}:`, error);
message.reply('An error occurred while executing the command.');
}
});
function logCommand(message, isError) {
const logType = isError ? '[ERROR]' : '[CMD]';
const logContext = message.channel.type === ChannelType.DM ? '[DM]' : `[${message.guild.name}(${message.guild.id})]`;
console.log(`${logType} ${logContext} ${message.author.tag}(${message.author.id}) | ${message.content}`);
}
client.once('ready', () => {
console.log(`${client.user.username} is ready!`);
client.user.setActivity('SA-MP', { type: ActivityType.Playing });
});
client.on('warn', console.warn);
client.on('error', console.error);
client.login(process.env.DISCORD_TOKEN)
.catch(console.error);