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;
35 protected function fetchGuild() {
36 if (isset($this->guild)) {
37 return \React\Promise\resolve($this->guild);
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);
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;
61 protected function fetchRoundChannel() {
62 if (isset($this->roundChannel)) {
63 return \React\Promise\resolve($this->roundChannel);
65 return $this->fetchGuild()
66 ->then(function (Guild $guild) {
67 $channel = $guild->channels->find(function (Channel $c) {
68 return $c->name == $this->getRoundChannelName();
73 $channel = $guild->channels->create([
74 'name' => $this->getRoundChannelName(),
76 'parent_id' => $this->command->tournament->discord_round_category,
78 return $guild->channels->save($channel);
80 ->then(function (Channel $channel) {
81 $this->roundChannel = $channel;
87 protected function getParameter($name) {
88 return $this->command->parameters[$name];
91 protected function getRound() {
92 if (!$this->hasParameter('round')) {
93 throw new \Exception('no rounds in parameters');
95 return Round::findOrFail($this->getParameter('round'));
98 protected function getRoundChannelName() {
99 $round = $this->getRound();
100 return sprintf($this->command->tournament->discord_round_template, $round->number);
103 protected function getUser() {
104 if (!$this->hasParameter('user')) {
105 throw new \Exception('no user in parameters');
107 return User::findOrFail($this->getParameter('user'));
110 protected function hasParameter($name) {
111 return array_key_exists($name, $this->command->parameters);
114 protected function hasRoundChannels() {
115 return !empty($this->command->tournament->discord_round_template);
122 protected $guild = null;
123 protected $member = null;
124 protected $roundChannel = null;