index.js 1.52 KB
const { Client, Intents } = require('discord.js');
const Discord = require("discord.js");
const { token } = require('./config.json');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.once('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
client.category = ['example', 'stationery', 'translate', 'tts', 'ocr'];

fs.readdirSync("./Commands/").forEach(dir => {
    const Filter = fs.readdirSync(`./Commands/${dir}`).filter(f => f.endsWith(".js"));
    Filter.forEach(file => {
        const cmd = require(`./Commands/${dir}/${file}`);
        client.commands.set(cmd.config.name, cmd);
        for (let alias of cmd.config.aliases) {
            client.aliases.set(alias, cmd.config.name);
        }
    });
});

function runCommand(command, message, args, prefix) {
    if (client.commands.get(command) || client.aliases.get(command)) {
        const cmd = client.commands.get(command) || client.commands.get(client.aliases.get(command));
        if (cmd) cmd.run(client, message, args, prefix);
    }
}

client.on('messageCreate', msg => {
    const prefix = "!";
    if (!msg.content.startsWith(prefix)) return;
    let args = msg.content.slice(prefix.length).trim().split(/ +/g);
    let command = args.shift().toLowerCase();
    try {
        runCommand(command, msg, args, prefix);
    } catch (e) {
        console.error(e);
    }
});

client.login(token);