]> git.localhorst.tv Git - alttp.git/commitdiff
log irc messages
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 1 Jan 2024 14:18:46 +0000 (15:18 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 1 Jan 2024 14:18:46 +0000 (15:18 +0100)
app/Models/ChatLog.php [new file with mode: 0644]
app/TwitchBot/IRCMessage.php
app/TwitchBot/TwitchBot.php
database/migrations/2024_01_01_133735_create_chat_logs_table.php [new file with mode: 0644]

diff --git a/app/Models/ChatLog.php b/app/Models/ChatLog.php
new file mode 100644 (file)
index 0000000..9e50add
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class ChatLog extends Model {
+
+       use HasFactory;
+
+       protected $casts = [
+               'params' => 'array',
+               'tags' => 'array',
+       ];
+
+       protected $fillable = [
+               'command',
+               'nick',
+               'params',
+               'tags',
+       ];
+
+}
index d127f9f5d57425e850512640d6ae470c2f0c1bc3..1e64bd9b6de7c12c0674ce7966e06adbd914f0b0 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace App\TwitchBot;
 
+use App\Models\ChatLog;
+
 class IRCMessage {
 
        public $command = null;
@@ -125,6 +127,15 @@ class IRCMessage {
                return $str;
        }
 
+       public function log() {
+               ChatLog::create([
+                       'command' => $this->command,
+                       'nick' => $this->nick,
+                       'params' => $this->params,
+                       'tags' => $this->tags,
+               ]);
+       }
+
        public static function join($channels) {
                $msg = new IRCMessage();
                $msg->command = 'JOIN';
index a45de87fcce69a36b32e06cdb2fc567b222144a0..c8d77a36d6f656007a0b3b50c009689b55f3adbe 100644 (file)
@@ -99,10 +99,6 @@ class TwitchBot {
 
        public function handleIRCMessage(IRCMessage $msg) {
                $this->last_contact = time();
-               if ($msg->isPrivMsg()) {
-                       $this->handlePrivMsg($msg);
-                       return;
-               }
                if ($msg->isPing()) {
                        $this->sendIRCMessage($msg->makePong());
                        return;
@@ -110,6 +106,11 @@ class TwitchBot {
                if ($msg->isPong()) {
                        return;
                }
+               $msg->log();
+               if ($msg->isPrivMsg()) {
+                       $this->handlePrivMsg($msg);
+                       return;
+               }
                if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
                        $this->logger->notice('login failed, refreshing access token');
                        $this->token->refresh();
@@ -126,7 +127,7 @@ class TwitchBot {
 
        public function handlePrivMsg(IRCMessage $msg) {
                $target = $msg->getPrivMsgTarget();
-               if ($target[0] != '#') return;
+               if ($target[0] != '#') return; // direct message
                $text = $msg->getText();
                if ($text[0] != '!') return;
                $channel = Channel::firstWhere('twitch_chat', '=', $target);
diff --git a/database/migrations/2024_01_01_133735_create_chat_logs_table.php b/database/migrations/2024_01_01_133735_create_chat_logs_table.php
new file mode 100644 (file)
index 0000000..7f11c6c
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        *
+        * @return void
+        */
+       public function up()
+       {
+               Schema::create('chat_logs', function (Blueprint $table) {
+                       $table->id();
+                       $table->timestamps();
+                       $table->string('command');
+                       $table->string('nick')->nullable();
+                       $table->text('params');
+                       $table->text('tags');
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        *
+        * @return void
+        */
+       public function down()
+       {
+               Schema::dropIfExists('chat_logs');
+       }
+};