]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
add speedgaming as known bot
[alttp.git] / app / Models / ChatLog.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7 use LanguageDetector\LanguageDetector;
8
9 class ChatLog extends Model {
10
11         use HasFactory;
12
13         public function channel() {
14                 return $this->belongsTo(Channel::class);
15         }
16
17         public function user() {
18                 return $this->belongsTo(User::class);
19         }
20
21         public function evaluate() {
22                 $this->evaluateUser();
23                 $this->evaluateChannel();
24
25                 if (is_null($this->nick)) {
26                         $this->type = 'system';
27                         return;
28                 }
29                 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
30                         $this->type = 'self';
31                         return;
32                 }
33
34                 if ($this->command == 'PRIVMSG') {
35                         if ($this->isKnownBot()) {
36                                 $this->type = 'bot';
37                         } else if (substr($this->params[0], 0, 1) == '#') {
38                                 $this->type = 'chat';
39                         } else {
40                                 $this->type = 'dm';
41                         }
42                         $this->text_content = $this->params[1];
43                         $this->detectLanguage();
44                         if ($this->scanForSpam()) {
45                                 $this->banned = true;
46                         }
47                         return;
48                 }
49
50                 throw new \Exception('unidentified message');
51         }
52
53         public function isKnownBot() {
54                 return in_array(strtolower($this->nick), [
55                         'funtoon',
56                         'nightbot',
57                         'pokemoncommunitygame',
58                         'speedgaming',
59                         'streamelements',
60                         'wizebot',
61                         'zockerstuebchen',
62                 ]);
63         }
64
65         protected function evaluateUser() {
66         }
67
68         protected function evaluateChannel() {
69                 if (empty($this->params)) {
70                         $this->channel()->associate(null);
71                         return;
72                 }
73                 $cname = $this->params[0];
74                 if (substr($cname, 0, 1) != '#') {
75                         $cname = '#'.$cname;
76                 }
77                 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
78                 $this->channel()->associate($channel);
79         }
80
81         protected function detectLanguage() {
82                 $languages = ['de', 'en', 'es', 'fr'];
83                 if (!is_null($this->channel)) {
84                         $languages = array_values($this->channel->languages);
85                         if (!in_array('en', $languages)) {
86                                 $languages[] = 'en';
87                         }
88                 }
89                 $detector = LanguageDetector::detect($this->text_content, $languages);
90                 $scores = $detector->getScores();
91                 $lang = strval($detector->getLanguage());
92                 //var_dump($scores, $lang, $this->text_content);
93                 if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
94                         $this->detected_language = $lang;
95                 }
96         }
97
98         protected function scanForSpam() {
99                 if (substr($this->text_content, 0, 1) == '!') {
100                         return true;
101                 }
102                 if (strpos($this->text_content, '$') !== false) {
103                         return true;
104                 }
105                 if (strpos($this->text_content, '€') !== false) {
106                         return true;
107                 }
108                 if (strpos($this->text_content, '@') !== false) {
109                         return true;
110                 }
111                 if (strpos($this->text_content, '://') !== false) {
112                         return true;
113                 }
114                 if (is_numeric($this->text_content)) {
115                         return true;
116                 }
117                 if (strpos($this->text_content, 'followers') !== false) {
118                         return true;
119                 }
120                 if (strpos($this->text_content, 'promotion') !== false) {
121                         return true;
122                 }
123                 if (strpos($this->text_content, 'viewers') !== false) {
124                         return true;
125                 }
126                 if (strpos($this->text_content, 'view ers') !== false) {
127                         return true;
128                 }
129                 return false;
130         }
131
132         protected $casts = [
133                 'banned' => 'boolean',
134                 'params' => 'array',
135                 'tags' => 'array',
136                 'user_id' => 'string',
137         ];
138
139         protected $fillable = [
140                 'command',
141                 'nick',
142                 'params',
143                 'tags',
144         ];
145
146 }