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), [
57 'pokemoncommunitygame',
65 protected function evaluateUser() {
68 protected function evaluateChannel() {
69 if (empty($this->params)) {
70 $this->channel()->associate(null);
73 $cname = $this->params[0];
74 if (substr($cname, 0, 1) != '#') {
77 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
78 $this->channel()->associate($channel);
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)) {
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;
98 protected function scanForSpam() {
99 if (substr($this->text_content, 0, 1) == '!') {
102 if (strpos($this->text_content, '$') !== false) {
105 if (strpos($this->text_content, '€') !== false) {
108 if (strpos($this->text_content, '@') !== false) {
111 if (strpos($this->text_content, '://') !== false) {
114 if (is_numeric($this->text_content)) {
117 if (strpos($this->text_content, 'followers') !== false) {
120 if (strpos($this->text_content, 'promotion') !== false) {
123 if (strpos($this->text_content, 'viewers') !== false) {
126 if (strpos($this->text_content, 'view ers') !== false) {
133 'banned' => 'boolean',
136 'user_id' => 'string',
139 protected $fillable = [