]> git.localhorst.tv Git - alttp.git/commitdiff
try to respond more appropriately
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 12 Apr 2024 16:30:55 +0000 (18:30 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 12 Apr 2024 16:30:55 +0000 (18:30 +0200)
app/Models/Channel.php
app/TwitchBot/TokenizedMessage.php
app/TwitchBot/TwitchChatBot.php
resources/js/components/twitch-bot/Controls.js
resources/js/i18n/de.js
resources/js/i18n/en.js
tests/Unit/TwitchBot/TokenizedMessageTest.php

index 8af6a7d43e201e191651a4ab9b28fc7c11642ec5..8045a4194fa529561ba6a091c0aa7770beb18dce 100644 (file)
@@ -32,6 +32,11 @@ class Channel extends Model {
        }
 
        public function randomOfClass($class) {
+               if (is_array($class)) {
+                       return $this->queryChatlog()
+                               ->whereIn('classification', $class)
+                               ->first();
+               }
                return $this->queryChatlog()
                        ->where('classification', '=', $class)
                        ->first();
index 61ca5c8dee40d346e575342e7494512ac055a562..b03e2192eca4948273047cad7c6ea59c5b2a83c1 100644 (file)
@@ -215,7 +215,7 @@ class TokenizedMessage {
                if ($this->containsRaw('horstie')) {
                        return true;
                }
-               if ($this->containsRaw(['vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
+               if ($this->containsRaw(['folgtjetzt', 'vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
                        return true;
                }
                return false;
@@ -228,20 +228,18 @@ class TokenizedMessage {
                                $this->classification = 'unclassified';
                        } else if ($this->startsWith('!')) {
                                $this->classification = 'cmd';
-                       } else if (is_numeric($this->raw)) {
-                               $this->classification = 'number';
                        } else if ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg'])) {
                                $this->classification = 'gg';
                        } else if ($this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl'])) {
                                $this->classification = 'gl';
+                       } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
+                               $this->classification = 'thx';
                        } else if ($this->startsWithRaw(['ahoi', 'hallo', 'hello', 'hey', 'huhu', 'moin']) || $this->hasEmoteThatEndsWith(['hello', 'heyguys', 'hi', 'vohiyo', 'wave']) || $this->hasToken(['hi', 'hey', 'yo']) || $this->containsRaw(['gutenmorgen', 'gutenabend'])) {
                                $this->classification = 'hi';
                        } else if ($this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
                                $this->classification = 'pog';
                        } else if ($this->containsRaw(['hype']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
                                $this->classification = 'hype';
-                       } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty'])) {
-                               $this->classification = 'thx';
                        } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
                                $this->classification = 'love';
                        } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
@@ -256,10 +254,18 @@ class TokenizedMessage {
                                $this->classification = 'sweat';
                        } else if ($this->endsWithEmoteless('?')) {
                                $this->classification = 'question';
+                       } else if ($this->hasToken(['ja', 'jo', 'yep', 'yes']) || $this->containsRaw('nodders') || $this->hasEmoteThatEndsWith(['nod', 'nodders', 'yea'])) {
+                               $this->classification = 'yes';
+                       } else if ($this->hasToken(['nah', 'nee', 'nein', 'no']) || $this->containsRaw('nopers') || $this->hasEmoteThatEndsWith(['nay', 'nope', 'nopers'])) {
+                               $this->classification = 'no';
+                       } else if ($this->hasEmoteThatContains(['kappa', 'keepo'])) {
+                               $this->classification = 'kappa';
                        } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
                                $this->classification = 'o7';
                        } else if ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd'])) {
                                $this->classification = 'lol';
+                       } else if (is_numeric($this->raw)) {
+                               $this->classification = 'number';
                        } else {
                                $this->classification = 'unclassified';
                        }
index ebf2b94af14d2da6175f578a55f759c1cd9fd515..a403b0b57b716ae488c1056e8f3e6d8be53573f8 100644 (file)
@@ -94,6 +94,7 @@ class TwitchChatBot extends TwitchBot {
                                'last_special' => [],
                                'last_write' => time(),
                                'latest_msgs' => [],
+                               'queued_special' => false,
                                'read_since_last_write' => 0,
                                'wait_msgs' => $this->randomWaitMsgs($channel),
                                'wait_time' => $this->randomWaitTime($channel),
@@ -132,6 +133,20 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function contextualMsg(Channel $channel) {
+               if ($this->hasQueuedSpecial($channel)) {
+                       $classification = $this->getQueuedSpecial($channel);
+                       if (is_string($classification)) {
+                               $this->tagChannelSpecialSent($channel, $classification);
+                       }
+                       $this->clearQueuedSpecial($channel);
+                       if ($classification == 'number') {
+                               return $this->randomContextualNumber($channel);
+                       }
+                       if ($classification == 'lol') {
+                               return $this->randomLaughter($channel);
+                       }
+                       return $channel->randomOfClass($classification);
+               }
                $last = $this->getLastSpecialSent($channel);
                $classifications = $this->collectClassifications($channel);
                $count_quotas = [
@@ -176,7 +191,7 @@ class TwitchChatBot extends TwitchBot {
 
        private function randomChat(Channel $channel) {
                return $channel->queryChatlog()
-                       ->whereNotIn('classification', ['gg', 'gl', 'o7'])
+                       ->whereNotIn('classification', ['gg', 'gl', 'number', 'o7'])
                        ->first();
        }
 
@@ -245,6 +260,25 @@ class TwitchChatBot extends TwitchBot {
                return random_int($min, $max);
        }
 
+       private function queueSpecial(Channel $channel, $classification) {
+               $this->getNotes($channel);
+               $this->notes[$channel->id]['queued_special'] = $classification;
+       }
+
+       private function hasQueuedSpecial(Channel $channel) {
+               return !!$this->getQueuedSpecial($channel);
+       }
+
+       private function getQueuedSpecial(Channel $channel) {
+               $notes = $this->getNotes($channel);
+               return $notes['queued_special'];
+       }
+
+       private function clearQueuedSpecial(Channel $channel) {
+               $this->getNotes($channel);
+               $this->notes[$channel->id]['queued_special'] = false;
+       }
+
        private function tagChannelRead(Channel $channel, IRCMessage $msg) {
                $this->getNotes($channel);
                $this->notes[$channel->id]['last_read'] = time();
@@ -260,6 +294,10 @@ class TwitchChatBot extends TwitchBot {
                if ($this->isDirectedAtMe($msg->getText()) && $this->shouldRespond($channel)) {
                        $this->notes[$channel->id]['wait_msgs'] = 0;
                        $this->notes[$channel->id]['wait_time'] = 0;
+                       $response = $this->getResponseTo($tokenized);
+                       if ($response) {
+                               $this->queueSpecial($channel, $response);
+                       }
                }
        }
 
@@ -316,6 +354,28 @@ class TwitchChatBot extends TwitchBot {
                return false;
        }
 
+       private function getResponseTo(TokenizedMessage $msg) {
+               switch ($msg->classify()) {
+                       case 'gg':
+                               return ['love', 'eyes', 'thx', 'pog', 'kappa'];
+                       case 'gl':
+                               return ['love', 'eyes', 'thx'];
+                       case 'hi':
+                               return ['hi', 'love', 'eyes', 'hype', 'pog'];
+                       case 'kappa':
+                               return ['kappa', 'lol', 'eyes'];
+                       case 'love':
+                               return ['hi', 'love', 'eyes', 'thx'];
+                       case 'question':
+                               return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number'];
+                       case 'rage':
+                               return ['kappa', 'lol', 'rage'];
+                       case 'wtf':
+                               return ['kappa', 'lol', 'rage'];
+               }
+               return false;
+       }
+
        private $channels;
        private $notes = [];
 
index f3f551cb9ca4e1fd8ca4f1ffe320ef162bb9649d..f64ee62712a658da228c4ebacbc895d55931bf35 100644 (file)
@@ -21,12 +21,15 @@ const CHAT_CATEGORIES = [
        'eyes',
        'love',
        'lol',
+       'yes',
+       'no',
        'rage',
        'sad',
        'sweat',
        'wtf',
        'pog',
        'hype',
+       'kappa',
        'o7',
        'question',
        'thx',
index 2e13f7c561b7fc030bbc26847e8550ed46556c78..14f18fa30ba015370b9bb0112e1da23826331541 100644 (file)
@@ -692,8 +692,10 @@ export default {
                                gl: 'Good Luck',
                                hi: 'Begrüßung',
                                hype: 'Hype',
+                               kappa: 'Kappa',
                                lol: 'Gelächter',
                                love: 'Love',
+                               no: 'Nein',
                                o7: 'Salutieren',
                                pog: 'Pog',
                                question: 'Frage',
@@ -703,6 +705,7 @@ export default {
                                thx: 'Danke',
                                unclassified: 'Generisch',
                                wtf: 'WTF',
+                               yes: 'Ja',
                        },
                        chatError: 'Fehler beim Senden',
                        chatSettings: 'Chat Bot Einstellungen',
index 8770273af32ec2dd1beb8cdc9c8deb6d9e2050da..f6965043a84a355e9a380c04307284694e97577b 100644 (file)
@@ -692,8 +692,10 @@ export default {
                                gl: 'Good Luck',
                                hi: 'Greeting',
                                hype: 'Hype',
+                               kappa: 'Kappa',
                                lol: 'Laughter',
                                love: 'Love',
+                               no: 'No',
                                o7: 'Salute',
                                pog: 'Pog',
                                question: 'Question',
@@ -703,6 +705,7 @@ export default {
                                thx: 'Thanks',
                                unclassified: 'Generic',
                                wtf: 'WTF',
+                               yes: 'Yes',
                        },
                        chatError: 'Error sending message',
                        chatSettings: 'Chat Bot Settings',
index be38909a07d06a9d111fd33b6a6297102d3a4c65..b9577466a691addd9c1e6ff6900aafe8b677b55a 100644 (file)
@@ -35,6 +35,11 @@ class TokenizedMessageTest extends TestCase {
 
                $this->assertEquals('hype', TokenizedMessage::fromString('122 Hype!')->classify());
 
+               $this->assertEquals('kappa', TokenizedMessage::fromString('Kappa', ['emotes' => 'blah:0-4'])->classify());
+               $this->assertEquals('kappa', TokenizedMessage::fromString('KappaClaus', ['emotes' => 'blah:0-9'])->classify());
+               $this->assertEquals('kappa', TokenizedMessage::fromString('Keepo', ['emotes' => 'blah:0-4'])->classify());
+               $this->assertNotEquals('kappa', TokenizedMessage::fromString('I keep order')->classify());
+
                $this->assertEquals('number', TokenizedMessage::fromString('13')->classify());
                $this->assertEquals('number', TokenizedMessage::fromString('22')->classify());