3 namespace App\TwitchBot;
5 use App\Models\Channel;
7 class TwitchAppBot extends TwitchBot {
9 public function __construct() {
10 parent::__construct('localhorsttv');
11 $this->listenCommands();
14 public function logMessage(IRCMessage $msg) {
18 public function handlePrivMsg(IRCMessage $msg) {
19 $target = $msg->getPrivMsgTarget();
20 if ($target[0] != '#') return; // direct message
21 $text = $msg->getText();
22 if ($text[0] != '!') return;
23 $channel = $this->getMessageChannel($msg);
24 if (!$channel) return;
25 $this->handleChatCommand($channel, $msg);
28 public function handleChatCommand(Channel $channel, IRCMessage $msg) {
29 $cmd = explode(' ', ltrim($msg->getText(), '!'), 2);
30 if (!isset($channel->chat_commands[$cmd[0]])) return;
31 $config = $channel->chat_commands[$cmd[0]];
32 $this->getLogger()->info('got command '.$cmd[0].' on channel '.$channel->title);
34 $command = ChatCommand::create($this, $channel, $config);
35 $command->execute($cmd[1] ?? '');
36 } catch (\Exception $e) {
37 $this->getLogger()->warning('error executing command '.$cmd[0].' on channel '.$channel->title.': '.$e->getMessage());
41 public function joinChannels() {
42 $this->getLogger()->info('joining channels');
43 $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get();
45 foreach ($channels as $channel) {
46 $names[] = $channel->twitch_chat;
48 $chunks = array_chunk($names, 10);
49 foreach ($chunks as $chunk) {
50 $this->sendIRCMessage(IRCMessage::join($chunk));