-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: discord bot integration (#683)
- Loading branch information
Showing
29 changed files
with
1,013 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions\Discord; | ||
|
||
use App\Models\Wiki\Image; | ||
use App\Models\Wiki\Video; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
class DiscordVideoNotificationAction | ||
{ | ||
/** | ||
* Handle the action. | ||
* | ||
* @param Collection<int, Video> $videos | ||
* @param array $fields | ||
* | ||
* @return void | ||
*/ | ||
public function handle(Collection $videos, array $fields): void | ||
{ | ||
$type = Arr::get($fields, 'type'); | ||
|
||
/** @var \Illuminate\Filesystem\FilesystemAdapter */ | ||
$fs = Storage::disk(Config::get('image.disk')); | ||
|
||
$newVideos = []; | ||
|
||
foreach ($videos as $video) { | ||
$video | ||
->load([ | ||
'animethemeentries.animetheme.anime.discordthread', | ||
'animethemeentries.animetheme.anime.images', | ||
'animethemeentries.animetheme.group', | ||
'animethemeentries.animetheme.song.artists', | ||
]); | ||
|
||
$theme = $video->animethemeentries->first()->animetheme; | ||
|
||
if ($theme->anime->discordthread === null) continue; | ||
|
||
Arr::set($video, 'source_name', $video->source->localize()); | ||
Arr::set($video, 'overlap_name', $video->overlap->localize()); | ||
Arr::set($theme, 'type_name', $theme->type->localize()); | ||
|
||
$theme->anime->images->each(function (Image $image) use ($fs) { | ||
Arr::set($image, 'link', $fs->url($image->path)); | ||
}); | ||
|
||
$newVideos[] = $video; | ||
} | ||
|
||
Http::withHeaders(['x-api-key' => Config::get('services.discord.api_key')]) | ||
->post(Config::get('services.discord.api_url') . '/notification', [ | ||
'type' => $type, | ||
'videos' => $newVideos, | ||
]) | ||
->throw(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Events\Discord\DiscordThread; | ||
|
||
use App\Events\Base\Admin\AdminDeletedEvent; | ||
use App\Models\Discord\DiscordThread; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
/** | ||
* Class DiscordThreadDeleted. | ||
* | ||
* @extends AdminDeletedEvent<DiscordThread> | ||
*/ | ||
class DiscordThreadDeleted extends AdminDeletedEvent | ||
{ | ||
/** | ||
* Create a new event instance. | ||
* | ||
* @param DiscordThread $thread | ||
*/ | ||
public function __construct(DiscordThread $thread) | ||
{ | ||
parent::__construct($thread); | ||
$thread->forceDelete(); | ||
$this->deleteThread(); | ||
} | ||
|
||
/** | ||
* Get the model that has fired this event. | ||
* | ||
* @return DiscordThread | ||
*/ | ||
public function getModel(): DiscordThread | ||
{ | ||
return $this->model; | ||
} | ||
|
||
/** | ||
* Get the description for the Discord message payload. | ||
* | ||
* @return string | ||
*/ | ||
protected function getDiscordMessageDescription(): string | ||
{ | ||
return "Discord Thread '**{$this->getModel()->getName()}**' has been deleted."; | ||
} | ||
|
||
/** | ||
* Delete the thread on discord. | ||
* | ||
* @return void | ||
*/ | ||
protected function deleteThread(): void | ||
{ | ||
Http::withHeaders(['x-api-key' => Config::get('services.discord.api_key')]) | ||
->delete(Config::get('services.discord.api_url') . '/thread', ['id' => $this->getModel()->getKey()]) | ||
->throw(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Events\Discord\DiscordThread; | ||
|
||
use App\Events\Base\Admin\AdminUpdatedEvent; | ||
use App\Models\Discord\DiscordThread; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
/** | ||
* Class DiscordThreadUpdated. | ||
* | ||
* @extends AdminUpdatedEvent<DiscordThread> | ||
*/ | ||
class DiscordThreadUpdated extends AdminUpdatedEvent | ||
{ | ||
/** | ||
* Create a new event instance. | ||
* | ||
* @param DiscordThread $thread | ||
*/ | ||
public function __construct(DiscordThread $thread) | ||
{ | ||
parent::__construct($thread); | ||
$this->updateThread(); | ||
$this->initializeEmbedFields($thread); | ||
} | ||
|
||
/** | ||
* Get the model that has fired this event. | ||
* | ||
* @return DiscordThread | ||
*/ | ||
public function getModel(): DiscordThread | ||
{ | ||
return $this->model; | ||
} | ||
|
||
/** | ||
* Get the description for the Discord message payload. | ||
* | ||
* @return string | ||
*/ | ||
protected function getDiscordMessageDescription(): string | ||
{ | ||
return "Discord Thread '**{$this->getModel()->getName()}**' has been updated."; | ||
} | ||
|
||
/** | ||
* Update the thread on discord. | ||
* | ||
* @return void | ||
*/ | ||
protected function updateThread(): void | ||
{ | ||
Http::withHeaders(['x-api-key' => Config::get('services.discord.api_key')]) | ||
->put(Config::get('services.discord.api_url') . '/thread', $this->getModel()) | ||
->throw(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\BulkActions; | ||
|
||
use App\Models\BaseModel; | ||
use Filament\Tables\Actions\BulkAction; | ||
use Illuminate\Database\Eloquent\Collection; | ||
|
||
/** | ||
* Class BaseBulkAction. | ||
*/ | ||
abstract class BaseBulkAction extends BulkAction | ||
{ | ||
/** | ||
* Initial setup for the action. | ||
* | ||
* @return void | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->action(fn (Collection $records, array $data) => $this->handle($records, $data)); | ||
} | ||
|
||
/** | ||
* Handle the action. | ||
* | ||
* @param Collection<int, BaseModel> $records | ||
* @param array $fields | ||
* @return void | ||
*/ | ||
abstract public function handle(Collection $records, array $fields): void; | ||
} |
Oops, something went wrong.