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 Illuminate\Support\Facades\App;
14 abstract class BaseCommand {
16 public static function resolve(Discord $discord, DiscordBotCommand $cmd) {
17 switch ($cmd->command) {
19 return new PresenceCommand($discord, $cmd);
21 return new ResultCommand($discord, $cmd);
23 throw new Exception('unrecognized command');
27 public abstract function execute();
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);
38 protected function fetchGuild() {
39 if (isset($this->guild)) {
40 return \React\Promise\resolve($this->guild);
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);
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;
64 protected function fetchRoundChannel() {
65 if (isset($this->roundChannel)) {
66 return \React\Promise\resolve($this->roundChannel);
68 return $this->fetchGuild()
69 ->then(function (Guild $guild) {
70 $channel = $guild->channels->find(function (Channel $c) {
71 return $c->name == $this->getRoundChannelName();
76 $channel = $guild->channels->create([
77 'name' => $this->getRoundChannelName(),
79 'parent_id' => $this->command->tournament->discord_round_category,
81 return $guild->channels->save($channel);
83 ->then(function (Channel $channel) {
84 $this->roundChannel = $channel;
90 protected function getParameter($name) {
91 return $this->command->parameters[$name];
94 protected function getRound() {
95 if (!$this->hasParameter('round')) {
96 throw new \Exception('no rounds in parameters');
98 return Round::findOrFail($this->getParameter('round'));
101 protected function getRoundChannelName() {
102 $round = $this->getRound();
103 return sprintf($this->command->tournament->discord_round_template, $round->number);
106 protected function getUser() {
107 if (!$this->hasParameter('user')) {
108 throw new \Exception('no user in parameters');
110 return User::findOrFail($this->getParameter('user'));
113 protected function hasParameter($name) {
114 return array_key_exists($name, $this->command->parameters);
117 protected function hasRoundChannels() {
118 return !empty($this->command->tournament->discord_round_template);
125 protected $guild = null;
126 protected $member = null;
127 protected $roundChannel = null;