From: Daniel Karbach Date: Wed, 8 May 2024 13:10:27 +0000 (+0200) Subject: adlib chat X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=f0d1a566f5afd76ab7a56b295b71d5756dfd2bc3;p=alttp.git adlib chat --- diff --git a/app/Http/Controllers/ChannelController.php b/app/Http/Controllers/ChannelController.php index 026b9aa..121ae4e 100644 --- a/app/Http/Controllers/ChannelController.php +++ b/app/Http/Controllers/ChannelController.php @@ -54,13 +54,16 @@ class ChannelController extends Controller { throw new \Exception('channel has no twitch chat set'); } $validatedData = $request->validate([ + 'adlib' => 'boolean', 'bot_nick' => 'string', 'category' => 'string', 'text' => 'string', ]); $this->authorize('editRestream', $channel); $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick']; - if (empty($validatedData['category'])) { + if (isset($validatedData['adlib']) && $validatedData['adlib']) { + TwitchBotCommand::adlibChat($channel, $request->user()); + } else if (empty($validatedData['category'])) { TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick); } else { TwitchBotCommand::randomChat($channel, $validatedData['category'], $request->user(), $nick); @@ -83,6 +86,7 @@ class ChannelController extends Controller { throw new \Exception('channel has no twitch chat set'); } $validatedData = $request->validate([ + 'adlib' => 'integer|min:0|max:100', 'language' => 'string|in:de,en,es,fr', 'min_age' => 'integer|min:1', 'respond' => 'string|in:50,no,yes', diff --git a/app/Models/TwitchBotCommand.php b/app/Models/TwitchBotCommand.php index 6dc5a21..e232bdf 100644 --- a/app/Models/TwitchBotCommand.php +++ b/app/Models/TwitchBotCommand.php @@ -11,6 +11,18 @@ class TwitchBotCommand extends Model { use HasFactory; + public static function adlibChat(Channel $channel, User $user = null) { + $cmd = new TwitchBotCommand(); + $cmd->command = 'adlib-chat'; + $cmd->parameters = [ + 'channel' => $channel->id, + ]; + $cmd->status = 'pending'; + $cmd->user()->associate($user); + $cmd->bot_nick = 'horstiebot'; + $cmd->save(); + } + public static function chat($channel, $text, User $user = null, $nick = 'localhorsttv') { $cmd = new TwitchBotCommand(); $cmd->command = 'chat'; diff --git a/app/TwitchBot/TwitchChatBot.php b/app/TwitchBot/TwitchChatBot.php index 9289426..6ec517e 100644 --- a/app/TwitchBot/TwitchChatBot.php +++ b/app/TwitchBot/TwitchChatBot.php @@ -4,6 +4,7 @@ namespace App\TwitchBot; use App\Models\Channel; use App\Models\ChatBotLog; +use App\Models\ChatLib; use App\Models\ChatLog; use Illuminate\Support\Arr; use Illuminate\Support\Str; @@ -15,6 +16,8 @@ class TwitchChatBot extends TwitchBot { $this->updateChannels(); $this->startTimer(); $this->listenCommands(); + $this->chatlib = new ChatLib(); + $this->chatlib->loadFrom('de'); } public function joinChannels() { @@ -43,6 +46,10 @@ class TwitchChatBot extends TwitchBot { $this->tagChannelRead($channel, $msg); } + public function getChatlibDatabase(Channel $channel) { + return $this->chatlib; + } + private function startTimer() { $this->getLoop()->addPeriodicTimer(1, function () { @@ -73,6 +80,10 @@ class TwitchChatBot extends TwitchBot { return; } $text = $this->contextualMsg($channel); + if ($this->shouldAdlib($channel)) { + $this->performAdlib($channel); + return; + } if (!$text) $text = $this->randomChat($channel); if (!$text) return; $actual_text = is_object($text) ? $text->text_content : $text; @@ -245,6 +256,18 @@ class TwitchChatBot extends TwitchBot { return $channel->queryChatlog()->first(); } + private function performAdlib(Channel $channel) { + $db = $this->getChatlibDatabase($channel); + $text = $db->generate(); + $this->tagChannelWrite($channel); + $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text)); + $log = new ChatBotLog(); + $log->channel()->associate($channel); + $log->category = 'adlib'; + $log->text = $text; + $log->save(); + } + private function randomWaitMsgs(Channel $channel) { $min = $channel->getChatSetting('wait_msgs_min', 1); $max = $channel->getChatSetting('wait_msgs_max', 10); @@ -348,6 +371,17 @@ class TwitchChatBot extends TwitchBot { return false; } + private function shouldAdlib(Channel $channel) { + $setting = $channel->getChatSetting('adlib', 50); + if ($setting == 0) { + return false; + } + if ($setting == 100) { + return true; + } + return random_int(0, 100) <= $setting; + } + private function shouldRespond(Channel $channel) { $setting = $channel->getChatSetting('respond', 'yes'); if ($setting == 'yes') { @@ -387,5 +421,6 @@ class TwitchChatBot extends TwitchBot { private $channels; private $notes = []; + private $chatlib; } diff --git a/app/TwitchBotCommands/AdlibChatCommand.php b/app/TwitchBotCommands/AdlibChatCommand.php new file mode 100644 index 0000000..8079b8c --- /dev/null +++ b/app/TwitchBotCommands/AdlibChatCommand.php @@ -0,0 +1,34 @@ +getParameter('channel')); + $db = $this->bot->getChatlibDatabase($channel); + $text = $db->generate(); + $this->bot->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text)); + $log = new ChatBotLog(); + $log->channel()->associate($channel); + $log->text = $text; + $log->user()->associate($this->getExecutingUser()); + $log->category = 'adlib'; + $log->save(); + $resolve(); + }); + } + +} diff --git a/app/TwitchBotCommands/BaseCommand.php b/app/TwitchBotCommands/BaseCommand.php index e2df728..32ef41d 100644 --- a/app/TwitchBotCommands/BaseCommand.php +++ b/app/TwitchBotCommands/BaseCommand.php @@ -10,6 +10,8 @@ abstract class BaseCommand { public static function resolve(TwitchBot $bot, TwitchBotCommand $cmd) { switch ($cmd->command) { + case 'adlib-chat': + return new AdlibChatCommand($bot, $cmd); case 'chat': return new ChatCommand($bot, $cmd); case 'join': diff --git a/resources/js/components/twitch-bot/ChatSettingsForm.js b/resources/js/components/twitch-bot/ChatSettingsForm.js index 9f6a183..24b2a0a 100644 --- a/resources/js/components/twitch-bot/ChatSettingsForm.js +++ b/resources/js/components/twitch-bot/ChatSettingsForm.js @@ -160,6 +160,19 @@ const ChatSettingsForm = ({ : null} + + {t('twitchBot.chatAdlibChance')} + +
)}
+
+ +

{t('twitchBot.adlibChatNote')}

+
diff --git a/resources/js/i18n/de.js b/resources/js/i18n/de.js index 85d8fcb..f271354 100644 --- a/resources/js/i18n/de.js +++ b/resources/js/i18n/de.js @@ -695,9 +695,14 @@ export default { }, twitchBot: { addCommand: 'Command hinzufügen', + adlibChat: 'ad lib Chat', + adlibChatDesc: 'Generier eine Nachricht basierend auf alten.', + adlibChatNote: 'Aufgrund der benötigten Datenmenge aktuell nur in deutsch verfügbar.', channel: 'Channel', chat: 'Chat', + chatAdlibChance: 'ad lib Chance', chatCategories: { + adlib: 'ad lib', eyes: 'Augen', gg: 'Good Game', gl: 'Good Luck', diff --git a/resources/js/i18n/en.js b/resources/js/i18n/en.js index 5d5a1e4..8d8ccdc 100644 --- a/resources/js/i18n/en.js +++ b/resources/js/i18n/en.js @@ -695,9 +695,14 @@ export default { }, twitchBot: { addCommand: 'Add command', + adlibChat: 'ad lib Chat', + adlibChatDesc: 'Generate a message inspired by the chatlog.', + adlibChatNote: 'Due to the amount of data required currently only supported in german.', channel: 'Channel', chat: 'Chat', + chatAdlibChance: 'ad lib chance', chatCategories: { + adlib: 'ad lib', eyes: 'Eyes', gg: 'Good Game', gl: 'Good Luck',