<?php
error_reporting(E_ALL ^ E_DEPRECATED);
ini_set('memory_limit', '128M');

include __DIR__.'/vendor/autoload.php';

use \Monolog\Logger;
use \Monolog\Handler\ErrorLogHandler;
use \Monolog\Handler\RotatingFileHandler;

use Discord\Discord;
use Discord\WebSockets\Intents;
use Discord\WebSockets\Event;
use Discord\Parts\WebSockets\VoiceStateUpdate;

use VoiceState\Bot;

$botToken = getenv("BOT_TOKEN");
if (empty($botToken)) {
    die("Missing BOT_TOKEN");
}

$logger = new Logger(Bot::NAME);
$logger->pushHandler(new ErrorLogHandler(ErrorLogHandler::SAPI, Logger::INFO));
$logger->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/' . Bot::NAME . ".log", Bot::MAX_LOG_FILE_COUNT, Logger::WARNING));

$bot = new Bot(__DIR__ . '/public/states/', $logger);

$discord = new Discord([
    'token' => $botToken,
    'intents' => [
        Intents::GUILD_VOICE_STATES
    ],
    'logger' => $logger
]);

$discord->on('ready', function (Discord $discord) use ($logger) {
    $logger->info("Bot is ready!");
});

$discord->on(Event::VOICE_STATE_UPDATE, function (VoiceStateUpdate $state, Discord $discord, $oldstate) use ($bot, $logger) {
    $logger->info('Event: ' . Event::VOICE_STATE_UPDATE);
    $bot->updateVoiceState($state);
});

$discord->run();