]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
bots
[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                         'birrellthesquirrel',
56                         'funtoon',
57                         'nidbot2000',
58                         'nightbot',
59                         'pokemoncommunitygame',
60                         'speedgaming',
61                         'streamelements',
62                         'wizebot',
63                         'zockerstuebchen',
64                 ]);
65         }
66
67         protected function evaluateUser() {
68         }
69
70         protected function evaluateChannel() {
71                 if (empty($this->params)) {
72                         $this->channel()->associate(null);
73                         return;
74                 }
75                 $cname = $this->params[0];
76                 if (substr($cname, 0, 1) != '#') {
77                         $cname = '#'.$cname;
78                 }
79                 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
80                 $this->channel()->associate($channel);
81         }
82
83         protected function detectLanguage() {
84                 $languages = ['de', 'en', 'es', 'fr'];
85                 if (!is_null($this->channel)) {
86                         $languages = array_values($this->channel->languages);
87                         if (!in_array('en', $languages)) {
88                                 $languages[] = 'en';
89                         }
90                 }
91                 $detector = LanguageDetector::detect($this->text_content, $languages);
92                 $scores = $detector->getScores();
93                 $lang = strval($detector->getLanguage());
94                 //var_dump($scores, $lang, $this->text_content);
95                 if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
96                         $this->detected_language = $lang;
97                 }
98         }
99
100         protected function scanForSpam() {
101                 if (substr($this->text_content, 0, 1) == '!') {
102                         return true;
103                 }
104                 if (strpos($this->text_content, '$') !== false) {
105                         return true;
106                 }
107                 if (strpos($this->text_content, '€') !== false) {
108                         return true;
109                 }
110                 if (strpos($this->text_content, '@') !== false) {
111                         return true;
112                 }
113                 if (strpos($this->text_content, '://') !== false) {
114                         return true;
115                 }
116                 if (is_numeric($this->text_content)) {
117                         return true;
118                 }
119                 if (strpos($this->text_content, 'followers') !== false) {
120                         return true;
121                 }
122                 if (strpos($this->text_content, 'promotion') !== false) {
123                         return true;
124                 }
125                 if (strpos($this->text_content, 'viewers') !== false) {
126                         return true;
127                 }
128                 if (strpos($this->text_content, 'view ers') !== false) {
129                         return true;
130                 }
131                 return false;
132         }
133
134         protected $casts = [
135                 'banned' => 'boolean',
136                 'params' => 'array',
137                 'tags' => 'array',
138                 'user_id' => 'string',
139         ];
140
141         protected $fillable = [
142                 'command',
143                 'nick',
144                 'params',
145                 'tags',
146         ];
147
148 }