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