5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7 use LanguageDetector\LanguageDetector;
9 class ChatLog extends Model {
13 public function channel() {
14 return $this->belongsTo(Channel::class);
17 public function user() {
18 return $this->belongsTo(User::class);
21 public function evaluate() {
22 $this->evaluateUser();
23 $this->evaluateChannel();
25 if (is_null($this->nick)) {
26 $this->type = 'system';
29 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
34 if ($this->command == 'PRIVMSG') {
35 if ($this->isKnownBot()) {
37 } else if (substr($this->params[0], 0, 1) == '#') {
42 $this->text_content = $this->params[1];
43 $this->detectLanguage();
44 if ($this->scanForSpam()) {
50 throw new \Exception('unidentified message');
53 public function isKnownBot() {
54 return in_array(strtolower($this->nick), [
59 'pokemoncommunitygame',
67 protected function evaluateUser() {
70 protected function evaluateChannel() {
71 if (empty($this->params)) {
72 $this->channel()->associate(null);
75 $cname = $this->params[0];
76 if (substr($cname, 0, 1) != '#') {
79 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
80 $this->channel()->associate($channel);
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)) {
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;
100 protected function scanForSpam() {
101 if (substr($this->text_content, 0, 1) == '!') {
104 if (strpos($this->text_content, '$') !== false) {
107 if (strpos($this->text_content, '€') !== false) {
110 if (strpos($this->text_content, '@') !== false) {
113 if (strpos($this->text_content, '://') !== false) {
116 if (is_numeric($this->text_content)) {
119 if (strpos($this->text_content, 'followers') !== false) {
122 if (strpos($this->text_content, 'promotion') !== false) {
125 if (strpos($this->text_content, 'viewers') !== false) {
128 if (strpos($this->text_content, 'view ers') !== false) {
135 'banned' => 'boolean',
138 'user_id' => 'string',
141 protected $fillable = [