From: Daniel Karbach Date: Fri, 29 Apr 2022 17:36:12 +0000 (+0200) Subject: discord bot presence command X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=12f64085fd212a9744db01790cefad53c970e8d9;p=alttp.git discord bot presence command --- diff --git a/app/Console/Commands/DiscordBotCommand.php b/app/Console/Commands/DiscordBotCommand.php index c0a92b8..4368bb5 100644 --- a/app/Console/Commands/DiscordBotCommand.php +++ b/app/Console/Commands/DiscordBotCommand.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Models\DiscordBotCommand as CommandModel; use App\Models\DiscordChannel; use App\Models\DiscordGuild; use App\Models\DiscordRole; @@ -52,6 +53,16 @@ class DiscordBotCommand extends Command $activity->name = 'HolySmoke'; $activity->url = 'https://alttp.localhorst.tv/'; $discord->updatePresence($activity); + + $discord->getLoop()->addPeriodicTimer(1, function () use ($discord) { + $command = CommandModel::where('status', '=', 'pending')->oldest()->first(); + if ($command) { + try { + $command->execute($discord); + } catch (\Exception $e) { + } + } + }); }); $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) { try { diff --git a/app/Models/DiscordBotCommand.php b/app/Models/DiscordBotCommand.php new file mode 100644 index 0000000..aec2f56 --- /dev/null +++ b/app/Models/DiscordBotCommand.php @@ -0,0 +1,72 @@ +status = 'executing'; + $this->executed_at = now(); + $this->save(); + + try { + switch ($this->command) { + case 'presence': + $this->executePresence($discord); + break; + default: + throw new Exception('unrecognized command'); + } + $this->status = 'done'; + $this->save(); + } catch (\Exception $e) { + $this->status = 'exception'; + $this->result = [ + 'type' => get_class($e), + 'message' => $e->getMessage(), + ]; + $this->save(); + } + } + + protected function executePresence(Discord $discord) { + $activity = null; + $idle = false; + $status = 'online'; + $afk = false; + if (isset($this->parameters['activity'])) { + $activity = new Activity($discord); + $activity->type = $this->parameters['activity']; + if (isset($this->parameters['name'])) { + $activity->name = $this->parameters['name']; + } + if (isset($this->parameters['url'])) { + $activity->url = $this->parameters['url']; + } + } + if (isset($this->parameters['idle'])) { + $idle = $this->parameters['idle']; + } + if (isset($this->parameters['status'])) { + $status = $this->parameters['status']; + } + if (isset($this->parameters['afk'])) { + $afk = $this->parameters['afk']; + } + $discord->updatePresence($activity, $idle, $status, $afk); + } + + protected $casts = [ + 'parameters' => 'array', + 'result' => 'array', + 'executed_at' => 'datetime', + ]; + +} diff --git a/database/migrations/2022_04_29_154903_create_discord_bot_commands_table.php b/database/migrations/2022_04_29_154903_create_discord_bot_commands_table.php new file mode 100644 index 0000000..388240e --- /dev/null +++ b/database/migrations/2022_04_29_154903_create_discord_bot_commands_table.php @@ -0,0 +1,38 @@ +id(); + $table->foreignId('tournament_id')->nullable()->constrained(); + $table->foreignId('user_id')->nullable()->constrained(); + $table->string('command'); + $table->text('parameters')->nullable()->default(null); + $table->string('status')->default('hold'); + $table->text('result')->nullable()->default(null); + $table->timestamp('executed_at')->nullable()->default(null); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('discord_bot_commands'); + } +};