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 $channel = $this->getMessageChannel($msg);
22 if (!$channel) return;
23 $text = $msg->getText();
24 if ($text[0] == '!') {
25 $this->handleChatCommand($channel, $msg);
27 $channel->isAcceptingGuesses() &&
28 !empty($msg->tags['user-id']) &&
29 !empty($msg->tags['display-name'] &&
30 $channel->isValidGuess($text))
32 $uid = 't:'.$msg->tags['user-id'];
33 $uname = $msg->tags['display-name'];
35 $channel->registerGuess($uid, $uname, $text);
36 } catch (\Exception $e) {
37 $this->getLogger()->warning('error registering guess "'.$text.'" on channel '.$channel->title.': '.$e->getMessage());
42 public function handleChatCommand(Channel $channel, IRCMessage $msg) {
43 $cmd = explode(' ', ltrim($msg->getText(), '!'), 2);
44 if (!isset($channel->chat_commands[$cmd[0]])) return;
45 $config = $channel->chat_commands[$cmd[0]];
46 $this->getLogger()->info('got command '.$cmd[0].' on channel '.$channel->title);
48 $command = ChatCommand::create($this, $channel, $config);
49 if ($command->checkAccess($msg)) {
50 $command->execute($cmd[1] ?? '');
52 } catch (\Exception $e) {
53 $this->getLogger()->warning('error executing command '.$cmd[0].' on channel '.$channel->title.': '.$e->getMessage());
57 public function joinChannels() {
58 $this->getLogger()->info('joining channels');
59 $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get();
61 foreach ($channels as $channel) {
62 $names[] = $channel->twitch_chat;
64 $chunks = array_chunk($names, 10);
65 foreach ($chunks as $chunk) {
66 $this->sendIRCMessage(IRCMessage::join($chunk));