/** * Does something. Not sure what the hell it is, though. * * * @return bool */ public function input(Message $message) { $input = $message->getText(); if (!is_string($input) || !isset($input[0]) || $input[0] !== '!') { return FALSE; } // [!kill, #channel, @name] $input_array = explode(' ', $input); // Remove "!" from first element of array and set to lowercase. $command = strtolower(substr($input_array[0], 1)); $args = []; foreach ($input_array as $i => $arg) { if ($i == 0) { continue; } // Skip the command if (empty($arg)) { continue; } $args[] = $arg; } if ($command == NULL) { return FALSE; } try { /* @var \Slackwolf\Game\Command\Command $command */ $command = CommandFactory::getCommand($command, $this->client, $this, $message, $args); $command->fire(); } catch (Exception $e) { return FALSE; } return TRUE; }
/** * Command constructor. * * @param RealTimeClient $client * The Slack API client. * * @param GameManager $gameManager * The game manager. * * @param \Slackwolf\Message\Message $message * The message object. * * @param array|NULL $args */ public function __construct(RealTimeClient $client, GameManager $gameManager, Message $message, array $args = null) { $this->client = $client; $this->gameManager = $gameManager; $this->message = $message; $this->userId = $message->getUser(); $this->channel = $message->getChannel(); $this->args = $args; $this->game = $this->gameManager->getGame($this->channel); echo get_called_class() . " " . $this->userId . " " . $this->channel . "\r\n"; }
public function input(Message $message) { $input = $message->getText(); if (!is_string($input)) { return false; } if (!isset($input[0])) { return false; } if ($input[0] !== '!') { return false; } $input_array = explode(' ', $input); $command = $input_array[0]; if (strlen($command) < 2) { return false; } $command = substr($command, 1); $args = []; foreach ($input_array as $i => $arg) { if ($i == 0) { continue; } // Skip the command if (empty($arg)) { continue; } $args[] = $arg; } if ($command == null) { return false; } $command = strtolower($command); if (!isset($this->commandBindings[$command])) { return false; } try { /** @var Command $command */ $command = new $this->commandBindings[$command]($this->client, $this, $message, $args); $command->fire(); } catch (Exception $e) { return false; } return true; }
/** * @param Message $message * * @return bool */ public function input(Message $message) { $input = $message->getText(); if (!is_string($input) || !isset($input[0]) || $input[0] !== '!') { return FALSE; } // Example: [!kill, #channel, @name] $input_array = explode(' ', $input); // Remove "!" from first element of array and set to lowercase. $command = strtolower(substr($input_array[0], 1)); if (strlen($command) < 2) { return false; } $args = []; foreach ($input_array as $i => $arg) { if ($i == 0) { continue; } // Skip the command if (empty($arg)) { continue; } $args[] = $arg; } if ($command == null) { return false; } if (!isset($this->commandBindings[$command])) { return false; } try { /** @var Command $command */ $command = new $this->commandBindings[$command]($this->client, $this, $message, $args); $command->fire(); } catch (Exception $e) { return false; } return true; }
public function run() { /* * Create the event loop */ $eventLoop = Factory::create(); /* * Create our Slack client */ $client = new SlackRTMClient($eventLoop); $client->setToken(getenv('BOT_TOKEN')); /* * Setup command bindings */ $commandBindings = ['help' => HelpCommand::class, 'setoption' => SetOptionCommand::class, 'new' => NewCommand::class, 'join' => JoinCommand::class, 'leave' => LeaveCommand::class, 'start' => StartCommand::class, 'end' => EndCommand::class, 'see' => SeeCommand::class, 'vote' => VoteCommand::class, 'kill' => KillCommand::class, 'guard' => GuardCommand::class]; /* * Create the game manager */ $gameManager = new GameManager($client, $commandBindings); /* * Route incoming Slack messages */ $client->on('message', function ($data) use($client, $gameManager) { $message = new Message($data); if ($message->getSubType() == 'channel_join') { $client->refreshChannel($message->getChannel()); } else { if ($message->getSubType() == 'channel_leave') { $client->refreshChannel($message->getChannel()); } else { $gameManager->input($message); } } }); /* * Connect to Slack */ echo "Connecting...\r\n"; $client->connect()->then(function () { echo "Connected.\n"; }, function (ConnectionException $e) { echo $e->getMessage(); exit; }); /* * Start the event loop */ $eventLoop->run(); }
public function run() { /* * Create the event loop */ $eventLoop = Factory::create(); /* * @var \Slackwolf\SlackRTMClient */ $client = new SlackRTMClient($eventLoop); $client->setToken($this->token); /* * Create the game manager */ $gameManager = new GameManager($client); /* * Route incoming Slack messages */ $client->on('message', function ($data) use($client, $gameManager) { $message = new Message($data); if ($message->getSubType() == 'channel_join') { $client->refreshChannel($message->getChannel()); } else { if ($message->getSubType() == 'channel_leave') { $client->refreshChannel($message->getChannel()); } else { $gameManager->input($message); } } }); /* * Connect to Slack */ echo "Connecting...\r\n"; $client->connect()->then(function () { echo "Connected.\n"; }, function (ConnectionException $e) { echo $e->getMessage(); exit; }); /* * Start the event loop */ $eventLoop->run(); }