From a058ba02c473649714add36edd021faab7a96e4c Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 15 Apr 2022 12:15:01 +0200 Subject: [PATCH] sync discord guilds --- app/Console/Commands/DiscordBotCommand.php | 8 +++++ app/Models/DiscordGuild.php | 28 +++++++++++++++ ..._15_093538_create_discord_guilds_table.php | 35 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 app/Models/DiscordGuild.php create mode 100644 database/migrations/2022_04_15_093538_create_discord_guilds_table.php diff --git a/app/Console/Commands/DiscordBotCommand.php b/app/Console/Commands/DiscordBotCommand.php index d2cc73e..a049eba 100644 --- a/app/Console/Commands/DiscordBotCommand.php +++ b/app/Console/Commands/DiscordBotCommand.php @@ -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 index 0000000..3e91a4e --- /dev/null +++ b/app/Models/DiscordGuild.php @@ -0,0 +1,28 @@ + $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 index 0000000..b50c5f4 --- /dev/null +++ b/database/migrations/2022_04_15_093538_create_discord_guilds_table.php @@ -0,0 +1,35 @@ +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'); + } +}; -- 2.39.2