From 51a752c7cce2465043dfb3d63a0152b64765b167 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 8 Oct 2023 16:52:12 +0200 Subject: [PATCH] add twitch chat comand --- app/Http/Controllers/ChannelController.php | 12 ++++++ app/Models/TwitchBotCommand.php | 12 ++++++ app/TwitchBotCommands/BaseCommand.php | 4 +- app/TwitchBotCommands/ChatCommand.php | 23 ++++++++++++ .../js/components/twitch-bot/Controls.js | 37 ++++++++++++++++++- resources/js/i18n/de.js | 4 ++ resources/js/i18n/en.js | 4 ++ routes/api.php | 1 + 8 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 app/TwitchBotCommands/ChatCommand.php diff --git a/app/Http/Controllers/ChannelController.php b/app/Http/Controllers/ChannelController.php index f6840f8..da1f5ef 100644 --- a/app/Http/Controllers/ChannelController.php +++ b/app/Http/Controllers/ChannelController.php @@ -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'); diff --git a/app/Models/TwitchBotCommand.php b/app/Models/TwitchBotCommand.php index 94462e4..14cabf7 100644 --- a/app/Models/TwitchBotCommand.php +++ b/app/Models/TwitchBotCommand.php @@ -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'; diff --git a/app/TwitchBotCommands/BaseCommand.php b/app/TwitchBotCommands/BaseCommand.php index 881d4ed..8cdc0ee 100644 --- a/app/TwitchBotCommands/BaseCommand.php +++ b/app/TwitchBotCommands/BaseCommand.php @@ -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 index 0000000..fc69a01 --- /dev/null +++ b/app/TwitchBotCommands/ChatCommand.php @@ -0,0 +1,23 @@ +bot->sendIRCMessage(IRCMessage::privmsg($this->getParameter('channel'), $this->getParameter('text'))); + $resolve(); + }); + } + +} diff --git a/resources/js/components/twitch-bot/Controls.js b/resources/js/components/twitch-bot/Controls.js index 0aea916..1faa912 100644 --- a/resources/js/components/twitch-bot/Controls.js +++ b/resources/js/components/twitch-bot/Controls.js @@ -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} {channel ? -
+ + + {t('twitchBot.chat')} + { + setChatText(value); + }} + value={chatText} + /> + + + : {t('twitchBot.selectChannel')} diff --git a/resources/js/i18n/de.js b/resources/js/i18n/de.js index 8d4e5e4..79eb371 100644 --- a/resources/js/i18n/de.js +++ b/resources/js/i18n/de.js @@ -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', diff --git a/resources/js/i18n/en.js b/resources/js/i18n/en.js index 480a57c..997c639 100644 --- a/resources/js/i18n/en.js +++ b/resources/js/i18n/en.js @@ -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', diff --git a/routes/api.php b/routes/api.php index 6380b2d..4333494 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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'); -- 2.39.2