]> git.localhorst.tv Git - alttp.git/commitdiff
event user sub command
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 3 Aug 2025 10:17:48 +0000 (12:17 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 3 Aug 2025 10:17:48 +0000 (12:17 +0200)
app/DiscordAppCommands/EventsCommand.php
app/DiscordAppCommands/EventsUserCommand.php [new file with mode: 0644]
app/DiscordBotCommands/SyncUserCommand.php

index 9a273c20cd1d8298a0dfd9b8454bc65aae433089..049b02b8f1dedc7044b222c5fb6e17afecc7f4df 100644 (file)
@@ -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 (file)
index 0000000..356e186
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+
+namespace App\DiscordAppCommands;
+
+use App\DiscordBotCommands\SyncUserCommand;
+use App\Models\DiscordGuild;
+use App\Models\Technique;
+use Discord\Builders\MessageBuilder;
+use Discord\Discord;
+use Discord\Parts\Interactions\Command\Option;
+use Discord\Parts\Interactions\Interaction;
+use Discord\Parts\User\User as DiscordUser;
+
+class EventsUserCommand {
+
+       public static function create(Discord $discord): Option {
+               $command = new Option($discord);
+               $command->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) {
+       }
+
+}
index dc2f74e5cde85429af012cdb0061fe3460b55f9a..2cb7bf447b35ef56917fe3ccf547b8b1c2eb6d07 100644 (file)
@@ -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;
+       }
+
 }