]> git.localhorst.tv Git - alttp.git/commitdiff
add twitch chat comand
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 8 Oct 2023 14:52:12 +0000 (16:52 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 8 Oct 2023 14:52:12 +0000 (16:52 +0200)
app/Http/Controllers/ChannelController.php
app/Models/TwitchBotCommand.php
app/TwitchBotCommands/BaseCommand.php
app/TwitchBotCommands/ChatCommand.php [new file with mode: 0644]
resources/js/components/twitch-bot/Controls.js
resources/js/i18n/de.js
resources/js/i18n/en.js
routes/api.php

index f6840f87f837f27e94dc36d4dad096d8d4a4ba17..da1f5ef76590166b5e9cf1827ce93cf2c8240421 100644 (file)
@@ -42,6 +42,18 @@ class ChannelController extends Controller {
                return $channel->toJson();
        }
 
+       public function chat(Request $request, Channel $channel) {
+               if (!$channel->twitch_chat) {
+                       throw new \Exception('channel has no twitch chat set');
+               }
+               $validatedData = $request->validate([
+                       'text' => 'string|required',
+               ]);
+               $this->authorize('editRestream', $channel);
+               TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user());
+               return $channel->toJson();
+       }
+
        public function join(Request $request, Channel $channel) {
                if (!$channel->twitch_chat) {
                        throw new \Exception('channel has no twitch chat set');
index 94462e4e5798b6d7967052b2d3054a59b77f722b..14cabf7a9513d0f812d317710b457652d397ac77 100644 (file)
@@ -11,6 +11,18 @@ class TwitchBotCommand extends Model
 {
        use HasFactory;
 
+       public static function chat($channel, $text, User $user = null) {
+               $cmd = new TwitchBotCommand();
+               $cmd->command = 'chat';
+               $cmd->parameters = [
+                       'channel' => $channel,
+                       'text' => $text,
+               ];
+               $cmd->status = 'pending';
+               $cmd->user()->associate($user);
+               $cmd->save();
+       }
+
        public static function join($channel, User $user = null) {
                $cmd = new TwitchBotCommand();
                $cmd->command = 'join';
index 881d4ed03e91c3611ac5fe7d65e4564be0154b8a..8cdc0ee61f01020790f5a0aa89a630ced86781ac 100644 (file)
@@ -10,12 +10,14 @@ abstract class BaseCommand {
 
        public static function resolve(TwitchBot $bot, TwitchBotCommand $cmd) {
                switch ($cmd->command) {
+                       case 'chat':
+                               return new ChatCommand($bot, $cmd);
                        case 'join':
                                return new JoinCommand($bot, $cmd);
                        case 'part':
                                return new PartCommand($bot, $cmd);
                        default:
-                               throw new Exception('unrecognized command');
+                               throw new \Exception('unrecognized command');
                }
        }
 
diff --git a/app/TwitchBotCommands/ChatCommand.php b/app/TwitchBotCommands/ChatCommand.php
new file mode 100644 (file)
index 0000000..fc69a01
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace App\TwitchBotCommands;
+
+use App\Models\TwitchBotCommand;
+use App\TwitchBot\IRCMessage;
+use App\TwitchBot\TwitchBot;
+use React\Promise\Promise;
+
+class ChatCommand extends BaseCommand {
+
+       public function __construct(TwitchBot $bot, TwitchBotCommand $cmd) {
+               parent::__construct($bot, $cmd);
+       }
+
+       public function execute() {
+               return new Promise(function($resolve) {
+                       $this->bot->sendIRCMessage(IRCMessage::privmsg($this->getParameter('channel'), $this->getParameter('text')));
+                       $resolve();
+               });
+       }
+
+}
index 0aea916cf04d698a879bc6bed886a88ebb426e08..1faa912fa0c95525dcc7bf1983fac4113f0243f1 100644 (file)
@@ -1,6 +1,6 @@
 import axios from 'axios';
 import React from 'react';
-import { Alert, Col, Form, Row } from 'react-bootstrap';
+import { Alert, Button, Col, Form, Row } from 'react-bootstrap';
 import { useTranslation } from 'react-i18next';
 import toastr from 'toastr';
 
@@ -9,9 +9,21 @@ import ToggleSwitch from '../common/ToggleSwitch';
 
 const Controls = () => {
        const [channel, setChannel] = React.useState(null);
+       const [chatText, setChatText] = React.useState('');
 
        const { t } = useTranslation();
 
+       const chat = React.useCallback(async text => {
+               try {
+                       await axios.post(`/api/channels/${channel.id}/chat`, {
+                               text,
+                       });
+                       toastr.success(t('twitchBot.chatSuccess'));
+               } catch (e) {
+                       toastr.error(t('twitchBot.chatError'));
+               }
+       }, [channel, chatText, t]);
+
        const join = React.useCallback(async () => {
                try {
                        const rsp = await axios.post(`/api/channels/${channel.id}/join`);
@@ -64,7 +76,28 @@ const Controls = () => {
                        : null}
                </Row>
                {channel ?
-                       <div />
+                       <Row>
+                               <Form.Group as={Col} md={6}>
+                                       <Form.Label>{t('twitchBot.chat')}</Form.Label>
+                                       <Form.Control
+                                               as="textarea"
+                                               onChange={({ target: { value } }) => {
+                                                       setChatText(value);
+                                               }}
+                                               value={chatText}
+                                       />
+                                       <Button
+                                               className="mt-2"
+                                               disabled={!chatText}
+                                               onClick={() => {
+                                                       if (chatText) chat(chatText);
+                                               }}
+                                               variant="twitch"
+                                       >
+                                               {t('button.send')}
+                                       </Button>
+                               </Form.Group>
+                       </Row>
                :
                        <Alert variant="info">
                                {t('twitchBot.selectChannel')}
index 8d4e5e4c8c07b6084f9afa6255bbaf9d4e766943..79eb3716b65df2f58d75396b718a9a3f4193f943 100644 (file)
@@ -76,6 +76,7 @@ export default {
                        retry: 'Neu versuchen',
                        save: 'Speichern',
                        search: 'Suche',
+                       send: 'Senden',
                        settings: 'Einstellungen',
                        signUp: 'Anmelden',
                        stop: 'Stop',
@@ -476,6 +477,9 @@ export default {
                },
                twitchBot: {
                        channel: 'Channel',
+                       chat: 'Chat',
+                       chatError: 'Fehler beim Senden',
+                       chatSuccess: 'Nachricht in Warteschlange',
                        controls: 'Controls',
                        heading: 'Twitch Bot',
                        join: 'Join',
index 480a57cc204ed8a0a6876d7f872d9b97e1ad02dd..997c639d30158323fb19033f15eba50c14e42e22 100644 (file)
@@ -76,6 +76,7 @@ export default {
                        retry: 'Retry',
                        save: 'Save',
                        search: 'Search',
+                       send: 'Send',
                        settings: 'Settings',
                        signUp: 'Sign up',
                        stop: 'Stop',
@@ -476,6 +477,9 @@ export default {
                },
                twitchBot: {
                        channel: 'Channel',
+                       chat: 'Chat',
+                       chatError: 'Error sending message',
+                       chatSuccess: 'Message queued',
                        controls: 'Controls',
                        heading: 'Twitch Bot',
                        join: 'Join',
index 6380b2d2a6d9e4b2d40b0299044ec5b177586237..43334942606f668a68ba3dca5ee0dc564b509c37 100644 (file)
@@ -26,6 +26,7 @@ Route::post('application/{application}/reject', 'App\Http\Controllers\Applicatio
 
 Route::get('channels', 'App\Http\Controllers\ChannelController@search');
 Route::get('channels/{channel}', 'App\Http\Controllers\ChannelController@single');
+Route::post('channels/{channel}/chat', 'App\Http\Controllers\ChannelController@chat');
 Route::post('channels/{channel}/join', 'App\Http\Controllers\ChannelController@join');
 Route::post('channels/{channel}/part', 'App\Http\Controllers\ChannelController@part');