diff --git a/bot.php b/bot.php index 47adbc4c..d74ac038 100644 --- a/bot.php +++ b/bot.php @@ -37,6 +37,7 @@ function execInBackgroundLinux($cmd) { 'loop' => $loop, 'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS, // default intents as well as guild members ]); +$filesystem = \React\Filesystem\Factory::create($loop); function portIsAvailable(int $port = 1714): bool { @@ -78,40 +79,51 @@ function search_players(string $ckey): string } else return 'Unable to access playerlogs.txt!'; } -function ooc_relay($guild, string $file_path, string $channel_id) +function ooc_relay($filesystem, $guild, string $file_path, string $channel_id) { - if ($file = fopen($file_path, "r+")) { - while (($fp = fgets($file, 4096)) !== false) { - $fp = str_replace(PHP_EOL, "", $fp); - if ($target_channel = $guild->channels->offsetGet($channel_id)) - $target_channel->sendMessage($fp); - } - ftruncate($file, 0); //clear the file - fclose($file); + if ($target_channel = $guild->channels->offsetGet($channel_id)) { + $file = $filesystem->file($file_path); + $file->getContents()->then(function (string $contents) use ($file, $target_channel) { + $promise = React\Async\async(function () use ($contents, $file, $target_channel) { + $lines = explode(PHP_EOL, $contents); + $fn = function ($lines, $target_channel) { + foreach ($lines as $line) { + if ($line) $target_channel->sendMessage($line); + else echo '[RELAY - EMPTY LINE] ' . PHP_EOL; + } + }; + $response = React\Async\await($fn($lines, $target_channel)); + }); + $promise->then(function () use ($file) { + echo '[RELAY] ' . PHP_EOL; + $file->putContents(''); + }, function (Exception $e) { + echo '[ERROR] ' . $e->getMessage() . PHP_EOL; + }); + })->done(); } } -function timer_function($discord) +function timer_function(\Discord\Discord $discord, $filesystem) { if ($guild = $discord->guilds->offsetGet('468979034571931648')) { - ooc_relay($guild, '/home/1713/civ13-rp/ooc.log', '468979034571931648'); // #ooc-nomads - ooc_relay($guild, '/home/1713/civ13-rp/admin.log', '637046890030170126'); // #ahelp-nomads - ooc_relay($guild, '/home/1713/civ13-tdm/ooc.log', '636644391095631872'); // #ooc-tdm - ooc_relay($guild, '/home/1713/civ13-tdm/admin.log', '637046904575885322'); // #ahelp-tdm + ooc_relay($filesystem, $guild, '/home/1713/civ13-rp/ooc.log', '636644156923445269'); // #ooc-nomads + ooc_relay($filesystem, $guild, '/home/1713/civ13-rp/admin.log', '637046890030170126'); // #ahelp-nomads + ooc_relay($filesystem, $guild, '/home/1713/civ13-tdm/ooc.log', '636644391095631872'); // #ooc-tdm + ooc_relay($filesystem, $guild, '/home/1713/civ13-tdm/admin.log', '637046904575885322'); // #ahelp-tdm } } -function on_ready($discord) +$on_ready = function (\Discord\Discord $discord) use ($filesystem) { - echo 'Logged in as ' . $discord->user->username . "#" . $discord->user->discriminator . ' ' . $discord->id . PHP_EOL; - echo('------' . PHP_EOL); + echo 'Logged in as ' . $discord->user->username . "#" . $discord->user->discriminator . ' ' . $discord->id . PHP_EOL . '------' . PHP_EOL; if (! isset($GLOBALS['relay_timer']) || (! $GLOBALS['relay_timer'] instanceof React\EventLoop\Timer\Timer) ) { - $GLOBALS['relay_timer'] = $discord->getLoop()->addPeriodicTimer(10, function() use ($discord) { - timer_function($discord); + $GLOBALS['relay_timer'] = $discord->getLoop()->addPeriodicTimer(10, function() use ($discord, $filesystem) { + timer_function($discord, $filesystem); }); } -} +}; function on_message($message, $discord, $loop, $command_symbol = '!s') { @@ -981,9 +993,9 @@ function on_message2($message, $discord, $loop, $command_symbol = '!s') { } } -$discord->once('ready', function ($discord) use ($loop, $command_symbol) +$discord->once('ready', function ($discord) use ($loop, $command_symbol, $on_ready) { - on_ready($discord); + $on_ready($discord); $discord->on('message', function ($message) use ($discord, $loop, $command_symbol) { //Handling of a message if ($message->channel->type == 1) return; //Only process commands from a guild diff --git a/composer.json b/composer.json index 7bfdf676..cd787f45 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "require": { "php": "^8.0.0", "team-reflex/discord-php": "dev-master", - "react/filesystem": "^0.2@dev" + "react/filesystem": "^0.2@dev", + "react/async": "^4@dev" }, "suggest": { "ext-uv": "For a faster, and more performant loop. PHP >=7 only. Preferred.", @@ -23,5 +24,10 @@ "unit": ["./vendor/bin/phpunit"] }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "config": { + "allow-plugins": { + "wyrihaximus/composer-update-bin-autoload-path": true + } + } }