Skip to content

Commit

Permalink
Added skeet command
Browse files Browse the repository at this point in the history
  • Loading branch information
PeeHaa committed Apr 9, 2016
1 parent cd3a2af commit 1526911
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Chat/Plugin/Chuck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Room11\Jeeves\Chat\Command\Message;

class Chuck implements Plugin {
const COMMAND = "chuck";
const COMMANDS = ["chuck", "skeet"];

private $chatClient;

Expand All @@ -25,7 +25,7 @@ public function handle(Message $message): \Generator {

private function validMessage(Message $message): bool {
return $message instanceof Command
&& $message->getCommand() === self::COMMAND;
&& in_array($message->getCommand(), self::COMMANDS, true);
}


Expand All @@ -39,14 +39,22 @@ private function getResult(Message $message): \Generator {
$this->chatClient->postMessage("not works :(");

if(isset($result["type"]) && $result["type"] == "success") {
yield from $this->postMessage($result);
yield from $this->postMessage($this->skeetify($message, $result["value"]["joke"]));
} else {
yield from $this->postError($message);
}
}

private function postMessage(array $result): \Generator {
$joke = htmlspecialchars_decode($result["value"]["joke"]);
private function skeetify(Message $message, string $joke): string {
if ($message->getCommand() !== "skeet") {
return $joke;
}

return str_replace("Chuck Norris", "Jon Skeet", $joke);
}

private function postMessage(string $joke): \Generator {
$joke = htmlspecialchars_decode($joke);

yield from $this->chatClient->postMessage($joke);
}
Expand Down

0 comments on commit 1526911

Please sign in to comment.