]> git.localhorst.tv Git - alttp.git/blob - app/Models/TwitchBotCommand.php
associate user with twitch command
[alttp.git] / app / Models / TwitchBotCommand.php
1 <?php
2
3 namespace App\Models;
4
5 use App\TwitchBot\TwitchBot;
6 use App\TwitchBotCommands\BaseCommand;
7 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 use Illuminate\Database\Eloquent\Model;
9
10 class TwitchBotCommand extends Model
11 {
12         use HasFactory;
13
14         public static function join($channel, User $user = null) {
15                 $cmd = new TwitchBotCommand();
16                 $cmd->command = 'join';
17                 $cmd->parameters = [
18                         'channel' => $channel,
19                 ];
20                 $cmd->status = 'pending';
21                 $cmd->user()->associate($user);
22                 $cmd->save();
23         }
24
25         public static function part($channel, User $user = null) {
26                 $cmd = new TwitchBotCommand();
27                 $cmd->command = 'part';
28                 $cmd->parameters = [
29                         'channel' => $channel,
30                 ];
31                 $cmd->status = 'pending';
32                 $cmd->user()->associate($user);
33                 $cmd->save();
34         }
35
36         public function tournament() {
37                 return $this->belongsTo(Tournament::class);
38         }
39
40         public function user() {
41                 return $this->belongsTo(User::class);
42         }
43
44         public function execute(TwitchBot $bot) {
45                 $this->setExecuting();
46
47                 try {
48                         BaseCommand::resolve($bot, $this)
49                                 ->execute()
50                                 ->otherwise(function (\Throwable $e) {
51                                         $this->setException($e);
52                                 })
53                                 ->done(function($v = null) {
54                                         $this->setDone();
55                                 });
56                 } catch (\Exception $e) {
57                         $this->setException($e);
58                 }
59         }
60
61
62         private function setDone() {
63                 $this->status = 'done';
64                 $this->save();
65         }
66
67         private function setExecuting() {
68                 $this->status = 'executing';
69                 $this->executed_at = now();
70                 $this->save();
71         }
72
73         private function setException(\Throwable $e) {
74                 $this->status = 'exception';
75                 $this->result = [
76                         'type' => get_class($e),
77                         'file' => $e->getFile(),
78                         'line' => $e->getLine(),
79                         'message' => $e->getMessage(),
80                 ];
81                 $this->save();
82         }
83
84
85         protected $casts = [
86                 'parameters' => 'array',
87                 'result' => 'array',
88                 'executed_at' => 'datetime',
89         ];
90
91 }