]> git.localhorst.tv Git - alttp.git/blob - app/Models/DiscordBotCommand.php
log who sent manual random chats
[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 static function queueResult(Result $result) {
17                 $cmd = new DiscordBotCommand();
18                 $cmd->tournament_id = $result->round->tournament_id;
19                 $cmd->command = 'result';
20                 $cmd->parameters = [
21                         'round' => $result->round_id,
22                         'user' => $result->user_id,
23                 ];
24                 $cmd->status = 'pending';
25                 $cmd->save();
26         }
27
28         public static function syncUser($user_id) {
29                 $cmd = new DiscordBotCommand();
30                 $cmd->command = 'sync-user';
31                 $cmd->parameters = [
32                         'user' => $user_id,
33                 ];
34                 $cmd->status = 'pending';
35                 $cmd->save();
36         }
37
38         public function tournament() {
39                 return $this->belongsTo(Tournament::class);
40         }
41
42         public function user() {
43                 return $this->belongsTo(User::class);
44         }
45
46         public function execute(Discord $discord) {
47                 $this->setExecuting();
48
49                 try {
50                         BaseCommand::resolve($discord, $this)
51                                 ->execute()
52                                 ->otherwise(function (\Throwable $e) {
53                                         $this->setException($e);
54                                 })
55                                 ->done(function($v = null) {
56                                         $this->setDone();
57                                 });
58                 } catch (\Exception $e) {
59                         $this->setException($e);
60                 }
61         }
62
63
64         private function setDone() {
65                 $this->status = 'done';
66                 $this->save();
67         }
68
69         private function setExecuting() {
70                 $this->status = 'executing';
71                 $this->executed_at = now();
72                 $this->save();
73         }
74
75         private function setException(\Throwable $e) {
76                 $this->status = 'exception';
77                 $this->result = [
78                         'type' => get_class($e),
79                         'file' => $e->getFile(),
80                         'line' => $e->getLine(),
81                         'message' => $e->getMessage(),
82                 ];
83                 $this->save();
84         }
85
86
87         protected $casts = [
88                 'parameters' => 'array',
89                 'result' => 'array',
90                 'executed_at' => 'datetime',
91         ];
92
93 }