Skip to content

Commit 0b2efce

Browse files
committed
new PSR14 events
1 parent bd1226f commit 0b2efce

16 files changed

Lines changed: 224 additions & 23 deletions

files/lib/action/AbstractDiscordInteractionAction.class.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\ServerRequestInterface;
99
use Psr\Http\Server\RequestHandlerInterface;
10+
use wcf\command\discord\interaction\log\LogDiscordInteraction;
1011
use wcf\data\discord\bot\DiscordBotList;
11-
use wcf\data\discord\interaction\log\DiscordInteractionLogAction;
1212
use wcf\system\discord\DiscordApi;
1313
use wcf\system\discord\interaction\callback\PingInteractionCallback;
1414
use wcf\util\JSON;
@@ -23,13 +23,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
2323
$body = (string)$request->getBody();
2424

2525
if (ENABLE_DEBUG_MODE) {
26-
$action = new DiscordInteractionLogAction([], 'create', [
27-
'data' => [
28-
'log' => $body,
29-
'time' => TIME_NOW,
30-
],
31-
]);
32-
$action->executeAction();
26+
(new LogDiscordInteraction($body));
3327
}
3428

3529
if ($body === '') {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace wcf\command\discord\bot;
4+
5+
use wcf\data\discord\bot\DiscordBot;
6+
use wcf\data\discord\bot\DiscordBotAction;
7+
use wcf\event\discord\bot\DiscordBotDeleted;
8+
use wcf\system\event\EventHandler;
9+
10+
final class DeleteDiscordBot
11+
{
12+
public function __construct(
13+
private readonly DiscordBot $bot
14+
) {
15+
}
16+
17+
public function __invoke(): void
18+
{
19+
$action = new DiscordBotAction([$this->bot], 'delete');
20+
$action->executeAction();
21+
22+
EventHandler::getInstance()->fire(new DiscordBotDeleted($this->bot));
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace wcf\command\discord\interaction\log;
4+
5+
use wcf\data\discord\interaction\log\DiscordInteractionLogAction;
6+
7+
final class LogDiscordInteraction
8+
{
9+
public function __construct(
10+
private readonly string $log
11+
) {
12+
}
13+
14+
public function __invoke(): void
15+
{
16+
$action = new DiscordInteractionLogAction([], 'create', [
17+
'data' => [
18+
'log' => $this->log,
19+
'time' => \TIME_NOW,
20+
],
21+
]);
22+
$action->executeAction();
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace wcf\command\discord\webhook;
4+
5+
use wcf\data\discord\webhook\DiscordWebhook;
6+
use wcf\data\discord\webhook\DiscordWebhookAction;
7+
use wcf\event\discord\webhook\DiscordWebhookDeleted;
8+
use wcf\system\event\EventHandler;
9+
10+
final class DeleteDiscordWebhook
11+
{
12+
public function __construct(
13+
private readonly DiscordWebhook $webhook
14+
) {
15+
}
16+
17+
public function __invoke(): void
18+
{
19+
$discordApi = $this->webhook->getDiscordApi();
20+
$discordApi->deleteWebhookWithToken($this->webhook->webhookID, $this->webhook->webhookToken);
21+
22+
$action = new DiscordWebhookAction([$this->webhook], 'delete');
23+
$action->executeAction();
24+
25+
EventHandler::getInstance()->fire(new DiscordWebhookDeleted($this->webhook));
26+
}
27+
}

files/lib/data/discord/bot/DiscordBotAction.class.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace wcf\data\discord\bot;
44

55
use wcf\data\AbstractDatabaseObjectAction;
6+
use wcf\event\discord\bot\DiscordBotCreated;
7+
use wcf\event\discord\bot\DiscordBotUpdated;
68
use wcf\system\cache\builder\DiscordGuildChannelsCacheBuilder;
9+
use wcf\system\event\EventHandler;
710
use wcf\system\exception\AJAXException;
811
use wcf\system\exception\PermissionDeniedException;
912
use wcf\system\WCF;
@@ -44,7 +47,11 @@ public function create()
4447
unset($this->parameters['data']['useApplicationCommands']);
4548
}
4649

47-
return parent::create();
50+
$bot = parent::create();
51+
52+
EventHandler::getInstance()->fire(new DiscordBotCreated($bot));
53+
54+
return $bot;
4855
}
4956

5057
#[\Override]
@@ -58,6 +65,11 @@ public function update()
5865
}
5966

6067
parent::update();
68+
69+
foreach ($this->getObjects() as $bot) {
70+
$updatedBot = new DiscordBot($bot->botID);
71+
EventHandler::getInstance()->fire(new DiscordBotUpdated($updatedBot));
72+
}
6173
}
6274

6375
#[\Override]

files/lib/data/discord/interaction/log/DiscordInteractionLogAction.class.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace wcf\data\discord\interaction\log;
44

55
use wcf\data\AbstractDatabaseObjectAction;
6+
use wcf\event\discord\interaction\log\DiscordInteractionLogCreated;
7+
use wcf\system\event\EventHandler;
68

79
/**
810
* @extends AbstractDatabaseObjectAction<DiscordInteractionLog, DiscordInteractionLogEditor>
@@ -13,4 +15,14 @@ final class DiscordInteractionLogAction extends AbstractDatabaseObjectAction
1315
* @inheritDoc
1416
*/
1517
public $className = DiscordInteractionLogEditor::class;
18+
19+
#[\Override]
20+
public function create()
21+
{
22+
$log = parent::create();
23+
24+
EventHandler::getInstance()->fire(new DiscordInteractionLogCreated($log));
25+
26+
return $log;
27+
}
1628
}

files/lib/data/discord/webhook/DiscordWebhookAction.class.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace wcf\data\discord\webhook;
44

55
use wcf\data\AbstractDatabaseObjectAction;
6+
use wcf\event\discord\webhook\DiscordWebhookCreated;
7+
use wcf\event\discord\webhook\DiscordWebhookUpdated;
68
use wcf\system\cache\builder\DiscordGuildChannelsCacheBuilder;
9+
use wcf\system\event\EventHandler;
710

811
/**
912
* Discord-Webhook-Objekt-Action
@@ -28,15 +31,24 @@ final class DiscordWebhookAction extends AbstractDatabaseObjectAction
2831
public $className = DiscordWebhookEditor::class;
2932

3033
#[\Override]
31-
public function delete()
34+
public function create()
3235
{
33-
foreach ($this->objects as $object) {
34-
$discordWebhook = $object->getDecoratedObject();
35-
$discordApi = $discordWebhook->getDiscordApi();
36-
$discordApi->deleteWebhookWithToken($discordWebhook->webhookID, $discordWebhook->webhookToken);
37-
}
36+
$webhook = parent::create();
37+
38+
EventHandler::getInstance()->fire(new DiscordWebhookCreated($webhook));
39+
40+
return $webhook;
41+
}
3842

39-
return parent::delete();
43+
#[\Override]
44+
public function update()
45+
{
46+
parent::update();
47+
48+
foreach ($this->getObjects() as $webhook) {
49+
$updatedWebhook = new DiscordWebhook($webhook->webhookID);
50+
EventHandler::getInstance()->fire(new DiscordWebhookUpdated($updatedWebhook));
51+
}
4052
}
4153

4254
#[\Override]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace wcf\event\discord\bot;
4+
5+
use wcf\data\discord\bot\DiscordBot;
6+
use wcf\event\IPsr14Event;
7+
8+
final class DiscordBotCreated implements IPsr14Event
9+
{
10+
public function __construct(
11+
public readonly DiscordBot $bot,
12+
) {
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace wcf\event\discord\bot;
4+
5+
use wcf\data\discord\bot\DiscordBot;
6+
use wcf\event\IPsr14Event;
7+
8+
final class DiscordBotDeleted implements IPsr14Event
9+
{
10+
public function __construct(
11+
public readonly DiscordBot $bot,
12+
) {
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace wcf\event\discord\bot;
4+
5+
use wcf\data\discord\bot\DiscordBot;
6+
use wcf\event\IPsr14Event;
7+
8+
final class DiscordBotUpdated implements IPsr14Event
9+
{
10+
public function __construct(
11+
public readonly DiscordBot $bot,
12+
) {
13+
}
14+
}

0 commit comments

Comments
 (0)