--- /dev/null
+<?php
+
+namespace App\Models;
+
+use App\TwitchBot\TwitchBot;
+use App\TwitchBotCommands\BaseCommand;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class TwitchBotCommand extends Model
+{
+ use HasFactory;
+
+ public static function join($channel) {
+ $cmd = new TwitchBotCommand();
+ $cmd->command = 'join';
+ $cmd->parameters = [
+ 'channel' => $channel,
+ ];
+ $cmd->status = 'pending';
+ $cmd->save();
+ }
+
+ public static function part($channel) {
+ $cmd = new TwitchBotCommand();
+ $cmd->command = 'part';
+ $cmd->parameters = [
+ 'channel' => $channel,
+ ];
+ $cmd->status = 'pending';
+ $cmd->save();
+ }
+
+ public function tournament() {
+ return $this->belongsTo(Tournament::class);
+ }
+
+ public function user() {
+ return $this->belongsTo(User::class);
+ }
+
+ public function execute(TwitchBot $bot) {
+ $this->setExecuting();
+
+ try {
+ BaseCommand::resolve($bot, $this)
+ ->execute()
+ ->otherwise(function (\Throwable $e) {
+ $this->setException($e);
+ })
+ ->done(function($v = null) {
+ $this->setDone();
+ });
+ } catch (\Exception $e) {
+ $this->setException($e);
+ }
+ }
+
+
+ private function setDone() {
+ $this->status = 'done';
+ $this->save();
+ }
+
+ private function setExecuting() {
+ $this->status = 'executing';
+ $this->executed_at = now();
+ $this->save();
+ }
+
+ private function setException(\Throwable $e) {
+ $this->status = 'exception';
+ $this->result = [
+ 'type' => get_class($e),
+ 'file' => $e->getFile(),
+ 'line' => $e->getLine(),
+ 'message' => $e->getMessage(),
+ ];
+ $this->save();
+ }
+
+
+ protected $casts = [
+ 'parameters' => 'array',
+ 'result' => 'array',
+ 'executed_at' => 'datetime',
+ ];
+
+}