]> git.localhorst.tv Git - alttp.git/blob - app/Models/TwitchBotCommand.php
slightly improved message generation
[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 static function randomChat(Channel $channel, $category, User $user = null, $nick = 'localhorsttv') {
52                 $cmd = new TwitchBotCommand();
53                 $cmd->command = 'random-chat';
54                 $cmd->parameters = [
55                         'channel' => $channel->id,
56                         'category' => $category,
57                 ];
58                 $cmd->status = 'pending';
59                 $cmd->user()->associate($user);
60                 $cmd->bot_nick = $nick;
61                 $cmd->save();
62         }
63
64         public function tournament() {
65                 return $this->belongsTo(Tournament::class);
66         }
67
68         public function user() {
69                 return $this->belongsTo(User::class);
70         }
71
72         public function execute(TwitchBot $bot) {
73                 $this->setExecuting();
74
75                 try {
76                         BaseCommand::resolve($bot, $this)
77                                 ->execute()
78                                 ->otherwise(function (\Throwable $e) {
79                                         $this->setException($e);
80                                 })
81                                 ->done(function($v = null) {
82                                         $this->setDone();
83                                 });
84                 } catch (\Exception $e) {
85                         $this->setException($e);
86                 }
87         }
88
89
90         private function setDone() {
91                 $this->status = 'done';
92                 $this->save();
93         }
94
95         private function setExecuting() {
96                 $this->status = 'executing';
97                 $this->executed_at = now();
98                 $this->save();
99         }
100
101         private function setException(\Throwable $e) {
102                 $this->status = 'exception';
103                 $this->result = [
104                         'type' => get_class($e),
105                         'file' => $e->getFile(),
106                         'line' => $e->getLine(),
107                         'message' => $e->getMessage(),
108                 ];
109                 $this->save();
110         }
111
112
113         protected $casts = [
114                 'parameters' => 'array',
115                 'result' => 'array',
116                 'executed_at' => 'datetime',
117         ];
118
119 }