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, $nick = 'localhorsttv') {
15 $cmd = new TwitchBotCommand();
16 $cmd->command = 'chat';
18 'channel' => $channel,
21 $cmd->status = 'pending';
22 $cmd->user()->associate($user);
23 $cmd->bot_nick = $nick;
27 public static function join($channel, User $user = null, $nick = 'localhorsttv') {
28 $cmd = new TwitchBotCommand();
29 $cmd->command = 'join';
31 'channel' => $channel,
33 $cmd->status = 'pending';
34 $cmd->user()->associate($user);
35 $cmd->bot_nick = $nick;
39 public static function part($channel, User $user = null, $nick = 'localhorsttv') {
40 $cmd = new TwitchBotCommand();
41 $cmd->command = 'part';
43 'channel' => $channel,
45 $cmd->status = 'pending';
46 $cmd->user()->associate($user);
47 $cmd->bot_nick = $nick;
51 public function tournament() {
52 return $this->belongsTo(Tournament::class);
55 public function user() {
56 return $this->belongsTo(User::class);
59 public function execute(TwitchBot $bot) {
60 $this->setExecuting();
63 BaseCommand::resolve($bot, $this)
65 ->otherwise(function (\Throwable $e) {
66 $this->setException($e);
68 ->done(function($v = null) {
71 } catch (\Exception $e) {
72 $this->setException($e);
77 private function setDone() {
78 $this->status = 'done';
82 private function setExecuting() {
83 $this->status = 'executing';
84 $this->executed_at = now();
88 private function setException(\Throwable $e) {
89 $this->status = 'exception';
91 'type' => get_class($e),
92 'file' => $e->getFile(),
93 'line' => $e->getLine(),
94 'message' => $e->getMessage(),
101 'parameters' => 'array',
103 'executed_at' => 'datetime',