]> git.localhorst.tv Git - alttp.git/blob - app/Models/DiscordBotCommand.php
discord result command
[alttp.git] / app / Models / DiscordBotCommand.php
1 <?php
2
3 namespace App\Models;
4
5 use App\DiscordBotCommands\BaseCommand;
6 use Discord\Discord;
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;
11
12 class DiscordBotCommand extends Model
13 {
14         use HasFactory;
15
16         public function tournament() {
17                 return $this->belongsTo(Tournament::class);
18         }
19
20         public function user() {
21                 return $this->belongsTo(User::class);
22         }
23
24         public function execute(Discord $discord) {
25                 $this->setExecuting();
26
27                 try {
28                         BaseCommand::resolve($discord, $this)
29                                 ->execute()
30                                 ->otherwise(function (\Throwable $e) {
31                                         $this->setException($e);
32                                 })
33                                 ->done(function($v = null) {
34                                         $this->setDone();
35                                 });
36                 } catch (\Exception $e) {
37                         $this->setException($e);
38                 }
39         }
40
41
42         private function setDone() {
43                 $this->status = 'done';
44                 $this->save();
45         }
46
47         private function setExecuting() {
48                 $this->status = 'executing';
49                 $this->executed_at = now();
50                 $this->save();
51         }
52
53         private function setException(\Throwable $e) {
54                 $this->status = 'exception';
55                 $this->result = [
56                         'type' => get_class($e),
57                         'file' => $e->getFile(),
58                         'line' => $e->getLine(),
59                         'message' => $e->getMessage(),
60                 ];
61                 $this->save();
62         }
63
64
65         protected $casts = [
66                 'parameters' => 'array',
67                 'result' => 'array',
68                 'executed_at' => 'datetime',
69         ];
70
71 }