Added "Gimme" command

The Gimme command allows you to get stuff from various places
Also removed MinecraftInfo and Playback commands.
This commit is contained in:
Miguel Nogueira 2020-12-27 18:21:03 +00:00
parent 2a2d2a0b7e
commit 6021155289
Signed by: miguel456
GPG Key ID: 2CF61B825316C6A0
10 changed files with 157 additions and 138 deletions

View File

@ -5,6 +5,8 @@ BOT_AUTH_TOKEN=""
# The prefix the bot responds to. # The prefix the bot responds to.
COMMAND_PREFIX="" COMMAND_PREFIX=""
# Sign up for an API key at the CatAPI website. Used for commands in the Fun category.
CAT_API_KEY=""
# JSON Web Token Configuration # JSON Web Token Configuration
RASPBERRY_STAFF_URL="" RASPBERRY_STAFF_URL=""

12
Commandfiles/fun.txt Normal file
View File

@ -0,0 +1,12 @@
:white_check_mark: Raspberry Bot Command Help - Fun Commands :arrow_down:
Active prefix: <prefix>
Available commands:
- gimme [what: The thing you want to get.] [count: how many? (leave 0 if not applicable - empty arguments are not allowed)]
- Avaliable things:
- cat - Gives you a cat picture. Maximum of 25 cat pictures. :cat: :heart:
- Example: <prefix> gimme cat 2 - Gets you two cat pictures

View File

@ -6,3 +6,6 @@ Active prefix: <prefix>
Available commands: Available commands:
- numberfact [random/ (int) number] (Example: <prefix> numberfact 20) - numberfact [random/ (int) number] (Example: <prefix> numberfact 20)
- help (This command) - help (This command)
Other help categories:
- fun (? help fun)

View File

@ -2,6 +2,7 @@
<?php <?php
use nogueiracodes\RaspberryBot\Bot\Actions\MessageLogger; use nogueiracodes\RaspberryBot\Bot\Actions\MessageLogger;
use nogueiracodes\RaspberryBot\Bot\Commands\Gimme;
use nogueiracodes\RaspberryBot\Bot\Commands\MinecraftInfo; use nogueiracodes\RaspberryBot\Bot\Commands\MinecraftInfo;
use nogueiracodes\RaspberryBot\Bot\Commands\NumberFact; use nogueiracodes\RaspberryBot\Bot\Commands\NumberFact;
use nogueiracodes\RaspberryBot\Bot\Commands\SimplePlayback; use nogueiracodes\RaspberryBot\Bot\Commands\SimplePlayback;
@ -18,9 +19,8 @@ echo 'This console will now display logging output from actions and commands sen
$raspberryBot $raspberryBot
->initialize() ->initialize()
->addCommand([ ->addCommand([
new SimplePlayback,
new NumberFact, new NumberFact,
new MinecraftInfo, new Gimme,
new Help new Help
]) ])
->addAction(new MessageLogger) ->addAction(new MessageLogger)

View File

@ -0,0 +1,56 @@
<?php declare(strict_types=1);
namespace nogueiracodes\RaspberryBot\Bot\Commands;
use Exception;
use nogueiracodes\RaspberryBot\Bot\Helpers\CatAPI;
use nogueiracodes\RaspberryBot\Core\Interfaces\Command;
use nogueiracodes\RaspberryBot\Traits\HasSignature;
class Gimme implements Command
{
use HasSignature;
public $signature = "gimme {what: What to give you. Can be a cat pic!} {quantity: How many cat pictures do you want?}";
private $cats;
public function __construct()
{
$this->cats = new CatAPI();
}
public function run(array $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!');
}
switch($parameters['what']) {
case "cat":
try
{
// Many pictures will return a list of pictures, separated by new lines
return $this->cats->getCatPicture($parameters['quantity'], "small");
}
catch (Exception $ex)
{
throw $ex; // Let the bot handle this. It sends error messages as responses to keep things clean.
}
break;
default:
throw new Exception("Uh oh. I don\'t know what you mean by " . $parameters['what'] . " Maybe try ? help?");
break;
}
}
}

View File

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace nogueiracodes\RaspberryBot\Bot\Commands; namespace nogueiracodes\RaspberryBot\Bot\Commands;
@ -18,6 +18,7 @@ class Help implements Command
public function run(array $parameters) public function run(array $parameters)
{ {
// This could be a little cleaner.
switch($parameters['section']) switch($parameters['section'])
{ {
case "commands": case "commands":
@ -31,7 +32,20 @@ class Help implements Command
return ":x: There is currently no available help."; return ":x: There is currently no available help.";
} }
break; break;
case "fun":
if(file_exists(PROJECT_ROOT . "/Commandfiles/fun.txt"))
{
return str_replace("<prefix>", $_ENV['COMMAND_PREFIX'], file_get_contents(PROJECT_ROOT . "/Commandfiles/fun.txt"));
}
else
{
return ":x: There is currently no available help.";
}
break;
default: default:
return ":x: There is no help section named " . $parameters['section'] . "."; return ":x: There is no help section named " . $parameters['section'] . ".";

View File

@ -1,81 +0,0 @@
<?php
namespace nogueiracodes\RaspberryBot\Bot\Commands;
use Ely\Mojang\Api;
use Exception;
use nogueiracodes\RaspberryBot\Core\Interfaces\Command;
use nogueiracodes\RaspberryBot\Traits\HasSignature;
// Inspired by Laravel's artisan commands
class MinecraftInfo implements Command
{
use HasSignature;
/**
* Must be included at all times. This describes the command's parameters, which can be used in the run method.
*
* @var string
*/
private $signature = "minecraft {operation: The The kind of operation; for now, only convert is supported} {subOperationType: The sub operation type} {subOperationArgument: The argument you want to pass to to the sub operation}";
/**
* Parrots back whatever the user said. Proof of concept.
*
* @param string $parameters This is an array of named parameters with values, based on the signature. First value is always the command's name, like how PHP's $argv is organized.
* @return void
*/
public function run(array $parameters)
{
$mojangAPI = new Api();
switch($parameters['operation'])
{
// convert to username
case 'convert':
switch($parameters['subOperationType'])
{
case 'username':
$response = $parameters['subOperationType'] . "\'s UUID is " . $mojangAPI->usernameToUUID($parameters['subOperationArgument'])->getId();
break;
default:
throw new Exception("Sorry, but at the moment you can only convert usernames to UUIDs.");
}
break;
case 'blacklist':
switch($parameters['subOperationType'])
{
case 'is-banned':
$blockedServerCollection = $mojangAPI->blockedServers();
$response = ($blockedServerCollection->isBlocked($parameters['subOperationArgument']))
? 'Sorry! It appears that this server is indeed being blocked by Mojang. Users won\'t be able to join this Minecraft server using the official client.'
: 'A little bird told me that this server is NOT blocked by Mojang. Users can freely join this server.';
break;
default:
throw new Exception("Sorry, but at the moment you can only check if servers are banned.");
}
break;
default:
throw new Exception("Invalid usage! Please refer to !!rb help for more information.");
}
return $response;
}
}

View File

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace nogueiracodes\RaspberryBot\Bot\Commands; namespace nogueiracodes\RaspberryBot\Bot\Commands;

View File

@ -1,52 +0,0 @@
<?php
namespace nogueiracodes\RaspberryBot\Bot\Commands;
use nogueiracodes\RaspberryBot\Core\Interfaces\Command;
use nogueiracodes\RaspberryBot\Traits\HasSignature;
// Inspired by Laravel's artisan commands
class SimplePlayback implements Command
{
use HasSignature;
/**
* Must be included at all times. This describes the command's parameters, which can be used in the run method.
*
* @var string
*/
private $signature = "capitalize {word: The word you want to convert} {state: lower or upper}";
/**
* Parrots back whatever the user said. Proof of concept.
*
* @param string $parameters This is an array of named parameters with values, based on the signature. First value is always the command's name, like how PHP's $argv is organized.
* @return void
*/
public function run(array $parameters)
{
// Play with the string first before sending it
switch($parameters['state'])
{
case 'lower':
$capitalized = strtolower($parameters['word']);
break;
case 'upper':
$capitalized = strtoupper($parameters['word']);
break;
default:
$capitalized = "Usage: !!rb capitalize [word] [upper|lower]. Use !!rb help for more.";
}
return $capitalized;
}
}

View File

@ -0,0 +1,65 @@
<?php declare(strict_types=1);
use GuzzleHttp\Client as Client;
use GuzzleHttp\Exception;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\ClientException;
namespace nogueiracodes\RaspberryBot\Bot\Helpers;
use Exception;
use InvalidArgumentException;
use Zttp\Zttp;
class CatAPI {
private $headers;
private $baseURL = "https://api.thecatapi.com/v1/";
public function __construct()
{
$this->headers = [
'x-api-key' => $_ENV['CAT_API_KEY']
];
}
public function getCatPicture($count = 1, $size = "small"): string {
// 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())
{
$results = $catPic->json();
$pics = [];
if (count($results) == 1)
{
return $results[0]['url'];
}
foreach($results as $result)
{
array_push($pics, $result['url']);
}
return implode(PHP_EOL, $pics);
}
else
{
throw new Exception('Sorry, but I couldn\'t get you a good cat picture this time. Try again later!');
}
}
}