]> git.localhorst.tv Git - alttp.git/commitdiff
track emote only in chatbot
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 13 Jul 2024 13:35:13 +0000 (15:35 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 13 Jul 2024 13:35:13 +0000 (15:35 +0200)
app/Models/ChatLog.php
app/TwitchBot/IRCMessage.php
app/TwitchBot/TokenizedMessage.php
app/TwitchBot/TwitchBot.php
app/TwitchBot/TwitchChatBot.php
database/migrations/2024_07_13_124353_chat_logs_emote_only.php [new file with mode: 0644]

index d5e8e2874d69d2079b3a63c3ff4ff34487e4a221..d49e7450cbc63f7246c985417c6ab293bf979300 100644 (file)
@@ -95,6 +95,7 @@ class ChatLog extends Model {
                        if ($tokenized->isSpammy()) {
                                $this->banned = true;
                        }
+                       $this->emote_only = $tokenized->isEmoteOnly();
                        $this->classification = $tokenized->classify();
                        return;
                }
index b986847792f6898a4d99f8abb9a82f9455e55318..c256030981a2e1292457fa0ba1f02767ea2d5b19 100644 (file)
@@ -206,6 +206,10 @@ class IRCMessage {
                return $this->command == 'PRIVMSG';
        }
 
+       public function isRoomstate() {
+               return $this->command == 'ROOMSTATE';
+       }
+
        public function isWhisper() {
                return $this->command == 'WHISPER';
        }
@@ -218,6 +222,14 @@ class IRCMessage {
                return $this->isOwner() || (isset($this->tags['mod']) && $this->tags['mod'] == '1');
        }
 
+       public function hasTag($name) {
+               return array_key_exists($name, $this->tags);
+       }
+
+       public function getTag($name) {
+               return $this->tags[$name];
+       }
+
        public function makePong() {
                $msg = new IRCMessage();
                $msg->command = 'PONG';
index f218e7ae9de89e8482bb0a2b6d0b1c92ff8eaab3..e8465fd0bcdf4b0295486c03cefa0f0f81d9dcf5 100644 (file)
@@ -205,6 +205,10 @@ class TokenizedMessage {
                return false;
        }
 
+       public function isEmoteOnly() {
+               return empty($this->emoteless_raw);
+       }
+
        public function isLong() {
                return strlen($this->emoteless_raw) > 20;
        }
index 242ac927021dfd6edace3e196f95f3f36ec87448..23797cd1d3bda0ddd9912c53935a6994c3c2fbb4 100644 (file)
@@ -122,6 +122,10 @@ class TwitchBot {
                        $this->handleWhisper($msg);
                        return;
                }
+               if ($msg->isRoomstate()) {
+                       $this->handleRoomstate($msg);
+                       return;
+               }
                if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
                        $this->logger->notice('login failed, refreshing access token');
                        $this->token->refresh();
@@ -155,6 +159,9 @@ class TwitchBot {
        public function handlePrivMsg(IRCMessage $msg) {
        }
 
+       public function handleRoomstate(IRCMessage $msg) {
+       }
+
        public function handleUserState(IRCMessage $msg) {
                if (isset($msg->tags['user-id'])) {
                        $this->user_id = $msg->tags['user-id'];
index 7151b2b682d092dc1081569ee567f9faf676080b..7b505ed8c48341d61d9733934c56b33d3ac3c0ea 100644 (file)
@@ -46,6 +46,17 @@ class TwitchChatBot extends TwitchBot {
                $this->tagChannelRead($channel, $msg);
        }
 
+       public function handleRoomstate(IRCMessage $msg) {
+               $channel = $this->getMessageChannel($msg);
+               if ($channel && $msg->hasTag('emote-only')) {
+                       $oldEmote = $this->getNote($channel, 'emote_only');
+                       $newEmote = !!intval($msg->getTag('emote-only'));
+                       if ($oldEmote != $newEmote) {
+                               $this->setNote($channel, 'emote_only', $newEmote);
+                       }
+               }
+       }
+
        public function handleWhisper(IRCMessage $msg) {
                $text = $this->chatlib->generate($msg->getText());
                $this->sendWhisper($msg->tags['user-id'], $text);
@@ -109,6 +120,7 @@ class TwitchChatBot extends TwitchBot {
        private function getNotes(Channel $channel) {
                if (!isset($this->notes[$channel->id])) {
                        $this->notes[$channel->id] = [
+                               'emote_only' => false,
                                'last_read' => 0,
                                'last_special' => [],
                                'last_write' => time(),
@@ -203,9 +215,12 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function randomChat(Channel $channel) {
-               return $channel->queryChatlog()
-                       ->whereNotIn('classification', ['gg', 'gl', 'number', 'o7'])
-                       ->first();
+               $query = $channel->queryChatlog()
+                       ->whereNotIn('classification', ['gg', 'gl', 'number', 'o7']);
+               if ($this->getNote($channel, 'emote_only', false)) {
+                       $query->where('emote_only', '=', true);
+               }
+               return $query->first();
        }
 
        private function randomContextualNumber(Channel $channel) {
@@ -258,7 +273,11 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function randomMsg(Channel $channel) {
-               return $channel->queryChatlog()->first();
+               $query = $channel->queryChatlog();
+               if ($this->getNote($channel, 'emote_only', false)) {
+                       $query->where('emote_only', '=', true);
+               }
+               return $query->first();
        }
 
        private function performAdlib(Channel $channel) {
diff --git a/database/migrations/2024_07_13_124353_chat_logs_emote_only.php b/database/migrations/2024_07_13_124353_chat_logs_emote_only.php
new file mode 100644 (file)
index 0000000..0f3b7f9
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        */
+       public function up(): void
+       {
+               Schema::table('chat_logs', function (Blueprint $table) {
+                       $table->boolean('emote_only')->default(false);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        */
+       public function down(): void
+       {
+               Schema::table('chat_logs', function (Blueprint $table) {
+                       $table->dropColumn('emote_only');
+               });
+       }
+};