5 use App\TwitchBot\TwitchBot;
6 use App\TwitchBotCommands\BaseCommand;
7 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 use Illuminate\Database\Eloquent\Model;
10 class TwitchBotCommand extends Model
14 public static function adlibChat(Channel $channel, User $user = null) {
15 $cmd = new TwitchBotCommand();
16 $cmd->command = 'adlib-chat';
18 'channel' => $channel->id,
20 $cmd->status = 'pending';
21 $cmd->user()->associate($user);
22 $cmd->bot_nick = 'horstiebot';
26 public static function chat($channel, $text, User $user = null, $nick = 'localhorsttv') {
27 $cmd = new TwitchBotCommand();
28 $cmd->command = 'chat';
30 'channel' => $channel,
33 $cmd->status = 'pending';
34 $cmd->user()->associate($user);
35 $cmd->bot_nick = $nick;
39 public static function join($channel, User $user = null, $nick = 'localhorsttv') {
40 $cmd = new TwitchBotCommand();
41 $cmd->command = 'join';
43 'channel' => $channel,
45 $cmd->status = 'pending';
46 $cmd->user()->associate($user);
47 $cmd->bot_nick = $nick;
51 public static function part($channel, User $user = null, $nick = 'localhorsttv') {
52 $cmd = new TwitchBotCommand();
53 $cmd->command = 'part';
55 'channel' => $channel,
57 $cmd->status = 'pending';
58 $cmd->user()->associate($user);
59 $cmd->bot_nick = $nick;
63 public static function randomChat(Channel $channel, $category, User $user = null, $nick = 'localhorsttv') {
64 $cmd = new TwitchBotCommand();
65 $cmd->command = 'random-chat';
67 'channel' => $channel->id,
68 'category' => $category,
70 $cmd->status = 'pending';
71 $cmd->user()->associate($user);
72 $cmd->bot_nick = $nick;
76 public function tournament() {
77 return $this->belongsTo(Tournament::class);
80 public function user() {
81 return $this->belongsTo(User::class);
84 public function execute(TwitchBot $bot) {
85 $this->setExecuting();
88 BaseCommand::resolve($bot, $this)
90 ->otherwise(function (\Throwable $e) {
91 $this->setException($e);
93 ->done(function($v = null) {
96 } catch (\Exception $e) {
97 $this->setException($e);
102 private function setDone() {
103 $this->status = 'done';
107 private function setExecuting() {
108 $this->status = 'executing';
109 $this->executed_at = now();
113 private function setException(\Throwable $e) {
114 $this->status = 'exception';
116 'type' => get_class($e),
117 'file' => $e->getFile(),
118 'line' => $e->getLine(),
119 'message' => $e->getMessage(),
126 'parameters' => 'array',
128 'executed_at' => 'datetime',