]> git.localhorst.tv Git - alttp.git/blob - app/DiscordBotCommands/BaseCommand.php
better schedule start
[alttp.git] / app / DiscordBotCommands / BaseCommand.php
1 <?php
2
3 namespace App\DiscordBotCommands;
4
5 use App\Models\DiscordBotCommand;
6 use App\Models\Round;
7 use App\Models\User;
8 use Discord\Discord;
9 use Discord\Parts\Channel\Channel;
10 use Discord\Parts\Guild\Guild;
11 use Discord\Parts\User\Member;
12 use Discord\Parts\User\User as DiscordUser;
13 use Illuminate\Support\Facades\App;
14
15 abstract class BaseCommand {
16
17         public static function resolve(Discord $discord, DiscordBotCommand $cmd) {
18                 switch ($cmd->command) {
19                         case 'presence':
20                                 return new PresenceCommand($discord, $cmd);
21                         case 'result':
22                                 return new ResultCommand($discord, $cmd);
23                         case 'sync-user':
24                                 return new SyncUserCommand($discord, $cmd);
25                         default:
26                                 throw new Exception('unrecognized command');
27                 }
28         }
29
30         public abstract function execute();
31
32
33         protected function __construct(Discord $discord, DiscordBotCommand $cmd) {
34                 $this->discord = $discord;
35                 $this->command = $cmd;
36                 if ($cmd->tournament && $cmd->tournament->locale) {
37                         App::setLocale($cmd->tournament->locale);
38                 }
39         }
40
41         protected function fetchGuild() {
42                 if (isset($this->guild)) {
43                         return \React\Promise\resolve($this->guild);
44                 }
45                 return $this->discord->guilds
46                         ->fetch($this->command->tournament->discord)
47                         ->then(function (Guild $guild) {
48                                 $this->guild = $guild;
49                                 if ($guild->preferred_locale && !($this->command->tournament && $this->command->tournament->locale)) {
50                                         App::setLocale($guild->preferred_locale);
51                                 }
52                                 return $guild;
53                         });
54         }
55
56         protected function fetchMember() {
57                 return $this->fetchGuild()->then(function (Guild $guild) {
58                         return $guild->members
59                                 ->fetch($this->getParameter('user'))
60                                 ->then(function (Member $member) {
61                                         $this->member = $member;
62                                         return $member;
63                                 });
64                 });
65         }
66
67         protected function fetchRoundChannel() {
68                 if (isset($this->roundChannel)) {
69                         return \React\Promise\resolve($this->roundChannel);
70                 }
71                 return $this->fetchGuild()
72                         ->then(function (Guild $guild) {
73                                 $channel = $guild->channels->find(function (Channel $c) {
74                                         return $c->name == $this->getRoundChannelName();
75                                 });
76                                 if ($channel) {
77                                         return $channel;
78                                 }
79                                 $channel = $guild->channels->create([
80                                         'name' => $this->getRoundChannelName(),
81                                         'is_private' => true,
82                                         'parent_id' => $this->command->tournament->discord_round_category,
83                                 ]);
84                                 return $guild->channels->save($channel);
85                         })
86                         ->then(function (Channel $channel) {
87                                 $this->roundChannel = $channel;
88                                 return $channel;
89                         });
90         }
91
92         protected function fetchUser() {
93                 if (isset($this->user)) {
94                         return \React\Promise\resolve($this->user);
95                 }
96                 return $this->discord->users
97                         ->fetch($this->getParameter('user'))
98                         ->then(function (DiscordUser $user) {
99                                 $this->user = $user;
100                                 return $user;
101                         });
102         }
103
104
105         protected function getParameter($name) {
106                 return $this->command->parameters[$name];
107         }
108
109         protected function getRound() {
110                 if (!$this->hasParameter('round')) {
111                         throw new \Exception('no rounds in parameters');
112                 }
113                 return Round::findOrFail($this->getParameter('round'));
114         }
115
116         protected function getRoundChannelName() {
117                 $round = $this->getRound();
118                 return sprintf($this->command->tournament->discord_round_template, $round->number);
119         }
120
121         protected function getUser() {
122                 if (!$this->hasParameter('user')) {
123                         throw new \Exception('no user in parameters');
124                 }
125                 return User::findOrFail($this->getParameter('user'));
126         }
127
128         protected function hasParameter($name) {
129                 return array_key_exists($name, $this->command->parameters);
130         }
131
132         protected function hasRoundChannels() {
133                 return !empty($this->command->tournament->discord_round_template);
134         }
135
136
137         protected $command;
138         protected $discord;
139
140         protected $guild = null;
141         protected $member = null;
142         protected $roundChannel = null;
143         protected $user = null;
144
145 }