From 6021155289783870cda55d0dc5460b39d8a02e14 Mon Sep 17 00:00:00 2001 From: Miguel N Date: Sun, 27 Dec 2020 18:21:03 +0000 Subject: [PATCH] Added "Gimme" command The Gimme command allows you to get stuff from various places Also removed MinecraftInfo and Playback commands. --- .env.example | 2 + Commandfiles/fun.txt | 12 +++++ Commandfiles/help.txt | 3 ++ bot.php | 4 +- src/Bot/Commands/Gimme.php | 56 ++++++++++++++++++++ src/Bot/Commands/Help.php | 18 ++++++- src/Bot/Commands/MinecraftInfo.php | 81 ----------------------------- src/Bot/Commands/NumberFact.php | 2 +- src/Bot/Commands/SimplePlayback.php | 52 ------------------ src/Bot/Helpers/CatAPI.php | 65 +++++++++++++++++++++++ 10 files changed, 157 insertions(+), 138 deletions(-) create mode 100644 Commandfiles/fun.txt create mode 100644 src/Bot/Commands/Gimme.php delete mode 100644 src/Bot/Commands/MinecraftInfo.php delete mode 100644 src/Bot/Commands/SimplePlayback.php create mode 100644 src/Bot/Helpers/CatAPI.php diff --git a/.env.example b/.env.example index aca91de..756669a 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,8 @@ BOT_AUTH_TOKEN="" # The prefix the bot responds to. 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 RASPBERRY_STAFF_URL="" diff --git a/Commandfiles/fun.txt b/Commandfiles/fun.txt new file mode 100644 index 0000000..9370170 --- /dev/null +++ b/Commandfiles/fun.txt @@ -0,0 +1,12 @@ + +:white_check_mark: Raspberry Bot Command Help - Fun Commands :arrow_down: + +Active 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: gimme cat 2 - Gets you two cat pictures diff --git a/Commandfiles/help.txt b/Commandfiles/help.txt index 0299cc9..490deea 100644 --- a/Commandfiles/help.txt +++ b/Commandfiles/help.txt @@ -6,3 +6,6 @@ Active prefix: Available commands: - numberfact [random/ (int) number] (Example: numberfact 20) - help (This command) + +Other help categories: + - fun (? help fun) \ No newline at end of file diff --git a/bot.php b/bot.php index 01645e4..bf0f681 100755 --- a/bot.php +++ b/bot.php @@ -2,6 +2,7 @@ initialize() ->addCommand([ - new SimplePlayback, new NumberFact, - new MinecraftInfo, + new Gimme, new Help ]) ->addAction(new MessageLogger) diff --git a/src/Bot/Commands/Gimme.php b/src/Bot/Commands/Gimme.php new file mode 100644 index 0000000..2e51cc1 --- /dev/null +++ b/src/Bot/Commands/Gimme.php @@ -0,0 +1,56 @@ +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; + + } + } +} \ No newline at end of file diff --git a/src/Bot/Commands/Help.php b/src/Bot/Commands/Help.php index b00ff9c..f83d152 100644 --- a/src/Bot/Commands/Help.php +++ b/src/Bot/Commands/Help.php @@ -1,4 +1,4 @@ -", $_ENV['COMMAND_PREFIX'], file_get_contents(PROJECT_ROOT . "/Commandfiles/fun.txt")); + } + else + { + return ":x: There is currently no available help."; + } + + break; default: return ":x: There is no help section named " . $parameters['section'] . "."; diff --git a/src/Bot/Commands/MinecraftInfo.php b/src/Bot/Commands/MinecraftInfo.php deleted file mode 100644 index b4f4695..0000000 --- a/src/Bot/Commands/MinecraftInfo.php +++ /dev/null @@ -1,81 +0,0 @@ -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; - - } - -} \ No newline at end of file diff --git a/src/Bot/Commands/NumberFact.php b/src/Bot/Commands/NumberFact.php index 2818050..be79a78 100644 --- a/src/Bot/Commands/NumberFact.php +++ b/src/Bot/Commands/NumberFact.php @@ -1,4 +1,4 @@ -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!'); + } + + } + +} \ No newline at end of file