/*
Bot Ingénieurs pour demain
Developped by Valentin SERVIERES, if you have any question, you can contact me by Discord MagicTINTIN#4389
- Discord.js version v12 (depreciated)
*/
console.log("Starting...");
const { appID, publicKey, token, prefix } = require('./config/credentials.json');
const fs = require('fs');
const path = require('path');
const Discord = require('discord.js');
const { Client, Intents, MessageEmbed } = require('discord.js');
// Only mandatory for Discord.js v13
const client = new Client({
    intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_BANS, Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS, Intents.FLAGS.GUILD_INTEGRATIONS, Intents.FLAGS.GUILD_WEBHOOKS, Intents.FLAGS.GUILD_INVITES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_MESSAGE_TYPING, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS, Intents.FLAGS.DIRECT_MESSAGE_TYPING],
});
exports.client = client;


// --- INITIALIZING BOT ---
config = JSON.parse(fs.readFileSync(path.resolve(`./config/botinfo.json`)));
console.log("config found\ngetting log channel id");
const logch = config.logChannelID
exports.logch = logch;

// Import functions
const random = require("./functions/random.js");
const logger = require("./functions/logger.js");
const msgcheck = require("./functions/messageCheck.js");
const rolereact = require("./functions/rolereaction.js");

// Import commands
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./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', () => {
    // cache message that could be used for reactions
    rolereact.cache();
    console.log('Ready to act !')
    client.user.setActivity(config.activity, { type: 'PLAYING' });
    client.channels.cache.get(logch).send("**❕ BOT ONLINE** - ready to act");
});
// --- END INITIALIZATION ---


client.on('message', async message => {
    if (message.author.bot) return
    if (!message.channel.guild) {
        return logger.all(message.author.tag + " a envoyé un mp : " + message.content)
    }
    // For more details see /functions/messageCheck.js
    msgcheck.general(message);
    // Command trigger
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLocaleLowerCase();

    if (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args);
        logger.all(`🔨 ${message.author.tag} - \`${message}\``)
    } catch (error) {
        console.error(error);
    }


});


client.on('guildMemberAdd', async member => {
    try {
        logger.all(`🆕 ${member.user.tag} vient d'arriver sur le serveur`);
        // update user file
        msgcheck.memberUpdate(member, "join");
    } catch (err) { console.error(err); }
});
client.on('guildMemberRemove', async member => {
    try {
        logger.all(`❌ ${member.user.tag} vient de quitter le serveur`);
        // update user file
        msgcheck.memberUpdate(member, "left");
    } catch (error) {

    }
});


client.on('messageReactionAdd', (reaction, user) => {
    rolereact.give(reaction, user);
});
client.on('messageReactionRemove', (reaction, user) => {
    rolereact.remove(reaction, user);
});


client.login(token);