]> git.localhorst.tv Git - alttp.git/commitdiff
random chat buttons
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 6 Apr 2024 12:55:37 +0000 (14:55 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 6 Apr 2024 12:55:37 +0000 (14:55 +0200)
app/Http/Controllers/ChannelController.php
app/Models/Channel.php
app/TwitchBot/TwitchChatBot.php
resources/js/components/twitch-bot/Controls.js
resources/js/i18n/de.js
resources/js/i18n/en.js

index b72742f2d835d664ed7c55e30d8b3cf6e1aa6e14..622276b9c4e56bde9d3e7ac4aea421cb8e558dd8 100644 (file)
@@ -55,11 +55,13 @@ class ChannelController extends Controller {
                }
                $validatedData = $request->validate([
                        'bot_nick' => 'string',
-                       'text' => 'string|required',
+                       'category' => 'string',
+                       'text' => 'string',
                ]);
                $this->authorize('editRestream', $channel);
                $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
-               TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
+               $text = empty($validatedData['category']) ? $validatedData['text'] : $channel->randomOfClass($validatedData['category']);
+               TwitchBotCommand::chat($channel->twitch_chat, $text, $request->user(), $nick);
                return $this->sendChannel($channel);
        }
 
index 500f2e33e6f9fb3456694d7dc403159dd7205260..5c7ae70c0e95ebfec714052becfc28580dc1f354 100644 (file)
@@ -31,6 +31,42 @@ class Channel extends Model {
                        ->first();
        }
 
+       public function randomOfClass($class) {
+               $line = $this->queryChatlog()
+                       ->where('classification', '=', $class)
+                       ->first();
+               return $line ? $line->text_content : '';
+       }
+
+       public function queryChatlog() {
+               return ChatLog::where('type', '=', 'chat')
+                       ->where('banned', '=', false)
+                       ->where('created_at', '<', now()->sub(1, 'day'))
+                       ->where(function ($query) {
+                               $query->whereNull('detected_language');
+                               $query->orWhereIn('detected_language', $this->getPreferredLanguages());
+                       })
+                       ->inRandomOrder();
+       }
+
+       public function getPreferredLanguages() {
+               $setting = $this->getChatSetting('language');
+               if ($setting) {
+                       return [$setting];
+               }
+               if (!empty($this->languages)) {
+                       return $this->languages;
+               }
+               return ['de'];
+       }
+
+       public function getChatSetting($name, $default = null) {
+               if (array_key_exists($name, $this->chat_settings)) {
+                       return $this->chat_settings[$name];
+               }
+               return $default;
+       }
+
        public function getGuessingLeaderboard() {
                $query = $this->winners()
                        ->selectRaw('(select t2.uname from guessing_winners t2 where t2.uid = guessing_winners.uid order by created_at desc limit 1) as name, sum(score) as score')
index 03d9a38c88b402b327532e290249bb8d986f8990..7cdc704bedfcd4e6c25a40298929430405070690 100644 (file)
@@ -78,13 +78,6 @@ class TwitchChatBot extends TwitchBot {
                $this->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text));
        }
 
-       private function getChatSetting(Channel $channel, $name, $default = null) {
-               if (array_key_exists($name, $channel->chat_settings)) {
-                       return $channel->chat_settings[$name];
-               }
-               return $default;
-       }
-
        private function getNotes(Channel $channel) {
                if (!isset($this->notes[$channel->id])) {
                        $this->notes[$channel->id] = [
@@ -205,7 +198,7 @@ class TwitchChatBot extends TwitchBot {
                $last = $this->getNote($channel, 'last_special');
                if ($last != 'gg' && $this->checkForGG($channel)) {
                        $this->setNote($channel, 'last_special', 'gg');
-                       return $this->randomOfClass($channel, 'gg');
+                       return $channel->randomOfClass('gg');
                }
                if ($last != 'number' && $this->checkForNumbers($channel)) {
                        $this->setNote($channel, 'last_special', 'number');
@@ -217,40 +210,29 @@ class TwitchChatBot extends TwitchBot {
                }
                if ($last != 'glhf' && $this->checkForGLHF($channel)) {
                        $this->setNote($channel, 'last_special', 'glhf');
-                       return $this->randomOfClass($channel, 'gl');
+                       return $channel->randomOfClass('gl');
                }
                if ($last != 'hi' && $this->checkForGreeting($channel)) {
                        $this->setNote($channel, 'last_special', 'hi');
-                       return $this->randomOfClass($channel, 'hi');
+                       return $channel->randomOfClass('hi');
                }
                if ($last != 'hype' && $this->checkForHype($channel)) {
                        $this->setNote($channel, 'last_special', 'hype');
-                       return $this->randomOfClass($channel, 'hype');
+                       return $channel->randomOfClass('hype');
                }
                if ($last != 'pog' && $this->checkForPog($channel)) {
                        $this->setNote($channel, 'last_special', 'pog');
-                       return $this->randomOfClass($channel, 'pog');
+                       return $channel->randomOfClass('pog');
                }
                if ($last != 'o7' && $this->checkForSalute($channel)) {
                        $this->setNote($channel, 'last_special', 'o7');
-                       return $this->randomOfClass($channel, 'o7');
+                       return $channel->randomOfClass('o7');
                }
                return false;
        }
 
-       private function queryChatlog(Channel $channel) {
-               return ChatLog::where('type', '=', 'chat')
-                       ->where('banned', '=', false)
-                       ->where('created_at', '<', now()->sub(1, 'day'))
-                       ->where(function ($query) use ($channel) {
-                               $query->whereNull('detected_language');
-                               $query->orWhereIn('detected_language', $this->getPreferredLanguages($channel));
-                       })
-                       ->inRandomOrder();
-       }
-
        private function randomChat(Channel $channel) {
-               $line = $this->queryChatlog($channel)
+               $line = $channel->queryChatlog()
                        ->whereIn('classification', ['hi', 'hype', 'lol', 'pog', 'unclassified'])
                        ->first();
                return $line->text_content;
@@ -270,13 +252,6 @@ class TwitchChatBot extends TwitchBot {
                return random_int($min, $max);
        }
 
-       private function randomOfClass(Channel $channel, $class) {
-               $line = $this->queryChatlog($channel)
-                       ->where('classification', '=', $class)
-                       ->first();
-               return $line->text_content;
-       }
-
        private function randomLaughter(Channel $channel) {
                return Arr::random([
                        ':tf:',
@@ -307,24 +282,24 @@ class TwitchChatBot extends TwitchBot {
                        'SUBprise',
                        'xD',
                        'YouDontSay',
-                       $this->randomOfClass($channel, 'lol'),
+                       $channel->randomOfClass('lol'),
                ]);
        }
 
        private function randomMsg(Channel $channel) {
-               $line = $this->queryChatlog($channel)->first();
+               $line = $channel->queryChatlog()->first();
                return $line->text_content;
        }
 
        private function randomWaitMsgs(Channel $channel) {
-               $min = $this->getChatSetting($channel, 'wait_msgs_min', 1);
-               $max = $this->getChatSetting($channel, 'wait_msgs_max', 10);
+               $min = $channel->getChatSetting('wait_msgs_min', 1);
+               $max = $channel->getChatSetting('wait_msgs_max', 10);
                return random_int($min, $max);
        }
 
        private function randomWaitTime(Channel $channel) {
-               $min = $this->getChatSetting($channel, 'wait_time_min', 1);
-               $max = $this->getChatSetting($channel, 'wait_time_max', 900);
+               $min = $channel->getChatSetting('wait_time_min', 1);
+               $max = $channel->getChatSetting('wait_time_max', 900);
                return random_int($min, $max);
        }
 
@@ -352,17 +327,6 @@ class TwitchChatBot extends TwitchBot {
                $this->notes[$channel->id]['wait_time'] = $this->randomWaitTime($channel);
        }
 
-       private function getPreferredLanguages(Channel $channel) {
-               $setting = $this->getChatSetting($channel, 'language');
-               if ($setting) {
-                       return [$setting];
-               }
-               if (!empty($channel->languages)) {
-                       return $channel->languages;
-               }
-               return ['de'];
-       }
-
        private function isDirectedAtMe($raw_text) {
                $text = strtolower($raw_text);
                if (strpos($text, 'horstie') !== false) {
@@ -372,7 +336,7 @@ class TwitchChatBot extends TwitchBot {
        }
 
        private function shouldRespond(Channel $channel) {
-               $setting = $this->getChatSetting($channel, 'respond', 'yes');
+               $setting = $channel->getChatSetting('respond', 'yes');
                if ($setting == 'yes') {
                        return true;
                }
index 3093d90c04dd34e8b4f050367efa4c5c16f219b6..496ff11dd9dae4f977eecb793e25e0f0f2349256 100644 (file)
@@ -12,6 +12,8 @@ import ChannelSelect from '../common/ChannelSelect';
 import Icon from '../common/Icon';
 import ToggleSwitch from '../common/ToggleSwitch';
 
+const CHAT_CATEGORIES = ['unclassified', 'hi', 'gl', 'gg', 'lol', 'pog', 'hype', 'o7'];
+
 const Controls = () => {
        const [channel, setChannel] = React.useState(null);
        const [chatText, setChatText] = React.useState('');
@@ -33,6 +35,18 @@ const Controls = () => {
                }
        }, [channel, chatText, t]);
 
+       const randomChat = React.useCallback(async (category) => {
+               try {
+                       await axios.post(`/api/channels/${channel.id}/chat`, {
+                               bot_nick: 'horstiebot',
+                               category,
+                       });
+                       toastr.success(t('twitchBot.chatSuccess'));
+               } catch (e) {
+                       toastr.error(t('twitchBot.chatError'));
+               }
+       }, [channel, chatText, t]);
+
        const join = React.useCallback(async (bot_nick) => {
                try {
                        const rsp = await axios.post(`/api/channels/${channel.id}/join`, { bot_nick });
@@ -200,6 +214,18 @@ const Controls = () => {
                                                        </Button>
                                                </div>
                                        </Form.Group>
+                                       <h3 className="mt-3">{t('twitchBot.randomChat')}</h3>
+                                       <div className="button-bar">
+                                               {CHAT_CATEGORIES.map(category =>
+                                                       <Button
+                                                               key={category}
+                                                               onClick={() => { randomChat(category); }}
+                                                               variant="outline-secondary"
+                                                       >
+                                                               {t(`twitchBot.chatCategories.${category}`)}
+                                                       </Button>
+                                               )}
+                                       </div>
                                </Col>
                                <Col className="mt-5" md={6}>
                                        <h3>{t('twitchBot.chatSettings')}</h3>
index 2301b077c10cb9ea47a579029c3aee7c36ac661a..c663747c87c3b36d66c2486140327e2f5b5aa5f7 100644 (file)
@@ -679,6 +679,16 @@ export default {
                        addCommand: 'Command hinzufügen',
                        channel: 'Channel',
                        chat: 'Chat',
+                       chatCategories: {
+                               gg: 'Good Game',
+                               gl: 'Good Luck',
+                               hi: 'Begrüßung',
+                               hype: 'Hype',
+                               lol: 'Gelächter',
+                               o7: 'Salutieren',
+                               pog: 'Pog',
+                               unclassified: 'Generisch',
+                       },
                        chatError: 'Fehler beim Senden',
                        chatSettings: 'Chat Bot Einstellungen',
                        chatSuccess: 'Nachricht in Warteschlange',
@@ -759,6 +769,7 @@ export default {
                        noManagePermission: 'Du verfügst nicht über die notwendigen Berechtigungen, um den Twitch Bot zu administrieren.',
                        partError: 'Fehler beim Verlassen',
                        partSuccess: 'Verlassen',
+                       randomChat: 'Random Chat',
                        respond: 'Antworten',
                        respondOptions: {
                                50: '50:50',
index d76ce1bf0af0d1a0e6c9a9471892266b408697b4..0bc85e8e989bcc2c625fdca2c6dd196e00ba58db 100644 (file)
@@ -679,6 +679,16 @@ export default {
                        addCommand: 'Add command',
                        channel: 'Channel',
                        chat: 'Chat',
+                       chatCategories: {
+                               gg: 'Good Game',
+                               gl: 'Good Luck',
+                               hi: 'Greeting',
+                               hype: 'Hype',
+                               lol: 'Laughter',
+                               o7: 'Salute',
+                               pog: 'Pog',
+                               unclassified: 'Generic',
+                       },
                        chatError: 'Error sending message',
                        chatSettings: 'Chat Bot Settings',
                        chatSuccess: 'Message queued',
@@ -759,6 +769,7 @@ export default {
                        noManagePermission: 'You lack the required privileges to manage the twitch bot.',
                        partError: 'Error parting channel',
                        partSuccess: 'Parted',
+                       randomChat: 'Random Chat',
                        respond: 'Respond',
                        respondOptions: {
                                50: '50:50',