namespace App\TwitchBot;
use App\Models\Channel;
+use App\Models\TwitchBotCommand;
use App\Models\TwitchToken;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
$this->connector = new Connector();
$this->connect();
+ $this->listenCommands();
}
public function getLogger() {
public function handleWsMessage(Message $message, WebSocket $ws) {
$irc_messages = explode("\r\n", rtrim($message->getPayload(), "\r\n"));
foreach ($irc_messages as $irc_message) {
- $this->logger->debug('received IRC message '.$irc_message);
+ $this->logger->info('received IRC message '.$irc_message);
$this->handleIRCMessage(IRCMessage::fromString($irc_message));
}
}
public function handleWsClose(int $op, string $reason) {
+ $this->ready = false;
$this->logger->info('websocket connection closed: '.$reason.' ['.$op.']');
if (!$this->shutting_down) {
$this->logger->info('reconnecting in 10 seconds');
if ($msg->command == '001') {
// successful login
$this->joinChannels();
+ $this->ready = true;
return;
}
}
public function joinChannels() {
$this->logger->info('joining channels');
- $channels = Channel::where('twitch_chat', '!=', '')->get();
+ $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get();
$names = [];
foreach ($channels as $channel) {
$names[] = $channel->twitch_chat;
}
}
+ private function listenCommands() {
+ $this->getLoop()->addPeriodicTimer(1, function () {
+ if (!$this->ready) return;
+ $command = TwitchBotCommand::where('status', '=', 'pending')->oldest()->first();
+ if ($command) {
+ try {
+ $command->execute($this);
+ } catch (\Exception $e) {
+ }
+ }
+ });
+
+ }
+
public function sendIRCMessage(IRCMessage $msg) {
$irc_message = $msg->encode();
- $this->logger->debug('sending IRC message '.$irc_message);
+ $this->logger->info('sending IRC message '.$irc_message);
$this->ws->send($irc_message);
}
private $connector;
private $ws;
+ private $ready = false;
private $shutting_down = false;
}