]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TokenizedMessage.php
better quota system for contextual messages
[alttp.git] / app / TwitchBot / TokenizedMessage.php
diff --git a/app/TwitchBot/TokenizedMessage.php b/app/TwitchBot/TokenizedMessage.php
new file mode 100644 (file)
index 0000000..259cfc6
--- /dev/null
@@ -0,0 +1,133 @@
+<?php
+
+namespace App\TwitchBot;
+
+use App\Models\ChatLog;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Str;
+
+class TokenizedMessage {
+
+       public function __construct($text, $tags = []) {
+               $this->text = $text;
+               $this->tags = $tags;
+               $this->raw = strtolower(preg_replace('/[^\w]/', '', $text));
+               $this->tokens = preg_split('/\s+/', strtolower(trim($text)));
+
+               $this->emoteless = $this->text;
+               if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
+                       $emotes = explode('/', $this->tags['emotes']);
+                       foreach ($emotes as $emote) {
+                               $set = explode(':', $emote);
+                               $positions = explode(',', $set[1]);
+                               foreach ($positions as $position) {
+                                       $coords = explode('-', $position);
+                                       $this->emotes[] = substr($this->text, $coords[0], $coords[1] - $coords[0] + 1);
+                                       for ($i = intval($coords[0]); $i <= intval($coords[1]); ++$i) {
+                                               $this->emoteless[$i] = ' ';
+                                       }
+                               }
+                       }
+                       $this->emoteless = trim(preg_replace('/\s+/', ' ', $this->emoteless));
+               }
+               $this->emoteless_tokens = preg_split('/\s+/', strtolower($this->emoteless));
+       }
+
+       public static function fromIRC(IRCMessage $msg) {
+               return new self($msg->getText(), $msg->tags);
+       }
+
+       public static function fromLog(ChatLog $log) {
+               return new self($log->params[1], $log->tags);
+       }
+
+       public static function fromString($text, $tags = []) {
+               return new self($text, $tags);
+       }
+
+
+       public function getNumericValue() {
+               return intval($this->raw);
+       }
+
+       public function isSpammy() {
+               if (substr($this->raw, 0, 1) == '!') {
+                       return true;
+               }
+               if (strpos($this->raw, '$') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, '€') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, '@') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, '://') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, 'followers') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, 'horstie') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, 'promotion') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, 'viewers') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, 'view ers') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, 'vielen dank für den raid') !== false) {
+                       return true;
+               }
+               if (strpos($this->raw, 'willkommen auf starbase 47') !== false) {
+                       return true;
+               }
+               return false;
+       }
+
+
+       public function classify() {
+               if (is_null($this->classification)) {
+                       if (empty($this->raw)) {
+                               $this->classification = 'unclassified';
+                       } else if (is_numeric($this->raw)) {
+                               $this->classification = 'number';
+                       } else if (Str::startsWith($this->raw, 'gg') || Str::endsWith($this->raw, 'gg')) {
+                               $this->classification = 'gg';
+                       } else if (Str::contains($this->raw, ['glgl', 'glhf', 'hfgl'])) {
+                               $this->classification = 'gl';
+                       } else if (Str::contains($this->raw, ['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul', 'xd'])) {
+                               $this->classification = 'lol';
+                       } else if (Str::startsWith($this->raw, ['ahoi', 'hallo', 'hello', 'hi ', 'huhu']) || Str::endsWith($this->raw, ['hi', 'wave'])) {
+                               $this->classification = 'hi';
+                       } else if (Str::contains($this->raw, ['pog', 'wow'])) {
+                               $this->classification = 'pog';
+                       } else if (Str::contains($this->raw, ['hype'])) {
+                               $this->classification = 'hype';
+                       } else if (Str::startsWith($this->raw, 'o7') || Str::endsWith($this->raw, 'o7') || Str::contains($this->raw, 'salut')) {
+                               $this->classification = 'o7';
+                       } else {
+                               $this->classification = 'unclassified';
+                       }
+               }
+               return $this->classification;
+       }
+
+
+       private $text;
+       private $tags;
+       private $raw;
+       private $tokens;
+
+       private $emotes = [];
+       private $emoteless = '';
+       private $emoteless_tokens = [];
+
+       private $classification = null;
+
+}