/** * Connect to IRC */ public function connect() { $channels = $this->channels; $this->irc->on('open', function (Bucket $bucket) use($channels) { foreach ($channels as $channel) { $bucket->getSource()->join($this->nick, $channel); } return; }); $this->irc->on('invite', function (Bucket $bucket) use($channels) { $data = $bucket->getData(); $bucket->getSource()->join($this->nick, $data['invitation_channel']); return; }); $this->irc->on('private-message', function (Bucket $bucket) { $data = $bucket->getData(); $message = $data['message']; if (Str::startsWith($message, 'join')) { $channel = explode(' ', $message)[1]; $bucket->getSource()->join($this->nick, $channel); } return; }); $this->irc->on('message', function (Bucket $bucket) { $data = $bucket->getData(); $message = trim($data['message']); foreach ($this->commands as $command) { $command = new $command(); if (Str::startsWith($message, $command->trigger())) { $args = array_values(array_filter(explode(' ', $message), function ($item) use($command) { return $item != $command->trigger(); })); try { $response = $command->run($args); $bucket->getSource()->say($response); } catch (\Exception $e) { $bucket->getSource()->say("Something went wrong " . $e->getMessage()); } } } return; }); $this->irc->run(); }
/** * Constructor. * * @access public * @param \Hoa\Socket\Client $client Client. * @return void * @throw \Hoa\Socket\Exception */ public function __construct(Socket\Client $client) { parent::__construct($client); $this->_on->addIds(['mode', 'nick', 'notice', 'part', 'quit']); return; }
public function loadEvents(IrcClient $ircClient) { foreach ($this->getSubscribedEvents() as $name => $method) { $ircClient->on($name, array($this, $method)); } }