From: Daniel Karbach Date: Fri, 29 Aug 2025 20:08:19 +0000 (+0200) Subject: notify channel crew on episode crew signup X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=ccf3cd4b451e89b8fac054766f75765e456112f7;p=alttp.git notify channel crew on episode crew signup --- diff --git a/app/DiscordBotCommands/BaseCommand.php b/app/DiscordBotCommands/BaseCommand.php index 5e1c5e6..af647ce 100644 --- a/app/DiscordBotCommands/BaseCommand.php +++ b/app/DiscordBotCommands/BaseCommand.php @@ -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 index 0000000..086c64c --- /dev/null +++ b/app/DiscordBotCommands/CrewSignupNotifyCommand.php @@ -0,0 +1,29 @@ +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().')' + .' (episode->start)->timestamp.':F>)'; + return $user->sendMessage($text); + }); + } + +} diff --git a/app/Http/Controllers/EpisodeController.php b/app/Http/Controllers/EpisodeController.php index 97d2aad..b9bbb5c 100644 --- a/app/Http/Controllers/EpisodeController.php +++ b/app/Http/Controllers/EpisodeController.php @@ -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 { diff --git a/app/Models/ChannelCrew.php b/app/Models/ChannelCrew.php index 0f78eb0..3a916d1 100644 --- a/app/Models/ChannelCrew.php +++ b/app/Models/ChannelCrew.php @@ -17,6 +17,11 @@ class ChannelCrew extends Model return $this->belongsTo(User::class); } + + public function wantsSignupNotifications() { + return true; + } + protected $casts = [ 'user_id' => 'string', ]; diff --git a/app/Models/DiscordBotCommand.php b/app/Models/DiscordBotCommand.php index aa54937..b9dd3bc 100644 --- a/app/Models/DiscordBotCommand.php +++ b/app/Models/DiscordBotCommand.php @@ -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);