From 156217eb69980adbf8bca982179e47c2f32926b2 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 13 Jul 2024 15:35:13 +0200 Subject: [PATCH] track emote only in chatbot --- app/Models/ChatLog.php | 1 + app/TwitchBot/IRCMessage.php | 12 ++++++++ app/TwitchBot/TokenizedMessage.php | 4 +++ app/TwitchBot/TwitchBot.php | 7 +++++ app/TwitchBot/TwitchChatBot.php | 27 +++++++++++++++--- ...2024_07_13_124353_chat_logs_emote_only.php | 28 +++++++++++++++++++ 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2024_07_13_124353_chat_logs_emote_only.php diff --git a/app/Models/ChatLog.php b/app/Models/ChatLog.php index d5e8e28..d49e745 100644 --- a/app/Models/ChatLog.php +++ b/app/Models/ChatLog.php @@ -95,6 +95,7 @@ class ChatLog extends Model { if ($tokenized->isSpammy()) { $this->banned = true; } + $this->emote_only = $tokenized->isEmoteOnly(); $this->classification = $tokenized->classify(); return; } diff --git a/app/TwitchBot/IRCMessage.php b/app/TwitchBot/IRCMessage.php index b986847..c256030 100644 --- a/app/TwitchBot/IRCMessage.php +++ b/app/TwitchBot/IRCMessage.php @@ -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'; diff --git a/app/TwitchBot/TokenizedMessage.php b/app/TwitchBot/TokenizedMessage.php index f218e7a..e8465fd 100644 --- a/app/TwitchBot/TokenizedMessage.php +++ b/app/TwitchBot/TokenizedMessage.php @@ -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; } diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php index 242ac92..23797cd 100644 --- a/app/TwitchBot/TwitchBot.php +++ b/app/TwitchBot/TwitchBot.php @@ -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']; diff --git a/app/TwitchBot/TwitchChatBot.php b/app/TwitchBot/TwitchChatBot.php index 7151b2b..7b505ed 100644 --- a/app/TwitchBot/TwitchChatBot.php +++ b/app/TwitchBot/TwitchChatBot.php @@ -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 index 0000000..0f3b7f9 --- /dev/null +++ b/database/migrations/2024_07_13_124353_chat_logs_emote_only.php @@ -0,0 +1,28 @@ +boolean('emote_only')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('chat_logs', function (Blueprint $table) { + $table->dropColumn('emote_only'); + }); + } +}; -- 2.39.2