Skip to content

Commit c2424e8

Browse files
committed
Add Discord Client
1 parent 765a24f commit c2424e8

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
- SLACK_TEAM_ID
2323
- SLACK_REDIRECT_URI
2424
- SLACK_BOT_USER_OAUTH_TOKEN
25+
- DISCORD_BOT_TOKEN
2526
- SENTRY_BACKEND_DSN
2627
- SENTRY_FRONTEND_DSN
2728
- SENTRY_AUTH_TOKEN

src/Event/AbstractEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace App\Event;
55

66
use App\Http\SlackClient;
7+
use App\Http\DiscordClient;
78
use Cake\ORM\Locator\LocatorAwareTrait;
89

910
abstract class AbstractEvent
@@ -20,6 +21,7 @@ abstract class AbstractEvent
2021
public const TYPE_LINK_SHARED = 'link_shared';
2122

2223
protected SlackClient $slackClient;
24+
protected DiscordClient $discordClient;
2325

2426
public string $type;
2527
public string $eventTimestamp;
@@ -30,6 +32,7 @@ abstract class AbstractEvent
3032
public function __construct()
3133
{
3234
$this->slackClient = new SlackClient();
35+
$this->discordClient = new DiscordClient();
3336
}
3437

3538
/**

src/Event/LinkSharedEvent.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace App\Event;
55

6+
use RestCord\DiscordClient;
7+
68
class LinkSharedEvent extends AbstractEvent
79
{
810
public string $user;
@@ -38,23 +40,38 @@ public function __construct(array $event)
3840
public function process(): void
3941
{
4042
foreach ($this->links as $link) {
41-
$this->slackClient->unfurl(
42-
channel: $this->channel,
43-
timestamp: $this->messageTimeStamp,
44-
unfurls: [
45-
$link['url'] => [
46-
'blocks' => [
47-
[
48-
'type' => 'section',
49-
'text' => [
50-
'type' => 'mrkdwn',
51-
'text' => 'asdasdasd',
43+
// if Discord link, unfurl and fetch the message and return it
44+
if (str_starts_with($link['url'], 'https://discord.com/')) {
45+
$message = $this->fetchDiscordMessage($link['url']);
46+
$this->slackClient->unfurl(
47+
channel: $this->channel,
48+
timestamp: $this->messageTimeStamp,
49+
unfurls: [
50+
$link['url'] => [
51+
'blocks' => [
52+
[
53+
'type' => 'section',
54+
'text' => [
55+
'type' => 'mrkdwn',
56+
'text' => $message,
57+
],
5258
],
5359
],
5460
],
5561
],
56-
],
57-
);
62+
);
63+
}
5864
}
5965
}
66+
67+
private function fetchDiscordMessage(string $url): string
68+
{
69+
$parts = explode('/', $url);
70+
$channelId = $parts[5];
71+
$messageId = $parts[6];
72+
73+
$message = $this->discordClient->getMessage($channelId, $messageId);
74+
return $message;
75+
}
76+
6077
}

src/Http/DiscordClient.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Http;
5+
6+
use function Cake\Core\env;
7+
use function Sentry\captureMessage;
8+
use function Sentry\withScope;
9+
10+
class DiscordClient
11+
{
12+
protected const DISCORD_API_URL = 'discord.com/api/v10';
13+
14+
protected Client $client;
15+
16+
/**
17+
* Constructor.
18+
*/
19+
public function __construct()
20+
{
21+
$this->client = new Client([
22+
'host' => self::DISCORD_API_URL,
23+
'scheme' => 'https',
24+
'headers' => [
25+
'Authorization' => 'Bot ' . env('DISCORD_BOT_TOKEN'),
26+
'Content-Type' => 'application/json',
27+
],
28+
]);
29+
}
30+
31+
/**
32+
* Get a message from a channel
33+
*
34+
* @param string $channelId The channel ID
35+
* @param string $messageId The message ID
36+
* @return string The message content
37+
*/
38+
public function getMessage(string $channelId, string $messageId): string
39+
{
40+
$response = $this->client->get("channels/{$channelId}/messages/{$messageId}");
41+
42+
if ($response->isSuccess()) {
43+
$json = $response->getJson();
44+
return $json['content'] ?? '';
45+
}
46+
47+
withScope(function ($scope) use ($response, $channelId, $messageId): void {
48+
$scope->setContext('Discord API', [
49+
'channel_id' => $channelId,
50+
'message_id' => $messageId,
51+
'discord_response' => $response->getJson(),
52+
]);
53+
captureMessage('Discord API error: Failed to fetch message');
54+
});
55+
56+
return '';
57+
}
58+
}

0 commit comments

Comments
 (0)