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