]> git.localhorst.tv Git - alttp.git/commitdiff
discord bot presence command
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 29 Apr 2022 17:36:12 +0000 (19:36 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 29 Apr 2022 17:36:12 +0000 (19:36 +0200)
app/Console/Commands/DiscordBotCommand.php
app/Models/DiscordBotCommand.php [new file with mode: 0644]
database/migrations/2022_04_29_154903_create_discord_bot_commands_table.php [new file with mode: 0644]

index c0a92b885867161acc1ed350574a5b32de070972..4368bb5b1d4b06d2bc815c74ff35d470cf708d95 100644 (file)
@@ -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 (file)
index 0000000..aec2f56
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+namespace App\Models;
+
+use Discord\Discord;
+use Discord\Parts\User\Activity;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class DiscordBotCommand extends Model
+{
+       use HasFactory;
+
+       public function execute(Discord $discord) {
+               $this->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 (file)
index 0000000..388240e
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        *
+        * @return void
+        */
+       public function up()
+       {
+               Schema::create('discord_bot_commands', function (Blueprint $table) {
+                       $table->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');
+       }
+};