namespace App\Console\Commands;
+use App\Models\DiscordChannel;
use App\Models\DiscordGuild;
use App\Models\DiscordRole;
use Discord\Discord;
+use Discord\Parts\Channel\Channel;
use Discord\Parts\Channel\Message;
use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Role;
$this->error('guild update: '.$e->getMessage());
}
});
+ $discord->on(Event::CHANNEL_CREATE, function (Channel $channel, Discord $discord) {
+ try {
+ DiscordGuild::onUpstreamCreate($channel);
+ } catch (\Exception $e) {
+ $this->error('channel create: '.$e->getMessage());
+ }
+ });
+ $discord->on(Event::CHANNEL_UPDATE, function (Channel $channel, Discord $discord, ?Channel $old) {
+ try {
+ DiscordGuild::onUpstreamUpdate($channel);
+ } catch (\Exception $e) {
+ $this->error('channel update: '.$e->getMessage());
+ }
+ });
+ $discord->on(Event::CHANNEL_DELETE, function ($channel, Discord $discord) {
+ try {
+ DiscordGuild::onUpstreamDelete($channel);
+ } catch (\Exception $e) {
+ $this->error('channel delete: '.$e->getMessage());
+ }
+ });
$discord->on(Event::GUILD_ROLE_CREATE, function (Role $role, Discord $discord) {
try {
DiscordRole::onUpstreamCreate($role);
$this->error('guild role create: '.$e->getMessage());
}
});
- $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discordi, ?Role $old) {
+ $discord->on(Event::GUILD_ROLE_UPDATE, function (Role $role, Discord $discord, ?Role $old) {
try {
DiscordRole::onUpstreamUpdate($role);
} catch (\Exception $e) {
--- /dev/null
+<?php
+
+namespace App\Models;
+
+use Discord\Parts\Channel\Channel;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class DiscordChannel extends Model
+{
+ use HasFactory;
+
+ public static function onUpstreamCreate(Channel $channel) {
+ $guild = DiscordGuild::firstWhere('guild_id', $channel->guild_id);
+ if (!$guild) return;
+ static::sync($guild, $channel);
+ }
+
+ public static function onUpstreamUpdate(Channel $channel) {
+ $guild = DiscordGuild::firstWhere('guild_id', $channel->guild_id);
+ if (!$guild) return;
+ static::sync($guild, $channel);
+ }
+
+ public static function onUpstreamDelete($channel) {
+ if (!$channel) return;
+ $model = static::firstWhere('channel_id', $channel->id);
+ if (!$model) return;
+ $model->delete();
+ }
+
+ public static function sync(DiscordGuild $guild, Channel $channel) {
+ $model = static::firstOrNew([
+ 'channel_id' => $channel->id,
+ 'discord_guild_id' => $guild->id,
+ ]);
+ $model->name = $channel->name;
+ $model->type = $channel->type;
+ $model->position = $channel->position;
+ $model->private = $channel->is_private;
+ $model->save();
+ }
+
+ public function guild() {
+ return $this->belongsTo(DiscordGuild::class);
+ }
+
+ protected $fillable = [
+ 'channel_id',
+ 'discord_guild_id',
+ ];
+
+}
$role_ids[] = $role->id;
}
$model->roles()->whereNotIn('role_id', $role_ids)->delete();
+
+ $channel_ids = [];
+ foreach ($guild->channels as $channel) {
+ DiscordChannel::sync($model, $channel);
+ $channel_ids[] = $channel->id;
+ }
+ $model->channels()->whereNotIn('channel_id', $channel_ids)->delete();
+ }
+
+ public function channels() {
+ return $this->hasMany(DiscordChannel::class);
}
public function roles() {
--- /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_channels', function (Blueprint $table) {
+ $table->id();
+ $table->foreignId('discord_guild_id')->nullable()->default(null)->constrained();
+ $table->string('channel_id')->unique();
+ $table->string('name');
+ $table->integer('type');
+ $table->integer('position');
+ $table->boolean('private');
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('discord_channels');
+ }
+};