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 chat($channel, $text, User $user = null) {
15 $cmd = new TwitchBotCommand();
16 $cmd->command = 'chat';
18 'channel' => $channel,
21 $cmd->status = 'pending';
22 $cmd->user()->associate($user);
26 public static function join($channel, User $user = null) {
27 $cmd = new TwitchBotCommand();
28 $cmd->command = 'join';
30 'channel' => $channel,
32 $cmd->status = 'pending';
33 $cmd->user()->associate($user);
37 public static function part($channel, User $user = null) {
38 $cmd = new TwitchBotCommand();
39 $cmd->command = 'part';
41 'channel' => $channel,
43 $cmd->status = 'pending';
44 $cmd->user()->associate($user);
48 public function tournament() {
49 return $this->belongsTo(Tournament::class);
52 public function user() {
53 return $this->belongsTo(User::class);
56 public function execute(TwitchBot $bot) {
57 $this->setExecuting();
60 BaseCommand::resolve($bot, $this)
62 ->otherwise(function (\Throwable $e) {
63 $this->setException($e);
65 ->done(function($v = null) {
68 } catch (\Exception $e) {
69 $this->setException($e);
74 private function setDone() {
75 $this->status = 'done';
79 private function setExecuting() {
80 $this->status = 'executing';
81 $this->executed_at = now();
85 private function setException(\Throwable $e) {
86 $this->status = 'exception';
88 'type' => get_class($e),
89 'file' => $e->getFile(),
90 'line' => $e->getLine(),
91 'message' => $e->getMessage(),
98 'parameters' => 'array',
100 'executed_at' => 'datetime',