]> git.localhorst.tv Git - alttp.git/blob - app/Models/TwitchBotCommand.php
fix number and space normalization
[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 adlibChat(Channel $channel, User $user = null) {
15                 $cmd = new TwitchBotCommand();
16                 $cmd->command = 'adlib-chat';
17                 $cmd->parameters = [
18                         'channel' => $channel->id,
19                 ];
20                 $cmd->status = 'pending';
21                 $cmd->user()->associate($user);
22                 $cmd->bot_nick = 'horstiebot';
23                 $cmd->save();
24         }
25
26         public static function chat($channel, $text, User $user = null, $nick = 'localhorsttv') {
27                 $cmd = new TwitchBotCommand();
28                 $cmd->command = 'chat';
29                 $cmd->parameters = [
30                         'channel' => $channel,
31                         'text' => $text,
32                 ];
33                 $cmd->status = 'pending';
34                 $cmd->user()->associate($user);
35                 $cmd->bot_nick = $nick;
36                 $cmd->save();
37         }
38
39         public static function join($channel, User $user = null, $nick = 'localhorsttv') {
40                 $cmd = new TwitchBotCommand();
41                 $cmd->command = 'join';
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 static function part($channel, User $user = null, $nick = 'localhorsttv') {
52                 $cmd = new TwitchBotCommand();
53                 $cmd->command = 'part';
54                 $cmd->parameters = [
55                         'channel' => $channel,
56                 ];
57                 $cmd->status = 'pending';
58                 $cmd->user()->associate($user);
59                 $cmd->bot_nick = $nick;
60                 $cmd->save();
61         }
62
63         public static function randomChat(Channel $channel, $category, User $user = null, $nick = 'localhorsttv') {
64                 $cmd = new TwitchBotCommand();
65                 $cmd->command = 'random-chat';
66                 $cmd->parameters = [
67                         'channel' => $channel->id,
68                         'category' => $category,
69                 ];
70                 $cmd->status = 'pending';
71                 $cmd->user()->associate($user);
72                 $cmd->bot_nick = $nick;
73                 $cmd->save();
74         }
75
76         public function tournament() {
77                 return $this->belongsTo(Tournament::class);
78         }
79
80         public function user() {
81                 return $this->belongsTo(User::class);
82         }
83
84         public function execute(TwitchBot $bot) {
85                 $this->setExecuting();
86
87                 try {
88                         BaseCommand::resolve($bot, $this)
89                                 ->execute()
90                                 ->otherwise(function (\Throwable $e) {
91                                         $this->setException($e);
92                                 })
93                                 ->done(function($v = null) {
94                                         $this->setDone();
95                                 });
96                 } catch (\Exception $e) {
97                         $this->setException($e);
98                 }
99         }
100
101
102         private function setDone() {
103                 $this->status = 'done';
104                 $this->save();
105         }
106
107         private function setExecuting() {
108                 $this->status = 'executing';
109                 $this->executed_at = now();
110                 $this->save();
111         }
112
113         private function setException(\Throwable $e) {
114                 $this->status = 'exception';
115                 $this->result = [
116                         'type' => get_class($e),
117                         'file' => $e->getFile(),
118                         'line' => $e->getLine(),
119                         'message' => $e->getMessage(),
120                 ];
121                 $this->save();
122         }
123
124
125         protected $casts = [
126                 'parameters' => 'array',
127                 'result' => 'array',
128                 'executed_at' => 'datetime',
129         ];
130
131 }