From: Daniel Karbach Date: Tue, 9 Apr 2024 12:59:13 +0000 (+0200) Subject: protocol chat bot messages X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=ebdf8e5f6761de2abd85b01096a67dee62d7d4aa;p=alttp.git protocol chat bot messages --- diff --git a/app/Http/Controllers/ChannelController.php b/app/Http/Controllers/ChannelController.php index 622276b..883ca65 100644 --- a/app/Http/Controllers/ChannelController.php +++ b/app/Http/Controllers/ChannelController.php @@ -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); } diff --git a/app/Models/Channel.php b/app/Models/Channel.php index 5c7ae70..46155cd 100644 --- a/app/Models/Channel.php +++ b/app/Models/Channel.php @@ -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 index 0000000..34d2feb --- /dev/null +++ b/app/Models/ChatBotLog.php @@ -0,0 +1,20 @@ +belongsTo(Channel::class); + } + + public function origin() { + return $this->morphTo(); + } + +} diff --git a/app/Models/TwitchBotCommand.php b/app/Models/TwitchBotCommand.php index 716b8d5..6dc5a21 100644 --- a/app/Models/TwitchBotCommand.php +++ b/app/Models/TwitchBotCommand.php @@ -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); } diff --git a/app/TwitchBot/TwitchChatBot.php b/app/TwitchBot/TwitchChatBot.php index aacc56f..90a718d 100644 --- a/app/TwitchBot/TwitchChatBot.php +++ b/app/TwitchBot/TwitchChatBot.php @@ -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) { diff --git a/app/TwitchBotCommands/BaseCommand.php b/app/TwitchBotCommands/BaseCommand.php index 8cdc0ee..d803fb7 100644 --- a/app/TwitchBotCommands/BaseCommand.php +++ b/app/TwitchBotCommands/BaseCommand.php @@ -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 index 0000000..cc49c00 --- /dev/null +++ b/app/TwitchBotCommands/RandomChatCommand.php @@ -0,0 +1,34 @@ +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 index 0000000..3845b01 --- /dev/null +++ b/database/migrations/2024_04_09_122502_create_chat_bot_logs_table.php @@ -0,0 +1,31 @@ +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'); + } +};