]> git.localhorst.tv Git - alttp.git/commitdiff
notify channel crew on episode crew signup
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 29 Aug 2025 20:08:19 +0000 (22:08 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 29 Aug 2025 20:08:19 +0000 (22:08 +0200)
app/DiscordBotCommands/BaseCommand.php
app/DiscordBotCommands/CrewSignupNotifyCommand.php [new file with mode: 0644]
app/Http/Controllers/EpisodeController.php
app/Models/ChannelCrew.php
app/Models/DiscordBotCommand.php

index 5e1c5e6ef48be5f144b3a49616bf925f3695a02d..af647ce7dd76ac7b77f4446215caa3f8be5a3974 100644 (file)
@@ -2,9 +2,11 @@
 
 namespace App\DiscordBotCommands;
 
+use App\Models\ChannelCrew;
 use App\Models\DiscordBotCommand;
 use App\Models\DiscordGuild;
 use App\Models\Episode;
+use App\Models\EpisodeCrew;
 use App\Models\Round;
 use App\Models\User;
 use Discord\Discord;
@@ -21,6 +23,8 @@ abstract class BaseCommand {
                switch ($cmd->command) {
                        case 'cancel-event':
                                return new CancelEventCommand($discord, $cmd);
+                       case 'crew-signup-notify':
+                               return new CrewSignupNotifyCommand($discord, $cmd);
                        case 'episode-event':
                                return new EpisodeEventCommand($discord, $cmd);
                        case 'message':
@@ -142,11 +146,18 @@ abstract class BaseCommand {
                return $this->command->parameters[$name];
        }
 
+       protected function getCrew(): EpisodeCrew {
+               if (!$this->hasParameter('crew')) {
+                       throw new \Exception('no crew in parameters');
+               }
+               return EpisodeCrew::query()->findOrFail($this->getParameter('crew'));
+       }
+
        protected function getEpisode(): Episode {
                if (!$this->hasParameter('episode')) {
                        throw new \Exception('no episode in parameters');
                }
-               return Episode::findOrFail($this->getParameter('episode'));
+               return Episode::query()->findOrFail($this->getParameter('episode'));
        }
 
        protected function getRound(): Round {
diff --git a/app/DiscordBotCommands/CrewSignupNotifyCommand.php b/app/DiscordBotCommands/CrewSignupNotifyCommand.php
new file mode 100644 (file)
index 0000000..086c64c
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\DiscordBotCommands;
+
+use App\Models\DiscordBotCommand;
+use Carbon\Carbon;
+use Discord\Discord;
+use Discord\Parts\User\User;
+use React\Promise\PromiseInterface;
+
+class CrewSignupNotifyCommand extends BaseCommand {
+
+       public function __construct(Discord $discord, DiscordBotCommand $cmd) {
+               parent::__construct($discord, $cmd);
+       }
+
+       public function execute(): PromiseInterface {
+               return $this->fetchUser()
+                       ->then(function (User $user) {
+                               $crew = $this->getCrew();
+                               // TODO: l10n
+                               $text = '<@'.$crew->user_id.'> signed up for '.$crew->role.' on '
+                                       .'['.$crew->episode->getScheduledEventName().']('.$crew->episode->event->getURL().')'
+                                       .' (<t:'.Carbon::parse($crew->episode->start)->timestamp.':F>)';
+                               return $user->sendMessage($text);
+                       });
+       }
+
+}
index 97d2aad5bad170f606392f832d18bd843bd1a2c3..b9bbb5c1d115056e5700058e60067134e7d4f8b5 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use App\Models\Channel;
+use App\Models\DiscordBotCommand;
 use App\Models\Event;
 use App\Models\Episode;
 use App\Models\EpisodeCrew;
@@ -158,12 +159,18 @@ class EpisodeController extends Controller {
                        }
                }
 
-               $episode->crew()->create([
+               $crew = $episode->crew()->create([
                        'channel_id' => $channel->id,
                        'role' => $as,
                        'user_id' => $user->id,
                ]);
 
+               foreach ($channel->crews as $channel_crew) {
+                       if ($channel_crew->wantsSignupNotifications()) {
+                               DiscordBotCommand::crewSignupNotify($channel_crew->user, $crew);
+                       }
+               }
+
                if ($user->isPrivileged()) {
                        return $episode->load(['crew', 'crew.user'])->toJson();
                } else {
index 0f78eb0a8edc109a0e7f61df2ec6921da0cedd0c..3a916d1671bb000f931ff84bf3d30304d970ced0 100644 (file)
@@ -17,6 +17,11 @@ class ChannelCrew extends Model
                return $this->belongsTo(User::class);
        }
 
+
+       public function wantsSignupNotifications() {
+               return true;
+       }
+
        protected $casts = [
                'user_id' => 'string',
        ];
index aa549379a163f4d7f3af61982db90ba5f208878b..b9dd3bc2dae95f8ae509068970faf7acbd9c9fef 100644 (file)
@@ -38,6 +38,18 @@ class DiscordBotCommand extends Model {
                return $cmd;
        }
 
+       public static function crewSignupNotify(User $user, EpisodeCrew $crew): DiscordBotCommand {
+               $cmd = new DiscordBotCommand();
+               $cmd->command = 'crew-signup-notify';
+               $cmd->parameters = [
+                       'crew' => $crew->id,
+                       'user' => $user->id,
+               ];
+               $cmd->status = 'pending';
+               $cmd->save();
+               return $cmd;
+       }
+
        public static function episodeEvent(DiscordGuild $guild, Episode $episode): DiscordBotCommand {
                $cmd = new DiscordBotCommand();
                $cmd->discord_guild()->associate($guild);