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