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 $cmd_name = strtolower($cmd[0]);
45 if (!isset($channel->chat_commands[$cmd_name])) return;
46 $config = $channel->chat_commands[$cmd_name];
47 $this->getLogger()->info('got command '.$cmd_name.' on channel '.$channel->title);
49 $command = ChatCommand::create($this, $channel, $config);
50 if ($command->checkAccess($msg)) {
51 $command->execute($cmd[1] ?? '');
53 } catch (\Exception $e) {
54 $this->getLogger()->warning('error executing command '.$cmd_name.' on channel '.$channel->title.': '.$e->getMessage());
58 public function joinChannels() {
59 $this->getLogger()->info('joining channels');
60 $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get();
62 foreach ($channels as $channel) {
63 $names[] = $channel->twitch_chat;
65 $chunks = array_chunk($names, 10);
66 foreach ($chunks as $chunk) {
67 $this->sendIRCMessage(IRCMessage::join($chunk));