From 8fd3830c342ed7734280407b03cc7ced6e116b10 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 1 Jan 2024 15:18:46 +0100 Subject: [PATCH] log irc messages --- app/Models/ChatLog.php | 24 +++++++++++++ app/TwitchBot/IRCMessage.php | 11 ++++++ app/TwitchBot/TwitchBot.php | 11 +++--- ...24_01_01_133735_create_chat_logs_table.php | 35 +++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 app/Models/ChatLog.php create mode 100644 database/migrations/2024_01_01_133735_create_chat_logs_table.php diff --git a/app/Models/ChatLog.php b/app/Models/ChatLog.php new file mode 100644 index 0000000..9e50add --- /dev/null +++ b/app/Models/ChatLog.php @@ -0,0 +1,24 @@ + 'array', + 'tags' => 'array', + ]; + + protected $fillable = [ + 'command', + 'nick', + 'params', + 'tags', + ]; + +} diff --git a/app/TwitchBot/IRCMessage.php b/app/TwitchBot/IRCMessage.php index d127f9f..1e64bd9 100644 --- a/app/TwitchBot/IRCMessage.php +++ b/app/TwitchBot/IRCMessage.php @@ -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'; diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php index a45de87..c8d77a3 100644 --- a/app/TwitchBot/TwitchBot.php +++ b/app/TwitchBot/TwitchBot.php @@ -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 index 0000000..7f11c6c --- /dev/null +++ b/database/migrations/2024_01_01_133735_create_chat_logs_table.php @@ -0,0 +1,35 @@ +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'); + } +}; -- 2.39.2