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');
{
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';
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');
}
}
--- /dev/null
+<?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();
+ });
+ }
+
+}
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';
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`);
: 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')}
retry: 'Neu versuchen',
save: 'Speichern',
search: 'Suche',
+ send: 'Senden',
settings: 'Einstellungen',
signUp: 'Anmelden',
stop: 'Stop',
},
twitchBot: {
channel: 'Channel',
+ chat: 'Chat',
+ chatError: 'Fehler beim Senden',
+ chatSuccess: 'Nachricht in Warteschlange',
controls: 'Controls',
heading: 'Twitch Bot',
join: 'Join',
retry: 'Retry',
save: 'Save',
search: 'Search',
+ send: 'Send',
settings: 'Settings',
signUp: 'Sign up',
stop: 'Stop',
},
twitchBot: {
channel: 'Channel',
+ chat: 'Chat',
+ chatError: 'Error sending message',
+ chatSuccess: 'Message queued',
controls: 'Controls',
heading: 'Twitch Bot',
join: 'Join',
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');