forked from miguel456/luke-bot
Added proper logging and error handling
This commit is contained in:
@@ -2,14 +2,21 @@
|
||||
|
||||
namespace nogueiracodes\RaspberryBot\Bot\Actions;
|
||||
|
||||
use Analog\Analog;
|
||||
use nogueiracodes\RaspberryBot\Core\Interfaces\Action;
|
||||
|
||||
class MessageLogger implements Action
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Analog::info('Actions: Loaded MessageLogger!');
|
||||
}
|
||||
|
||||
public function processAction($message)
|
||||
{
|
||||
|
||||
echo "Sniffed message from " . $message->author->username . ": " . $message->content . PHP_EOL;
|
||||
Analog::info("Siffer: Sniffed message from " . $message->author->username . ": " . $message->content)
|
||||
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace nogueiracodes\RaspberryBot\Bot\Commands;
|
||||
|
||||
use Analog\Analog;
|
||||
use Exception;
|
||||
use nogueiracodes\RaspberryBot\Bot\Helpers\CatAPI;
|
||||
use nogueiracodes\RaspberryBot\Core\Interfaces\Command;
|
||||
@@ -20,10 +21,19 @@ class Gimme implements Command
|
||||
public function __construct()
|
||||
{
|
||||
$this->cats = new CatAPI();
|
||||
Analog::info('Commands: Loaded Gimme!');
|
||||
}
|
||||
|
||||
public function run(array $parameters)
|
||||
{
|
||||
Analog::debug('Gimme: Nr of args: ' . count($parameters));
|
||||
|
||||
|
||||
|
||||
Analog::debug('Gimme: Thing: ' . $parameters['thing'] ?? 'unset');
|
||||
Analog::debug('Gimme: Count: ' . $parameters['quantity'] ?? 'unset');
|
||||
Analog::debug('Gimme: Raw parameter contents: ', json_encode($parameters));
|
||||
|
||||
if ($parameters['quantity'] > 25)
|
||||
{
|
||||
throw new Exception('Sorry, but I can\'t send you more than 25 cat pictures at a time. What a cat lover!');
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace nogueiracodes\RaspberryBot\Bot\Commands;
|
||||
|
||||
use Analog\Analog;
|
||||
use Exception;
|
||||
use nogueiracodes\RaspberryBot\Core\Interfaces\Command;
|
||||
use nogueiracodes\RaspberryBot\Traits\HasSignature;
|
||||
@@ -14,6 +15,10 @@ class Help implements Command
|
||||
|
||||
public $signature = "help {section: The section to retrieve help for}";
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Analog::info('Commands: Loaded Help!');
|
||||
}
|
||||
|
||||
public function run(array $parameters)
|
||||
{
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace nogueiracodes\RaspberryBot\Bot\Commands;
|
||||
|
||||
use Analog\Analog;
|
||||
use Exception;
|
||||
use nogueiracodes\RaspberryBot\Core\Interfaces\Command;
|
||||
use nogueiracodes\RaspberryBot\Traits\HasSignature;
|
||||
@@ -15,6 +16,11 @@ class NumberFact implements Command
|
||||
public $signature = "numberfact {number: The number for which you want facts for.}";
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Analog::log('Commands: Loaded NumberFact!');
|
||||
}
|
||||
|
||||
public function run(array $parameters)
|
||||
{
|
||||
|
||||
|
@@ -7,6 +7,7 @@ use GuzzleHttp\Exception\ClientException;
|
||||
|
||||
namespace nogueiracodes\RaspberryBot\Bot\Helpers;
|
||||
|
||||
use Analog\Analog;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Zttp\Zttp;
|
||||
@@ -18,37 +19,69 @@ class CatAPI {
|
||||
|
||||
private $baseURL = "https://api.thecatapi.com/v1/";
|
||||
|
||||
|
||||
private $hardLimit = 25;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->headers = [
|
||||
'x-api-key' => $_ENV['CAT_API_KEY']
|
||||
];
|
||||
Analog::info('Helpers: Loaded new instance of CatAPI!');
|
||||
|
||||
if (isset($_ENV['CAT_API_KEY']))
|
||||
{
|
||||
$this->headers = [
|
||||
'x-api-key' => $_ENV['CAT_API_KEY']
|
||||
];
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->hardLimit = 1;
|
||||
Analog::urgent('CatAPI ERROR: Failed to load Cat API key. Results limited to 1 picture. It is recommended set an API key.');
|
||||
}
|
||||
}
|
||||
|
||||
public function getCatPicture($count = 1, $size = "small"): string {
|
||||
|
||||
Analog::debug('CatAPI: Fetching cat picture with count ' . $count . ' and size ' . $size);
|
||||
|
||||
// anti smart-ass device
|
||||
if ($count = 0)
|
||||
$count = 1;
|
||||
|
||||
$catPic = Zttp::withHeaders($this->headers)->get($this->baseURL . 'images/search' . http_build_query([
|
||||
'limit' => $count,
|
||||
'size' => $size,
|
||||
'has_breeds' => false
|
||||
]));
|
||||
|
||||
if ($catPic->isOk())
|
||||
if ($count > 0 && $count <= $this->hardLimit)
|
||||
{
|
||||
Analog::error('CatAPI: Invalid arguments: Count of ' . $count . ' is over the hard limit of ' . $this->hardLimit);
|
||||
throw new Exception("Unacceptable quantity: " . $count . ". Please try again with no more than " . $this->hardLimit . " picture(s).");
|
||||
}
|
||||
|
||||
if (!empty($this->headers))
|
||||
{
|
||||
$catPic = Zttp::withHeaders($this->headers)->get($this->baseURL . 'images/search', [
|
||||
'limit' => $count,
|
||||
'size' => $size,
|
||||
'has_breeds' => false
|
||||
]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$catPic = Zttp::get($this->baseURL . 'images/search', [
|
||||
'limit' => $count,
|
||||
'size' => $size,
|
||||
'has_breeds' => false
|
||||
]);
|
||||
}
|
||||
|
||||
if ($catPic->isSuccess())
|
||||
{
|
||||
|
||||
$results = $catPic->json();
|
||||
$pics = [];
|
||||
|
||||
if (count($results) == 1)
|
||||
{
|
||||
Analog::debug('CatAPI: Fetched single cat picture.');
|
||||
return $results[0]['url'];
|
||||
}
|
||||
|
||||
foreach($results as $result)
|
||||
{
|
||||
Analog::debug('CatAPI: Fetched multiple cat pictures; Splitting into EOL divided string.');
|
||||
array_push($pics, $result['url']);
|
||||
}
|
||||
|
||||
@@ -57,6 +90,7 @@ class CatAPI {
|
||||
}
|
||||
else
|
||||
{
|
||||
Analog::error('CatAPI: Network error. Failed to fetch cat picture. Request status code: ' . $catPic->status());
|
||||
throw new Exception('Sorry, but I couldn\'t get you a good cat picture this time. Try again later!');
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Analog\Analog;
|
||||
use Analog\Handler\File;
|
||||
|
||||
require realpath(__DIR__ . '/../../vendor/autoload.php');
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(realpath(__DIR__ . '/../../'));
|
||||
@@ -10,3 +13,7 @@ $dotenv->required('COMMAND_PREFIX')->notEmpty();
|
||||
$dotenv->required('CAT_API_KEY')->notEmpty();
|
||||
|
||||
define('PROJECT_ROOT', realpath(__DIR__ . '/../../'));
|
||||
|
||||
Analog::handler(File::init(PROJECT_ROOT . 'Storage/Logs/bot.log'));
|
||||
|
||||
Analog::info("Finished intializing configuration and dependencies.");
|
||||
|
Reference in New Issue
Block a user