5 use App\DiscordBotCommands\BaseCommand;
7 use Discord\Parts\Channel\Channel;
8 use Discord\Parts\Guild\Guild;
9 use Illuminate\Database\Eloquent\Factories\HasFactory;
10 use Illuminate\Database\Eloquent\Model;
12 class DiscordBotCommand extends Model
16 public static function queueResult(Result $result) {
17 $cmd = new DiscordBotCommand();
18 $cmd->tournament_id = $result->round->tournament_id;
19 $cmd->command = 'result';
21 'round' => $result->round_id,
22 'user' => $result->user_id,
24 $cmd->status = 'pending';
28 public static function syncUser($user_id) {
29 $cmd = new DiscordBotCommand();
30 $cmd->command = 'sync-user';
34 $cmd->status = 'pending';
38 public function tournament() {
39 return $this->belongsTo(Tournament::class);
42 public function user() {
43 return $this->belongsTo(User::class);
46 public function execute(Discord $discord) {
47 $this->setExecuting();
50 BaseCommand::resolve($discord, $this)
52 ->otherwise(function (\Throwable $e) {
53 $this->setException($e);
55 ->done(function($v = null) {
58 } catch (\Exception $e) {
59 $this->setException($e);
64 private function setDone() {
65 $this->status = 'done';
69 private function setExecuting() {
70 $this->status = 'executing';
71 $this->executed_at = now();
75 private function setException(\Throwable $e) {
76 $this->status = 'exception';
78 'type' => get_class($e),
79 'file' => $e->getFile(),
80 'line' => $e->getLine(),
81 'message' => $e->getMessage(),
88 'parameters' => 'array',
90 'executed_at' => 'datetime',