]);
$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);
}
}
public function randomOfClass($class) {
- $line = $this->queryChatlog()
+ return $this->queryChatlog()
->where('classification', '=', $class)
->first();
- return $line ? $line->text_content : '';
}
public function queryChatlog() {
--- /dev/null
+<?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();
+ }
+
+}
$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);
}
namespace App\TwitchBot;
use App\Models\Channel;
+use App\Models\ChatBotLog;
use App\Models\ChatLog;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
$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) {
}
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) {
}
private function randomMsg(Channel $channel) {
- $line = $channel->queryChatlog()->first();
- return $line->text_content;
+ return $channel->queryChatlog()->first();
}
private function randomWaitMsgs(Channel $channel) {
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');
}
--- /dev/null
+<?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();
+ });
+ }
+
+}
--- /dev/null
+<?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');
+ }
+};