From 52a17ded5fff86012138bf19e9368745e20b990d Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 3 Aug 2025 12:17:48 +0200 Subject: [PATCH] event user sub command --- app/DiscordAppCommands/EventsCommand.php | 2 + app/DiscordAppCommands/EventsUserCommand.php | 105 +++++++++++++++++++ app/DiscordBotCommands/SyncUserCommand.php | 65 ++++++------ 3 files changed, 142 insertions(+), 30 deletions(-) create mode 100644 app/DiscordAppCommands/EventsUserCommand.php diff --git a/app/DiscordAppCommands/EventsCommand.php b/app/DiscordAppCommands/EventsCommand.php index 9a273c2..049b02b 100644 --- a/app/DiscordAppCommands/EventsCommand.php +++ b/app/DiscordAppCommands/EventsCommand.php @@ -15,6 +15,7 @@ class EventsCommand { $command->setDmPermission(false); $command->setDefaultMemberPermissions("0"); $command->addOption(EventsShowCommand::create($discord)); + $command->addOption(EventsUserCommand::create($discord)); $discord->application->commands->save( $discord->application->commands->create($command->toArray()) ); @@ -22,6 +23,7 @@ class EventsCommand { public static function listen(Discord $discord): void { EventsShowCommand::listen('events', $discord); + EventsUserCommand::listen('events', $discord); } } diff --git a/app/DiscordAppCommands/EventsUserCommand.php b/app/DiscordAppCommands/EventsUserCommand.php new file mode 100644 index 0000000..356e186 --- /dev/null +++ b/app/DiscordAppCommands/EventsUserCommand.php @@ -0,0 +1,105 @@ +setType(Option::SUB_COMMAND_GROUP); + $command->setName('user'); + $command->setDescription('Manage your server\'s user subscriptions on alttp.localhorst.tv'); + $subcom = new Option($discord); + $subcom->setType(Option::SUB_COMMAND); + $subcom->setName('add'); + $subcom->setDescription('Subscribe to a user'); + $option = new Option($discord); + $option->setType(Option::USER); + $option->setName('user'); + $option->setDescription('The user to subscribe to'); + $option->setRequired(true); + $subcom->addOption($option); + $command->addOption($subcom); + $subcom = new Option($discord); + $subcom->setType(Option::SUB_COMMAND); + $subcom->setName('remove'); + $subcom->setDescription('Unsubscribe from a user'); + $option = new Option($discord); + $option->setType(Option::USER); + $option->setName('user'); + $option->setDescription('The user to unsubscribe from'); + $option->setRequired(true); + $subcom->addOption($option); + $command->addOption($subcom); + return $command; + } + + public static function listen(string $parent, Discord $discord): void { + $discord->listenCommand( + [$parent, 'user'], + function (Interaction $interaction) use ($discord) { + $interaction + ->acknowledgeWithResponse() + ->done(function () use ($discord, $interaction) { + try { + // TODO: translation + $lang = Technique::locale2lang($interaction->locale); + $guild = DiscordGuild::query()->where('guild_id', '=', $interaction->guild_id)->firstOrFail(); + $mode = isset($interaction->data->options['user']->options['add']) ? 'add' : 'remove'; + $uid = $interaction->data->options['user']->options[$mode]->options['user']->value; + $discord->users->fetch($uid)->then( + function (DiscordUser $discordUser) use ($guild, $interaction, $mode) { + $user = SyncUserCommand::syncUserFromDiscordUser($discordUser); + if ($guild->user_subscriptions()->where('user_id', '=', $user->id)->count()) { + if ($mode == 'remove') { + $guild->user_subscriptions()->where('user_id', '=', $user->id)->delete(); + $message = MessageBuilder::new(); + $message->setContent('User unsubscribed'); + $interaction->updateOriginalResponse($message); + } else { + $message = MessageBuilder::new(); + $message->setContent('User is already subscribed'); + $interaction->updateOriginalResponse($message); + } + } else { + if ($mode == 'remove') { + $message = MessageBuilder::new(); + $message->setContent('User was not subscribed to begin with'); + $interaction->updateOriginalResponse($message); + } else { + $guild->user_subscriptions()->create(['user_id' => $user->id]); + $message = MessageBuilder::new(); + $message->setContent('User subscribed'); + $interaction->updateOriginalResponse($message); + } + } + }, + function (\Throwable $e) use ($interaction) { + $message = MessageBuilder::new(); + $message->setContent('Error: '.$e->getMessage()); + $interaction->updateOriginalResponse($message); + } + ); + } catch (\Throwable $e) { + $message = MessageBuilder::new(); + $message->setContent('Error: '.$e->getMessage()); + $interaction->updateOriginalResponse($message); + } + }); + }, + ); + } + + private static function getUser($uid) { + } + +} diff --git a/app/DiscordBotCommands/SyncUserCommand.php b/app/DiscordBotCommands/SyncUserCommand.php index dc2f74e..2cb7bf4 100644 --- a/app/DiscordBotCommands/SyncUserCommand.php +++ b/app/DiscordBotCommands/SyncUserCommand.php @@ -19,37 +19,42 @@ class SyncUserCommand extends BaseCommand { public function execute(): PromiseInterface { return $this->fetchUser() ->then(function (DiscordUser $discordUser) { - $user = User::find($discordUser->id); - if (!$user) { - $user = new User(); - $user->id = $discordUser->id; - $user->username = $discordUser->username; - $user->discriminator = $discordUser->discriminator; - $user->locale = $discordUser->locale ?: 'en'; - $user->verified = $discordUser->verified ?: false; - $user->mfa_enabled = $discordUser->mfaenabled ?: false; - } - $user->avatar = $discordUser->avatarhash ?: null; - if ($user->username != $discordUser->username) { - $user->username = $discordUser->username; - $user->discriminator = $discordUser->discriminator; - } - - $media = Storage::disk('media'); - if ($user->avatar && $media->missing('avatar/'.$user->id.'/'.$user->avatar.'.png')) { - try { - $url = $discordUser->getAvatarAttribute('png'); - $content = Http::get($url)->body(); - $media->makeDirectory('avatar/'.$user->id); - $media->put('avatar/'.$user->id.'/'.$user->avatar.'.png', $content); - $user->avatar_cached = now(); - } catch (\Exception $e) { - } - } else { - $user->avatar_cached = now(); - } - $user->save(); + static::syncUserFromDiscordUser($discordUser); }); } + public static function syncUserFromDiscordUser($discordUser): User { + $user = User::find($discordUser->id); + if (!$user) { + $user = new User(); + $user->id = $discordUser->id; + $user->username = $discordUser->username; + $user->discriminator = $discordUser->discriminator; + $user->locale = $discordUser->locale ?: 'en'; + $user->verified = $discordUser->verified ?: false; + $user->mfa_enabled = $discordUser->mfaenabled ?: false; + } + $user->avatar = $discordUser->avatarhash ?: null; + if ($user->username != $discordUser->username) { + $user->username = $discordUser->username; + $user->discriminator = $discordUser->discriminator; + } + + $media = Storage::disk('media'); + if ($user->avatar && $media->missing('avatar/'.$user->id.'/'.$user->avatar.'.png')) { + try { + $url = $discordUser->getAvatarAttribute('png'); + $content = Http::get($url)->body(); + $media->makeDirectory('avatar/'.$user->id); + $media->put('avatar/'.$user->id.'/'.$user->avatar.'.png', $content); + $user->avatar_cached = now(); + } catch (\Exception $e) { + } + } else { + $user->avatar_cached = now(); + } + $user->save(); + return $user; + } + } -- 2.47.2