<?php
use Discord\Discord;
use Discord\Parts\Channel\Message;
//use Discord\Parts\User\User;
use React\EventLoop\Factory;
//for remote script
use Psr\Http\Message\ResponseInterface;
use Discord\WebSockets\Intents;
use Discord\WebSockets\Event;
use React\Http\Browser;

require __DIR__ . "/vendor/autoload.php";
require_once __DIR__ . "/eq2emubot-functions.php";
require_once __DIR__ . "/options.php";


$loop = Factory::create();
$token = BOTTOKEN; // SEE OPTIONS.PHP FOR DEFINE
$dns = BOTDNS;     // SEE OPTIONS.PHP FOR DEFINE
//for http stuff
//$browser = new Browser($loop);

$discord = new Discord([
    "token" => $token,
    "loop" => $loop,
    "loadAllMembers" => true,
    "dnsConfig" => $dns,
    "intents" =>
        Intents::getDefaultIntents() |
        Intents::GUILD_MEMBERS |
        Intents::MESSAGE_CONTENT,
]);
//for browser stuff add it becomes
//$discord->on('message', function (Message $message, Discord $discord) use ($browser) {
$discord->on("message", function (Message $message, Discord $discord) {
    //setup global vars
    $idforadmin = ADMINID;
    $adminuser = ADMINNAME;
    //setup admin
    $isadmin = isadmin($message->author->username, $message->user_id);

    //ignore stuff from ourself, so we dont loop spam.
    if ($message->author->bot) {
        return;
    }

    //////////////////////////// BOT COMMANDS START
    $command = "!help";
    $command2 = "!8ball";
    $command3 = "!links";
    $command4 = "!team";
    $command5 = "!online";
    //////////////////////////// BOT COMMANDS END

    /////////////////////////// COMMAND HANDLERS START

    //help
    if (preg_match("/{$command}/i", $message->content)) {
        $msg =
            "```ini\n[Devn00bs Zeklabs Bot 1.0 Public]\n[!help		                 -   You are here.]\n[!8ball <question>     	    -   Shake the magic 8ball.]\n[!links			       	 -   Display links related to the project]\n[!team			       	  -   Lists the development team members]```";
        $message->reply($msg);
        if ($isadmin == 1) {
            //used to show admin commands. below is 100% example taken from Zeklabs bot. You should customize as you add functions
            $msg2 =
                "```css\n[Devbots ADMIN Commands]\n[!movechar <charname> <zoneshortname>			-   Moves character to selected zone.]\n[!btlog <world/login>							-   View last backtrace of type listed.]\n[!asanlog <world/login>						  -   View last asan log of type listed.]\n[!systemstats		  						  -   View server stats.]\n[!online     		  						  -   View Online Users]```";
            $message->reply($msg2);
        }
        return;
    }

    if (preg_match("/{$command2}/i", $message->content)) {
        $cont = $message["content"];
        $question = str_replace("!8ball", "", $cont);
        $pieces = explode(" ", $cont);
        $reset = $pieces[1];

        if (!$reset) {
            $msg = "\nPlease enter a question!";
            $message->reply($msg);
            return;
        }
        $answer = ball($question);
        $msg = "\n " . $answer . " ";
        $message->reply($msg);
        return;
    }

    if (preg_match("/{$command3}/i", $message->content)) {
        $msg =
            "\nHere are some useful links relating to our project:\nZeklabs Website: https://www.zeklabs.com/\nSource (git): https://git.eq2emu.com/devn00b/EQ2EMu\nPlay on Our Servers: https://eq2emu.com/playontest.html\nRun Your Own Server: https://eq2emu.com/installeq2emu.html\nWiki: https://wiki.eq2emu.com/\nDiscord-PHP (Documentation): https://discord-php.github.io/DiscordPHP/";
        $message->reply($msg);
        return;
    }

    if (preg_match("/{$command4}/i", $message->content)) {
        $msg =
            "```\nWe currently split development into two teams, Code and Content. Team Members Include:\nCode Team:\nDevn00b (Team Lead).\nImage (Developer/QC).\n\nContent Team:\nCynnar (Project Lead).\nNeatz09 (Content Team Lead).\nContent Developers: Dorbin, LordPazuzu, Neveruary, Premerio15.\n\nContent Tooling:\nTheFoof (Tooling Lead)\nEmemJR (Developer)\nSplashsky (Developer)\n\nPast/Inactive Developers of note:\nFounding Members: LethalEncounter, JohnAdams\nPast Project Developers Include: Jabantiz, Xinux, Scatman, Scribbles\n\n...And Many Many More.```";
        $message->reply($msg);
        return;
    }

    //example command that uses mysql to interact as well as functions from eq2emubot-functions.php
    if (preg_match("/{$command5}/i", $message->content)) {
        $isadmin = isadmin($message->author->username, $message->user_id);
        $link = mysqli_connect(MYSQLHOST, MYSQLUSER, MYSQLPASS, MYSQLDB);

        $sql_count = "SELECT count(*) from characters where is_online=1";
        $count = $link->query($sql_count);
        $row = mysqli_fetch_row($count);
        $count2 = $row[0];

        if ($count2 == 0) {
            $msg = "\nThere are no players currently online.";
            $message->reply($msg);
            mysqli_close($link);
            return;
        }

        $msg = onlineusers($isadmin);
        $msg .= "```";
        $msg .= "Total Online " . $count2;
        mysqli_close($link);
        $message->reply($msg);
    }

    /////////////////////////// COMMAND HANDLERS END
});

$discord->run();
?>
