]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBotCommands/BaseCommand.php
d803fb739defe022977c6b4cda7c017797695507
[alttp.git] / app / TwitchBotCommands / BaseCommand.php
1 <?php
2
3 namespace App\TwitchBotCommands;
4
5 use App\Models\TwitchBotCommand;
6 use App\Models\User;
7 use App\TwitchBot\TwitchBot;
8
9 abstract class BaseCommand {
10
11         public static function resolve(TwitchBot $bot, TwitchBotCommand $cmd) {
12                 switch ($cmd->command) {
13                         case 'chat':
14                                 return new ChatCommand($bot, $cmd);
15                         case 'join':
16                                 return new JoinCommand($bot, $cmd);
17                         case 'part':
18                                 return new PartCommand($bot, $cmd);
19                         case 'random-chat':
20                                 return new RandomChatCommand($bot, $cmd);
21                         default:
22                                 throw new \Exception('unrecognized command');
23                 }
24         }
25
26         public abstract function execute();
27
28         protected function __construct(TwitchBot $bot, TwitchBotCommand $cmd) {
29                 $this->bot = $bot;
30                 $this->command = $cmd;
31                 if ($cmd->tournament && $cmd->tournament->locale) {
32                         App::setLocale($cmd->tournament->locale);
33                 }
34         }
35
36         protected function getParameter($name) {
37                 return $this->command->parameters[$name];
38         }
39
40         protected function getUser() {
41                 if (!$this->hasParameter('user')) {
42                         throw new \Exception('no user in parameters');
43                 }
44                 return User::findOrFail($this->getParameter('user'));
45         }
46
47         protected function hasParameter($name) {
48                 return array_key_exists($name, $this->command->parameters);
49         }
50
51         protected $bot;
52         protected $command;
53
54 }