3 namespace App\DiscordBotCommands;
5 use App\Models\DiscordBotCommand;
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;
15 abstract class BaseCommand {
17 public static function resolve(Discord $discord, DiscordBotCommand $cmd) {
18 switch ($cmd->command) {
20 return new PresenceCommand($discord, $cmd);
22 return new ResultCommand($discord, $cmd);
24 return new SyncUserCommand($discord, $cmd);
26 throw new Exception('unrecognized command');
30 public abstract function execute();
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);
41 protected function fetchGuild() {
42 if (isset($this->guild)) {
43 return \React\Promise\resolve($this->guild);
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);
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;
67 protected function fetchRoundChannel() {
68 if (isset($this->roundChannel)) {
69 return \React\Promise\resolve($this->roundChannel);
71 return $this->fetchGuild()
72 ->then(function (Guild $guild) {
73 $channel = $guild->channels->find(function (Channel $c) {
74 return $c->name == $this->getRoundChannelName();
79 $channel = $guild->channels->create([
80 'name' => $this->getRoundChannelName(),
82 'parent_id' => $this->command->tournament->discord_round_category,
84 return $guild->channels->save($channel);
86 ->then(function (Channel $channel) {
87 $this->roundChannel = $channel;
92 protected function fetchUser() {
93 if (isset($this->user)) {
94 return \React\Promise\resolve($this->user);
96 return $this->discord->users
97 ->fetch($this->getParameter('user'))
98 ->then(function (DiscordUser $user) {
105 protected function getParameter($name) {
106 return $this->command->parameters[$name];
109 protected function getRound() {
110 if (!$this->hasParameter('round')) {
111 throw new \Exception('no rounds in parameters');
113 return Round::findOrFail($this->getParameter('round'));
116 protected function getRoundChannelName() {
117 $round = $this->getRound();
118 return sprintf($this->command->tournament->discord_round_template, $round->number);
121 protected function getUser() {
122 if (!$this->hasParameter('user')) {
123 throw new \Exception('no user in parameters');
125 return User::findOrFail($this->getParameter('user'));
128 protected function hasParameter($name) {
129 return array_key_exists($name, $this->command->parameters);
132 protected function hasRoundChannels() {
133 return !empty($this->command->tournament->discord_round_template);
140 protected $guild = null;
141 protected $member = null;
142 protected $roundChannel = null;
143 protected $user = null;