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',
64 protected function evaluateUser() {
67 protected function evaluateChannel() {
68 if (empty($this->params)) {
69 $this->channel()->associate(null);
72 $cname = $this->params[0];
73 if (substr($cname, 0, 1) != '#') {
76 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
77 $this->channel()->associate($channel);
80 protected function detectLanguage() {
81 $languages = ['de', 'en', 'es', 'fr'];
82 if (!is_null($this->channel)) {
83 $languages = array_values($this->channel->languages);
84 if (!in_array('en', $languages)) {
88 $detector = LanguageDetector::detect($this->text_content, $languages);
89 $scores = $detector->getScores();
90 $lang = strval($detector->getLanguage());
91 //var_dump($scores, $lang, $this->text_content);
92 if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
93 $this->detected_language = $lang;
97 protected function scanForSpam() {
98 if (substr($this->text_content, 0, 1) == '!') {
101 if (strpos($this->text_content, '$') !== false) {
104 if (strpos($this->text_content, '€') !== false) {
107 if (strpos($this->text_content, '@') !== false) {
110 if (strpos($this->text_content, '://') !== false) {
113 if (is_numeric($this->text_content)) {
116 if (strpos($this->text_content, 'followers') !== false) {
119 if (strpos($this->text_content, 'promotion') !== false) {
122 if (strpos($this->text_content, 'viewers') !== false) {
125 if (strpos($this->text_content, 'view ers') !== false) {
132 'banned' => 'boolean',
135 'user_id' => 'string',
138 protected $fillable = [