DZ-Musicbot/index.js

89 lines
2.8 KiB
JavaScript

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') || file.endsWith('.part')) {
fs.unlink(path.join(tmpDir, file), err => {
if (err) throw err;
});
}
}
});
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);
}
const blacklistPath = path.join(__dirname, 'blacklist.json');
if (!fs.existsSync(blacklistPath)) {
console.log('blacklist.json not found, creating one...');
fs.writeFileSync(blacklistPath, JSON.stringify({ blacklisted: [] }, null, 2));
}
let blacklistData = JSON.parse(fs.readFileSync(blacklistPath, 'utf8'));
let blacklist = blacklistData.blacklisted;
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setStatus('idle');
const statuses = require('./statuses.json');
function updateStatus() {
const randomStatus = statuses[Math.floor(Math.random() * statuses.length)];
client.user.setActivity(randomStatus);
console.log(`Activity set to: ${randomStatus}`);
}
updateStatus();
setInterval(updateStatus, 30000);
});
client.on('messageCreate', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
if (blacklist.includes(message.author.id)) {
return message.reply("You are blacklisted from using this bot.");
}
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) ||
Array.from(client.commands.values()).find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
try {
await command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('There was an error trying to execute that command!');
}
});
function updateBlacklist(newBlacklist) {
blacklist = newBlacklist;
console.log("Blacklist updated:", blacklist);
}
module.exports = { updateBlacklist };
client.login(token);