5 use App\TwitchBot\TokenizedMessage;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8 use Illuminate\Support\Arr;
9 use Illuminate\Support\Str;
10 use LanguageDetection\Language;
12 class ChatLog extends Model {
16 public function channel() {
17 return $this->belongsTo(Channel::class);
20 public function user() {
21 return $this->belongsTo(User::class);
24 public function tokenize() {
25 return TokenizedMessage::fromLog($this);
28 public function isReply() {
29 return !empty($this->tags['reply-parent-msg-body']);
32 public function getReplyParent() {
33 return str_replace('\\s', ' ', $this->tags['reply-parent-msg-body']);
36 public function getReplyParentUser() {
37 return $this->tags['reply-parent-display-name'];
40 public function getText() {
41 return $this->params[1];
44 public function getTextWithoutEmotes() {
45 $text = $this->params[1];
46 if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
47 $emotes = explode('/', $this->tags['emotes']);
48 foreach ($emotes as $emote) {
49 $set = explode(':', $emote);
50 $positions = explode(',', $set[1]);
51 foreach ($positions as $position) {
52 $coords = explode('-', $position);
53 $text = mb_substr($text, 0, $coords[0]).str_repeat(' ', $coords[1] - $coords[0] + 1).mb_substr($text, $coords[1] + 1);
57 return trim(preg_replace('/\s+/', ' ', $text));
60 public function getTextWithoutReply() {
61 if ($this->isReply()) {
62 return mb_substr($this->params[1], mb_strlen($this->getReplyParentUser()) + 2);
64 return $this->params[1];
67 public function evaluate() {
68 $this->evaluateUser();
69 $this->evaluateChannel();
71 if (is_null($this->nick)) {
72 $this->type = 'system';
75 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
80 if ($this->command == 'PRIVMSG' || $this->command == 'WHISPER') {
81 if (static::isKnownBot($this->nick)) {
83 } else if (substr($this->params[0], 0, 1) == '#') {
88 $this->text_content = $this->getTextWithoutReply();
89 $this->detectLanguage();
90 $tokenized = $this->tokenize();
91 if ($tokenized->isSpammy()) {
94 $this->classification = $tokenized->classify();
98 throw new \Exception('unidentified message');
101 public static function isKnownBot($nick) {
102 return in_array(strtolower($nick), [
104 'birrellthesquirrel',
108 'pokemoncommunitygame',
118 protected function evaluateUser() {
121 protected function evaluateChannel() {
122 if (empty($this->params)) {
125 $cname = $this->params[0];
126 if (substr($cname, 0, 1) != '#') {
129 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
130 if (!is_null($channel)) {
131 $this->channel()->associate($channel);
132 if (empty($this->twitch_category) && now()->sub(15, 'minute')->isBefore($this->created_at)) {
133 $this->twitch_category = $channel->twitch_category;
138 protected function detectLanguage() {
139 $languages = ['de', 'en', 'es', 'fr'];
140 if (!is_null($this->channel)) {
141 $languages = array_values($this->channel->languages);
142 if (!in_array('en', $languages)) {
146 $detector = (new Language($languages))->detect($this->getTextWithoutEmotes());
147 $scores = $detector->close();
148 $lang = strval($detector);
149 //var_dump($scores, $lang, $this->text_content);
150 if (!empty($lang) && $scores[$lang] > 0.4) {
151 $this->detected_language = $lang;
156 'banned' => 'boolean',
159 'user_id' => 'string',
162 protected $fillable = [