Skip to content

Commit

Permalink
feat: discord thread action (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Oct 26, 2023
1 parent 016593b commit e3baa82
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ POSTMARK_TOKEN=
DISCORD_BOT_API_TOKEN=
DB_UPDATES_DISCORD_CHANNEL=
ADMIN_DISCORD_CHANNEL=
SUBMISSIONS_DISCORD_CHANNEL=
WINTER_DISCORD_FORUM_TAG=
SPRING_DISCORD_FORUM_TAG=
SUMMER_DISCORD_FORUM_TAG=
FALL_DISCORD_FORUM_TAG=
MAL_CLIENT_ID=null
DO_BEARER_TOKEN=null
OPENAI_BEARER_TOKEN=null
Expand Down
2 changes: 2 additions & 0 deletions app/Constants/Config/ServiceConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ class ServiceConstants

final public const DB_UPDATES_DISCORD_CHANNEL_QUALIFIED = 'services.discord.db_updates_discord_channel';

final public const SUBMISSIONS_DISCORD_CHANNEL_QUALIFIED = 'services.discord.submissions_discord_channel';

final public const OPENAI_BEARER_TOKEN = 'services.openai.token';
}
1 change: 1 addition & 0 deletions app/Enums/Discord/EmbedColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ enum EmbedColor: int
case GREEN = 3066993;
case YELLOW = 16776960;
case RED = 15158332;
case PURPLE = 10092799;
}
128 changes: 128 additions & 0 deletions app/Nova/Actions/Discord/DiscordThreadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace App\Nova\Actions\Discord;

use App\Constants\Config\ServiceConstants;
use App\Enums\Discord\EmbedColor;
use App\Enums\Models\Wiki\AnimeSeason;
use App\Enums\Models\Wiki\ImageFacet;
use App\Models\Wiki\Image;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Http\Requests\NovaRequest;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Http;
use Laravel\Nova\Fields\Text;

class DiscordThreadAction extends Action
{
/**
* Get the displayable name of the action.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public function name(): string
{
return __('nova.actions.anime.discord.thread.name');
}

/**
* Perform the action on the given models.
*
* @param \Laravel\Nova\Fields\ActionFields $fields
* @param \Illuminate\Support\Collection $models
* @return mixed
*/
public function handle(ActionFields $fields, Collection $models)
{
$anime = $models->first();
$name = $fields->get('name');

$imagePath = $anime->images()->where(Image::ATTRIBUTE_FACET, ImageFacet::COVER_LARGE)->first()->path;
$image = Storage::disk(Config::get('image.disk'))->url($imagePath);

$animepage = json_decode(file_get_contents(base_path('composer.json')), true)['homepage'] . 'anime/';
$description = '**Synopsis:** ' . strip_tags($anime->synopsis) . "\n\n" . '**Link:** ' . $animepage . $anime->slug;

Http::withToken(Config::get('services.discord.token'), 'Bot')
->asMultipart()
->attach('file', file_get_contents($image), 'image.jpg')
->post("https://discord.com/api/v10/channels/{$this->getDiscordChannel()}/threads", [
'payload_json' => json_encode([
'name' => $name,
'applied_tags' => $this->getAppliedTags($anime->season->value),
'message' => [
'embeds' => [
[
'color' => EmbedColor::PURPLE,
'title' => $anime->name,
'description' => $description,
]
],
]
])
])->throw();

return $models;
}

/**
* Get Discord forum channel the thread will be created to.
*
* @return string
*/
private function getDiscordChannel(): string
{
return Config::get(ServiceConstants::SUBMISSIONS_DISCORD_CHANNEL_QUALIFIED);
}

/**
* Get the IDs of the tags applied to the thread.
*
* @param int $season
* @return array
*/
private function getAppliedTags(int $season): array
{
$map = [
AnimeSeason::WINTER->value => Config::get('services.discord.submissions_forum_tags.winter'),
AnimeSeason::SPRING->value => Config::get('services.discord.submissions_forum_tags.spring'),
AnimeSeason::SUMMER->value => Config::get('services.discord.submissions_forum_tags.summer'),
AnimeSeason::FALL->value => Config::get('services.discord.submissions_forum_tags.fall')
];

return $map[$season] === null
? []
: [$map[$season]];
}

/**
* Get the fields available on the action.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
$anime = $request->findModelQuery()->first();

return array_merge(
parent::fields($request),
[
Text::make(__('nova.actions.discord.thread.name'), 'name')
->default($anime->name ?? '')
->required()
->rules(['required', 'max:100'])
->maxlength(100)
->enforceMaxlength()
->help(__('nova.actions.discord.thread.help'))
]
);
}
}
8 changes: 8 additions & 0 deletions app/Nova/Resources/Wiki/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Enums\Models\Wiki\ResourceSite;
use App\Models\Wiki\Anime as AnimeModel;
use App\Models\Wiki\ExternalResource as ExternalResourceModel;
use App\Models\Wiki\Video as VideoModel;
use App\Nova\Actions\Discord\DiscordThreadAction;
use App\Nova\Actions\Models\Wiki\Anime\AttachAnimeResourceAction;
use App\Nova\Actions\Models\Wiki\Anime\BackfillAnimeAction;
use App\Nova\Lenses\Anime\Image\AnimeCoverLargeLens;
Expand Down Expand Up @@ -288,6 +290,12 @@ public function actions(NovaRequest $request): array
return array_merge(
parent::actions($request),
[
(new DiscordThreadAction())
->confirmButtonText(__('nova.actions.base.confirmButtonText'))
->cancelButtonText(__('nova.actions.base.cancelButtonText'))
->exceptOnIndex()
->canSeeWhen('create', VideoModel::class),

(new BackfillAnimeAction($request->user()))
->confirmButtonText(__('nova.actions.anime.backfill.confirmButtonText'))
->cancelButtonText(__('nova.actions.base.cancelButtonText'))
Expand Down
7 changes: 7 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
'token' => env('DISCORD_BOT_API_TOKEN'),
'db_updates_discord_channel' => env('DB_UPDATES_DISCORD_CHANNEL'),
'admin_discord_channel' => env('ADMIN_DISCORD_CHANNEL'),
'submissions_discord_channel' => env('SUBMISSIONS_DISCORD_CHANNEL'),
'submissions_forum_tags' => [
'winter' => env('WINTER_DISCORD_FORUM_TAG'),
'spring' => env('SPRING_DISCORD_FORUM_TAG'),
'summer' => env('SUMMER_DISCORD_FORUM_TAG'),
'fall' => env('FALL_DISCORD_FORUM_TAG')
]
],

'mal' => [
Expand Down
11 changes: 11 additions & 0 deletions lang/en/nova.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
],
'name' => 'Backfill Anime',
],
'discord' => [
'thread' => [
'name' => 'Create Thread Discord'
]
]
],
'audio' => [
'delete' => [
Expand All @@ -72,6 +77,12 @@
'cancelButtonText' => 'Cancel',
'confirmButtonText' => 'Confirm',
],
'discord' => [
'thread' => [
'name' => 'Name',
'help' => 'The name of the thread to be created. Use the default name or a shorter synonym if it exceeds 100 characters.'
]
],
'dump' => [
'dump' => [
'confirmButtonText' => 'Dump',
Expand Down

0 comments on commit e3baa82

Please sign in to comment.