]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TokenizedMessage.php
0e241d5cd804c490e72f761d01cba7ae70357e5d
[alttp.git] / app / TwitchBot / TokenizedMessage.php
1 <?php
2
3 namespace App\TwitchBot;
4
5 use App\Models\ChatLog;
6 use Illuminate\Support\Arr;
7 use Illuminate\Support\Str;
8
9 class TokenizedMessage {
10
11         public function __construct($text, $tags = []) {
12                 $this->text = $text;
13                 $this->tags = $tags;
14                 $this->raw = strtolower(preg_replace('/[^\w]/u', '', $text));
15                 $this->tokens = preg_split('/\s+/', strtolower(trim($text)));
16
17                 $this->emoteless = $this->text;
18                 if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
19                         $emotes = explode('/', $this->tags['emotes']);
20                         foreach ($emotes as $emote) {
21                                 $set = explode(':', $emote);
22                                 $positions = explode(',', $set[1]);
23                                 foreach ($positions as $position) {
24                                         $coords = explode('-', $position);
25                                         $this->emotes[] = preg_replace('/\d+$/', '', strtolower(substr($this->text, $coords[0], $coords[1] - $coords[0] + 1)));
26                                         for ($i = intval($coords[0]); $i <= intval($coords[1]); ++$i) {
27                                                 $this->emoteless[$i] = ' ';
28                                         }
29                                 }
30                         }
31                         $this->emoteless = trim(preg_replace('/\s+/', ' ', $this->emoteless));
32                 }
33                 $this->emoteless_raw = strtolower(preg_replace('/[^\w]/', '', $this->emoteless));
34                 $this->emoteless_tokens = preg_split('/\s+/', strtolower($this->emoteless));
35         }
36
37         public static function fromIRC(IRCMessage $msg) {
38                 return new self($msg->getText(), $msg->tags);
39         }
40
41         public static function fromLog(ChatLog $log) {
42                 return new self($log->params[1], $log->tags);
43         }
44
45         public static function fromString($text, $tags = []) {
46                 return new self($text, $tags);
47         }
48
49
50         public function contains($text) {
51                 return Str::contains($this->text, $text);
52         }
53
54         public function containsEmoteless($text) {
55                 return Str::contains($this->emoteless, $text);
56         }
57
58         public function containsRaw($text) {
59                 return Str::contains($this->raw, $text);
60         }
61
62         public function endsWith($text) {
63                 return Str::endsWith($this->text, $text);
64         }
65
66         public function endsWithEmoteless($text) {
67                 return Str::endsWith($this->emoteless, $text);
68         }
69
70         public function endsWithRaw($text) {
71                 return Str::endsWith($this->raw, $text);
72         }
73
74         public function getNumericValue() {
75                 return intval($this->text);
76         }
77
78         public function hasEmote($text) {
79                 if (is_array($text)) {
80                         foreach ($text as $token) {
81                                 if (in_array($token, $this->emotes)) {
82                                         return true;
83                                 }
84                         }
85                         return false;
86                 }
87                 return in_array($text, $this->emotes);
88         }
89
90         public function hasEmoteThatContains($text) {
91                 foreach ($this->emotes as $emote) {
92                         if (Str::contains($emote, $text)) {
93                                 return true;
94                         }
95                 }
96                 return false;
97         }
98
99         public function hasEmoteThatEndsWith($text) {
100                 foreach ($this->emotes as $emote) {
101                         if (Str::endsWith($emote, $text)) {
102                                 return true;
103                         }
104                 }
105                 return false;
106         }
107
108         public function hasEmoteThatStartsOrEndsWith($text) {
109                 foreach ($this->emotes as $emote) {
110                         if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
111                                 return true;
112                         }
113                 }
114                 return false;
115         }
116
117         public function hasEmoteThatStartsWith($text) {
118                 foreach ($this->emotes as $emote) {
119                         if (Str::startsWith($emote, $text)) {
120                                 return true;
121                         }
122                 }
123                 return false;
124         }
125
126         public function hasToken($text) {
127                 if (is_array($text)) {
128                         foreach ($text as $token) {
129                                 if (in_array($token, $this->tokens)) {
130                                         return true;
131                                 }
132                         }
133                         return false;
134                 }
135                 return in_array($text, $this->tokens);
136         }
137
138         public function hasTokenThatContains($text) {
139                 foreach ($this->tokens as $token) {
140                         if (Str::contains($token, $text)) {
141                                 return true;
142                         }
143                 }
144                 return false;
145         }
146
147         public function hasTokenThatEndsWith($text) {
148                 foreach ($this->tokens as $token) {
149                         if (Str::endsWith($token, $text)) {
150                                 return true;
151                         }
152                 }
153                 return false;
154         }
155
156         public function hasTokenThatStartsOrEndsWith($text) {
157                 foreach ($this->tokens as $token) {
158                         if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
159                                 return true;
160                         }
161                 }
162                 return false;
163         }
164
165         public function hasTokenThatStartsWith($text) {
166                 foreach ($this->tokens as $token) {
167                         if (Str::startsWith($token, $text)) {
168                                 return true;
169                         }
170                 }
171                 return false;
172         }
173
174         public function startsOrEndsWith($text) {
175                 return $this->startsWith($text) || $this->endsWith($text);
176         }
177
178         public function startsOrEndsWithRaw($text) {
179                 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
180         }
181
182         public function startsWith($text) {
183                 return Str::startsWith($this->text, $text);
184         }
185
186         public function startsWithEmoteless($text) {
187                 return Str::startsWith($this->emoteless, $text);
188         }
189
190         public function startsWithRaw($text) {
191                 return Str::startsWith($this->raw, $text);
192         }
193
194
195         public function isSpammy() {
196                 if ($this->startsWith('!')) {
197                         return true;
198                 }
199                 if ($this->contains(['€', '$', '@', '://'])) {
200                         return true;
201                 }
202                 if ($this->containsRaw(['followers', 'promotion', 'viewers'])) {
203                         return true;
204                 }
205                 if ($this->containsRaw('horstie')) {
206                         return true;
207                 }
208                 if ($this->containsRaw(['vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
209                         return true;
210                 }
211                 return false;
212         }
213
214
215         public function classify() {
216                 if (is_null($this->classification)) {
217                         if (empty($this->raw)) {
218                                 $this->classification = 'unclassified';
219                         } else if ($this->startsWith('!')) {
220                                 $this->classification = 'cmd';
221                         } else if (is_numeric($this->raw)) {
222                                 $this->classification = 'number';
223                         } else if ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg'])) {
224                                 $this->classification = 'gg';
225                         } else if ($this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl'])) {
226                                 $this->classification = 'gl';
227                         } else if ($this->startsWithRaw(['ahoi', 'hallo', 'hello', 'hey', 'huhu', 'moin']) || $this->hasEmoteThatEndsWith(['hello', 'heyguys', 'hi', 'wave']) || $this->hasToken(['hi', 'hey']) || $this->containsRaw(['gutenmorgen', 'gutenabend'])) {
228                                 $this->classification = 'hi';
229                         } else if ($this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
230                                 $this->classification = 'pog';
231                         } else if ($this->containsRaw(['hype'])) {
232                                 $this->classification = 'hype';
233                         } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty'])) {
234                                 $this->classification = 'thx';
235                         } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['herz', 'hug', 'love'])) {
236                                 $this->classification = 'love';
237                         } else if ($this->hasTokenThatStartsWith(['wat', 'wtf']) || $this->hasEmoteThatStartsWith(['wat', 'wtf'])) {
238                                 $this->classification = 'wtf';
239                         } else if ($this->endsWithEmoteless('?')) {
240                                 $this->classification = 'question';
241                         } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
242                                 $this->classification = 'o7';
243                         } else if ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith([':d', 'xd'])) {
244                                 $this->classification = 'lol';
245                         } else {
246                                 $this->classification = 'unclassified';
247                         }
248                 }
249                 return $this->classification;
250         }
251
252
253         private $text;
254         private $tags;
255         private $raw;
256         private $tokens;
257
258         private $emotes = [];
259         private $emoteless = '';
260         private $emoteless_raw = '';
261         private $emoteless_tokens = [];
262
263         private $classification = null;
264
265 }