DZ-Musicbot/index.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-08-17 11:11:10 -04:00
const { Client, GatewayIntentBits } = require('discord.js');
const fs = require('fs');
const path = require('path');
const { prefix, token } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.commands = new Map();
const tmpDir = path.join(__dirname, 'utils', 'tmp');
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, { recursive: true });
}
fs.readdir(tmpDir, (err, files) => {
if (err) throw err;
for (const file of files) {
if (file.endsWith('.mp3')) {
fs.unlink(path.join(tmpDir, file), err => {
if (err) throw err;
});
}
}
});
2024-08-17 15:11:10 -04:00
fs.readdir(tmpDir, (err, files) => {
if (err) throw err;
for (const file of files) {
if (file.endsWith('.part')) {
fs.unlink(path.join(tmpDir, file), err => {
if (err) throw err;
});
}
}
});
2024-08-17 11:11:10 -04:00
const commandFiles = fs.readdirSync(path.join(__dirname, 'commands')).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
try {
await client.commands.get(commandName).execute(message, args);
} catch (error) {
console.error(error);
message.reply('There was an error trying to execute that command!');
}
});
client.login(token);