X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FDiscordBotCommand.php;h=7d7c3a88da96b6761a74e943474dd5ef1e6aaa78;hb=cb1131aef6b342429304ce3e3b92e4b3d700d494;hp=aec2f56988997ec01f666fbacfbd659a55aa46f7;hpb=12f64085fd212a9744db01790cefad53c970e8d9;p=alttp.git diff --git a/app/Models/DiscordBotCommand.php b/app/Models/DiscordBotCommand.php index aec2f56..7d7c3a8 100644 --- a/app/Models/DiscordBotCommand.php +++ b/app/Models/DiscordBotCommand.php @@ -2,8 +2,10 @@ namespace App\Models; +use App\DiscordBotCommands\BaseCommand; use Discord\Discord; -use Discord\Parts\User\Activity; +use Discord\Parts\Channel\Channel; +use Discord\Parts\Guild\Guild; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -11,58 +13,77 @@ class DiscordBotCommand extends Model { use HasFactory; + public static function queueResult(Result $result) { + $cmd = new DiscordBotCommand(); + $cmd->tournament_id = $result->round->tournament_id; + $cmd->command = 'result'; + $cmd->parameters = [ + 'round' => $result->round_id, + 'user' => $result->user_id, + ]; + $cmd->status = 'pending'; + $cmd->save(); + } + + public static function syncUser($user_id) { + $cmd = new DiscordBotCommand(); + $cmd->command = 'sync-user'; + $cmd->parameters = [ + 'user' => $user_id, + ]; + $cmd->status = 'pending'; + $cmd->save(); + } + + public function tournament() { + return $this->belongsTo(Tournament::class); + } + + public function user() { + return $this->belongsTo(User::class); + } + public function execute(Discord $discord) { - $this->status = 'executing'; - $this->executed_at = now(); - $this->save(); + $this->setExecuting(); try { - switch ($this->command) { - case 'presence': - $this->executePresence($discord); - break; - default: - throw new Exception('unrecognized command'); - } - $this->status = 'done'; - $this->save(); + BaseCommand::resolve($discord, $this) + ->execute() + ->otherwise(function (\Throwable $e) { + $this->setException($e); + }) + ->done(function($v = null) { + $this->setDone(); + }); } catch (\Exception $e) { - $this->status = 'exception'; - $this->result = [ - 'type' => get_class($e), - 'message' => $e->getMessage(), - ]; - $this->save(); + $this->setException($e); } } - protected function executePresence(Discord $discord) { - $activity = null; - $idle = false; - $status = 'online'; - $afk = false; - if (isset($this->parameters['activity'])) { - $activity = new Activity($discord); - $activity->type = $this->parameters['activity']; - if (isset($this->parameters['name'])) { - $activity->name = $this->parameters['name']; - } - if (isset($this->parameters['url'])) { - $activity->url = $this->parameters['url']; - } - } - if (isset($this->parameters['idle'])) { - $idle = $this->parameters['idle']; - } - if (isset($this->parameters['status'])) { - $status = $this->parameters['status']; - } - if (isset($this->parameters['afk'])) { - $afk = $this->parameters['afk']; - } - $discord->updatePresence($activity, $idle, $status, $afk); + + private function setDone() { + $this->status = 'done'; + $this->save(); } + private function setExecuting() { + $this->status = 'executing'; + $this->executed_at = now(); + $this->save(); + } + + private function setException(\Throwable $e) { + $this->status = 'exception'; + $this->result = [ + 'type' => get_class($e), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'message' => $e->getMessage(), + ]; + $this->save(); + } + + protected $casts = [ 'parameters' => 'array', 'result' => 'array',