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 join($channel, User $user = null) {
15 $cmd = new TwitchBotCommand();
16 $cmd->command = 'join';
18 'channel' => $channel,
20 $cmd->status = 'pending';
21 $cmd->user()->associate($user);
25 public static function part($channel, User $user = null) {
26 $cmd = new TwitchBotCommand();
27 $cmd->command = 'part';
29 'channel' => $channel,
31 $cmd->status = 'pending';
32 $cmd->user()->associate($user);
36 public function tournament() {
37 return $this->belongsTo(Tournament::class);
40 public function user() {
41 return $this->belongsTo(User::class);
44 public function execute(TwitchBot $bot) {
45 $this->setExecuting();
48 BaseCommand::resolve($bot, $this)
50 ->otherwise(function (\Throwable $e) {
51 $this->setException($e);
53 ->done(function($v = null) {
56 } catch (\Exception $e) {
57 $this->setException($e);
62 private function setDone() {
63 $this->status = 'done';
67 private function setExecuting() {
68 $this->status = 'executing';
69 $this->executed_at = now();
73 private function setException(\Throwable $e) {
74 $this->status = 'exception';
76 'type' => get_class($e),
77 'file' => $e->getFile(),
78 'line' => $e->getLine(),
79 'message' => $e->getMessage(),
86 'parameters' => 'array',
88 'executed_at' => 'datetime',