namespace App\Console\Commands;
+use App\Models\DiscordBotCommand as CommandModel;
use App\Models\DiscordChannel;
use App\Models\DiscordGuild;
use App\Models\DiscordRole;
$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 {
--- /dev/null
+<?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',
+ ];
+
+}
--- /dev/null
+<?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');
+ }
+};