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;
'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;
--- /dev/null
+<?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',
+ ];
+
+}
--- /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_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');
+ }
+};