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 static function randomChat(Channel $channel, $category, User $user = null, $nick = 'localhorsttv') {
52 $cmd = new TwitchBotCommand();
53 $cmd->command = 'random-chat';
55 'channel' => $channel->id,
56 'category' => $category,
58 $cmd->status = 'pending';
59 $cmd->user()->associate($user);
60 $cmd->bot_nick = $nick;
64 public function tournament() {
65 return $this->belongsTo(Tournament::class);
68 public function user() {
69 return $this->belongsTo(User::class);
72 public function execute(TwitchBot $bot) {
73 $this->setExecuting();
76 BaseCommand::resolve($bot, $this)
78 ->otherwise(function (\Throwable $e) {
79 $this->setException($e);
81 ->done(function($v = null) {
84 } catch (\Exception $e) {
85 $this->setException($e);
90 private function setDone() {
91 $this->status = 'done';
95 private function setExecuting() {
96 $this->status = 'executing';
97 $this->executed_at = now();
101 private function setException(\Throwable $e) {
102 $this->status = 'exception';
104 'type' => get_class($e),
105 'file' => $e->getFile(),
106 'line' => $e->getLine(),
107 'message' => $e->getMessage(),
114 'parameters' => 'array',
116 'executed_at' => 'datetime',