]> git.localhorst.tv Git - alttp.git/commitdiff
sync discord guilds
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 15 Apr 2022 10:15:01 +0000 (12:15 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 15 Apr 2022 10:15:01 +0000 (12:15 +0200)
app/Console/Commands/DiscordBotCommand.php
app/Models/DiscordGuild.php [new file with mode: 0644]
database/migrations/2022_04_15_093538_create_discord_guilds_table.php [new file with mode: 0644]

index d2cc73e4def81fab37d50188c1d0cee3c7cf6237..a049eba3bacd4327ee46df2beb5f53b173e1f912 100644 (file)
@@ -2,8 +2,10 @@
 
 namespace App\Console\Commands;
 
+use App\Models\DiscordGuild;
 use Discord\Discord;
 use Discord\Parts\Channel\Message;
+use Discord\Parts\Guild\Guild;
 use Discord\WebSockets\Event;
 use Illuminate\Console\Command;
 use Monolog\Handler\StreamHandler;
@@ -39,6 +41,12 @@ class DiscordBotCommand extends Command
                        'logger' => $logger,
                        'token' => config('discord.token'),
                ]);
+               $discord->on(Event::GUILD_CREATE, function (Guild $guild, Discord $discord) {
+                       DiscordGuild::sync($guild);
+               });
+               $discord->on(Event::GUILD_UPDATE, function (Guild $guild, Discord $discord, Guild $old) {
+                       DiscordGuild::sync($guild);
+               });
                $discord->on('ready', function (Discord $discord) {
                        $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
                                if (!empty($message->guild_id)) return;
diff --git a/app/Models/DiscordGuild.php b/app/Models/DiscordGuild.php
new file mode 100644 (file)
index 0000000..3e91a4e
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Models;
+
+use Discord\Parts\Guild\Guild;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class DiscordGuild extends Model
+{
+
+       use HasFactory;
+
+       public static function sync(Guild $guild) {
+               $model = static::firstOrNew([
+                       'guild_id' => $guild->id,
+               ]);
+               $model->name = $guild->name;
+               $model->icon_hash = $guild->icon_hash;
+               $model->locale = $guild->preferred_locale;
+               $model->save();
+       }
+
+       protected $fillable = [
+               'guild_id',
+       ];
+
+}
diff --git a/database/migrations/2022_04_15_093538_create_discord_guilds_table.php b/database/migrations/2022_04_15_093538_create_discord_guilds_table.php
new file mode 100644 (file)
index 0000000..b50c5f4
--- /dev/null
@@ -0,0 +1,35 @@
+<?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_guilds', function (Blueprint $table) {
+                       $table->id();
+                       $table->string('guild_id')->unique();
+                       $table->string('name');
+                       $table->string('icon_hash');
+                       $table->string('locale');
+                       $table->timestamps();
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        *
+        * @return void
+        */
+       public function down()
+       {
+               Schema::dropIfExists('discord_guilds');
+       }
+};