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