]> git.localhorst.tv Git - alttp.git/commitdiff
protocol chat bot messages
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 9 Apr 2024 12:59:13 +0000 (14:59 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 9 Apr 2024 12:59:13 +0000 (14:59 +0200)
app/Http/Controllers/ChannelController.php
app/Models/Channel.php
app/Models/ChatBotLog.php [new file with mode: 0644]
app/Models/TwitchBotCommand.php
app/TwitchBot/TwitchChatBot.php
app/TwitchBotCommands/BaseCommand.php
app/TwitchBotCommands/RandomChatCommand.php [new file with mode: 0644]
database/migrations/2024_04_09_122502_create_chat_bot_logs_table.php [new file with mode: 0644]

index 622276b9c4e56bde9d3e7ac4aea421cb8e558dd8..883ca65eff2360d93bcd20d9fb2d2bf93640caad 100644 (file)
@@ -60,8 +60,11 @@ class ChannelController extends Controller {
                ]);
                $this->authorize('editRestream', $channel);
                $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
-               $text = empty($validatedData['category']) ? $validatedData['text'] : $channel->randomOfClass($validatedData['category']);
-               TwitchBotCommand::chat($channel->twitch_chat, $text, $request->user(), $nick);
+               if (empty($validatedData['category'])) {
+                       TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
+               } else {
+                       TwitchBotCommand::randomChat($channel, $validatedData['category'], $request->user(), $nick);
+               }
                return $this->sendChannel($channel);
        }
 
index 5c7ae70c0e95ebfec714052becfc28580dc1f354..46155cda710d1c5d0de970a692d84a8fce889924 100644 (file)
@@ -32,10 +32,9 @@ class Channel extends Model {
        }
 
        public function randomOfClass($class) {
-               $line = $this->queryChatlog()
+               return $this->queryChatlog()
                        ->where('classification', '=', $class)
                        ->first();
-               return $line ? $line->text_content : '';
        }
 
        public function queryChatlog() {
diff --git a/app/Models/ChatBotLog.php b/app/Models/ChatBotLog.php
new file mode 100644 (file)
index 0000000..34d2feb
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class ChatBotLog extends Model {
+
+       use HasFactory;
+
+       public function channel() {
+               return $this->belongsTo(Channel::class);
+       }
+
+       public function origin() {
+               return $this->morphTo();
+       }
+
+}
index 716b8d5841cd1c2c628d6ddff5b1b0b623567c4f..6dc5a218087a8530103a4f998b9ed42fcc32a91e 100644 (file)
@@ -48,6 +48,19 @@ class TwitchBotCommand extends Model
                $cmd->save();
        }
 
+       public static function randomChat(Channel $channel, $category, User $user = null, $nick = 'localhorsttv') {
+               $cmd = new TwitchBotCommand();
+               $cmd->command = 'random-chat';
+               $cmd->parameters = [
+                       'channel' => $channel->id,
+                       'category' => $category,
+               ];
+               $cmd->status = 'pending';
+               $cmd->user()->associate($user);
+               $cmd->bot_nick = $nick;
+               $cmd->save();
+       }
+
        public function tournament() {
                return $this->belongsTo(Tournament::class);
        }
index aacc56fc11f2aa0e410787be739eab776136aacf..90a718d25b36875591e44b7ea9baf8d33fba69f6 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\TwitchBot;
 
 use App\Models\Channel;
+use App\Models\ChatBotLog;
 use App\Models\ChatLog;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Str;
@@ -74,8 +75,16 @@ class TwitchChatBot extends TwitchBot {
                $text = $this->contextualMsg($channel);
                if (!$text) $text = $this->randomChat($channel);
                if (!$text) return;
+               $actual_text = is_object($text) ? $text->text_content : $text;
                $this->tagChannelWrite($channel);
-               $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text));
+               $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $actual_text));
+               $log = new ChatBotLog();
+               $log->channel()->associate($channel);
+               if (is_object($text)) {
+                       $log->origin()->associate($text);
+               }
+               $log->text = $actual_text;
+               $log->save();
        }
 
        private function getNotes(Channel $channel) {
@@ -162,10 +171,9 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function randomChat(Channel $channel) {
-               $line = $channel->queryChatlog()
+               return $channel->queryChatlog()
                        ->whereIn('classification', ['hi', 'hype', 'lol', 'pog', 'unclassified'])
                        ->first();
-               return $line->text_content;
        }
 
        private function randomContextualNumber(Channel $channel) {
@@ -217,8 +225,7 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function randomMsg(Channel $channel) {
-               $line = $channel->queryChatlog()->first();
-               return $line->text_content;
+               return $channel->queryChatlog()->first();
        }
 
        private function randomWaitMsgs(Channel $channel) {
index 8cdc0ee61f01020790f5a0aa89a630ced86781ac..d803fb739defe022977c6b4cda7c017797695507 100644 (file)
@@ -16,6 +16,8 @@ abstract class BaseCommand {
                                return new JoinCommand($bot, $cmd);
                        case 'part':
                                return new PartCommand($bot, $cmd);
+                       case 'random-chat':
+                               return new RandomChatCommand($bot, $cmd);
                        default:
                                throw new \Exception('unrecognized command');
                }
diff --git a/app/TwitchBotCommands/RandomChatCommand.php b/app/TwitchBotCommands/RandomChatCommand.php
new file mode 100644 (file)
index 0000000..cc49c00
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace App\TwitchBotCommands;
+
+use App\Models\Channel;
+use App\Models\ChatBotLog;
+use App\Models\TwitchBotCommand;
+use App\TwitchBot\IRCMessage;
+use App\TwitchBot\TwitchBot;
+use React\Promise\Promise;
+
+class RandomChatCommand extends BaseCommand {
+
+       public function __construct(TwitchBot $bot, TwitchBotCommand $cmd) {
+               parent::__construct($bot, $cmd);
+       }
+
+       public function execute() {
+               return new Promise(function($resolve) {
+                       $channel = Channel::findOrFail($this->getParameter('channel'));
+                       $text = $channel->randomOfClass($this->getParameter('category'));
+                       $this->bot->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text->text_content));
+                       $log = new ChatBotLog();
+                       $log->channel()->associate($channel);
+                       if (is_object($text)) {
+                               $log->origin()->associate($text);
+                       }
+                       $log->text = $text->text_content;
+                       $log->save();
+                       $resolve();
+               });
+       }
+
+}
diff --git a/database/migrations/2024_04_09_122502_create_chat_bot_logs_table.php b/database/migrations/2024_04_09_122502_create_chat_bot_logs_table.php
new file mode 100644 (file)
index 0000000..3845b01
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        */
+       public function up(): void
+       {
+               Schema::create('chat_bot_logs', function (Blueprint $table) {
+                       $table->id();
+                       $table->timestamps();
+                       $table->foreignId('channel_id')->constrained();
+                       $table->foreignId('origin_id')->nullable()->default(null);
+                       $table->string('origin_type')->nullable()->default(null);
+                       $table->text('text');
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        */
+       public function down(): void
+       {
+               Schema::dropIfExists('chat_bot_logs');
+       }
+};