3 namespace App\TwitchBot;
5 use App\Models\Channel;
6 use App\Models\TwitchBotCommand;
7 use App\Models\TwitchToken;
8 use Monolog\Handler\StreamHandler;
10 use Ratchet\Client\Connector;
11 use Ratchet\Client\WebSocket;
12 use Ratchet\RFC6455\Messaging\Message;
13 use React\EventLoop\Loop;
17 public function __construct($nick) {
19 $this->logger = new Logger('TwitchBot');
20 $this->logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
22 $this->token = TwitchToken::firstWhere('nick', $nick);
24 throw new \Exception('unable to find access token');
27 $this->connector = new Connector();
32 public function getLogger() {
36 public function getLoop() {
40 public function isReady() {
44 public function run() {
45 $this->shutting_down = false;
46 $this->getLoop()->run();
49 public function stop() {
50 $this->logger->info('shutting down');
51 $this->shutting_down = true;
53 $this->getLoop()->stop();
57 public function connect() {
58 ($this->connector)('wss://irc-ws.chat.twitch.tv:443')->done(
59 [$this, 'handleWsConnect'],
60 [$this, 'handleWsConnectError'],
64 public function disconnect() {
68 public function handleWsConnect(WebSocket $ws) {
69 $this->logger->info('websocket connection established');
71 $ws->on('message', [$this, 'handleWsMessage']);
72 $ws->on('close', [$this, 'handleWsClose']);
73 $ws->on('error', [$this, 'handleWsError']);
74 $ws->send('CAP REQ :twitch.tv/tags twitch.tv/commands');
78 public function handleWsConnectError(WebSocket $ws) {
79 $this->logger->error('failed to establish websocket connection');
82 public function handleWsMessage(Message $message, WebSocket $ws) {
83 $irc_messages = explode("\r\n", rtrim($message->getPayload(), "\r\n"));
84 foreach ($irc_messages as $irc_message) {
85 $this->logger->info('received IRC message '.$irc_message);
86 $this->handleIRCMessage(IRCMessage::fromString($irc_message));
90 public function handleWsClose(int $op, string $reason) {
92 $this->logger->info('websocket connection closed: '.$reason.' ['.$op.']');
93 if (!$this->shutting_down) {
94 $this->logger->info('reconnecting in 5 seconds');
95 Loop::addTimer(5, [$this, 'connect']);
99 public function handleWsError(\Exception $e, WebSocket $ws) {
100 $this->logger->error('websocket error '.$e->getMessage());
104 public function handleIRCMessage(IRCMessage $msg) {
105 $this->last_contact = time();
106 if ($msg->isPing()) {
107 $this->sendIRCMessage($msg->makePong());
110 if ($msg->isPong()) {
113 $this->logMessage($msg);
114 if ($msg->isPrivMsg()) {
115 $this->handlePrivMsg($msg);
118 if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
119 $this->logger->notice('login failed, refreshing access token');
120 $this->token->refresh();
124 if ($msg->command == '001') {
126 $this->joinChannels();
132 public function getMessageChannel(IRCMessage $msg) {
133 $target = $msg->getPrivMsgTarget();
134 if (substr($target, 0, 1) !== '#') {
135 $target = '#'.$target;
137 return Channel::firstWhere('twitch_chat', '=', $target);
140 public function logMessage(IRCMessage $msg) {
143 public function handlePrivMsg(IRCMessage $msg) {
146 public function login() {
147 $this->ws->send('PASS oauth:'.$this->token->access);
148 $this->ws->send('NICK '.$this->nick);
151 public function joinChannels() {
154 private function startPinger() {
155 $this->getLoop()->addPeriodicTimer(15, function () {
156 if (!$this->ready) return;
157 if (time() - $this->last_contact < 60) return;
159 $this->sendIRCMessage(IRCMessage::ping($this->nick));
160 } catch (\Exception $e) {
165 public function sendIRCMessage(IRCMessage $msg) {
166 $irc_message = $msg->encode();
167 $this->logger->info('sending IRC message '.$irc_message);
168 $this->ws->send($irc_message);
169 $this->last_contact = time();
173 protected function listenCommands() {
174 $this->getLoop()->addPeriodicTimer(1, function () {
175 if (!$this->isReady()) return;
176 $command = TwitchBotCommand::where('bot_nick', '=', $this->nick)->where('status', '=', 'pending')->oldest()->first();
179 $command->execute($this);
180 } catch (\Exception $e) {
194 private $ready = false;
195 private $shutting_down = false;
197 private $last_contact;