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