Skip to content

Commit

Permalink
Send the Discord spoiling code in attachment to avoid admin message l…
Browse files Browse the repository at this point in the history
…ength limit
  • Loading branch information
pyrech committed Dec 11, 2024
1 parent 4e959e6 commit 3265ba5
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"symfony/form": ">=7.1.5",
"symfony/framework-bundle": ">=7.1.5",
"symfony/http-client": ">=7.1.5",
"symfony/mime": "7.1.*",
"symfony/monolog-bundle": "^3.10",
"symfony/runtime": ">=7.1.1",
"symfony/twig-bundle": ">=7.1.5",
Expand Down
169 changes: 168 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions src/Discord/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace JoliCode\SecretSanta\Discord;

use JoliCode\SecretSanta\Model\File;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

Expand Down Expand Up @@ -56,7 +58,7 @@ public function getRolesInGuild(int $guildId): array
return $response->toArray();
}

public function sendMessage(int $userId, string $message): void
public function sendMessage(int $userId, string $message, ?File $file = null): void
{
// Create a private channel with the user
$response = $this->callApi('POST', '/users/@me/channels', [
Expand All @@ -72,14 +74,31 @@ public function sendMessage(int $userId, string $message): void
$channel = $response->toArray();
$channelId = $channel['id'];

$body = [];
$payload = [
'content' => $message,
];

if ($file) {
$payload['attachements'][] = [
'id' => 0,
'description' => 'Secret Santa spoiling code',
'filename' => $file->name,
];
$body['files[0]'] = new DataPart($file->content, $file->name, 'text/plain');
}

$body['payload_json'] = new DataPart(json_encode($payload), null, 'application/json');

$formData = new FormDataPart($body);

$options = [
'json' => [
'content' => $message
],
'body' => $formData->bodyToString(),
'headers' => $formData->getPreparedHeaders()->toArray(),
];

// Send message to the private channel
$response = $this->callApi('POST', "/channels/$channelId/messages", $options);
$response = $this->callApi('POST', "/channels/{$channelId}/messages", $options);

if (200 !== $response->getStatusCode()) {
throw new \RuntimeException('Failed to send private message: ' . $response->getContent(false));
Expand Down
7 changes: 3 additions & 4 deletions src/Discord/MessageSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace JoliCode\SecretSanta\Discord;

use JoliCode\SecretSanta\Exception\MessageSendFailedException;
use JoliCode\SecretSanta\Model\File;
use JoliCode\SecretSanta\Model\SecretSanta;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;

Expand Down Expand Up @@ -96,19 +97,17 @@ public function sendAdminMessage(SecretSanta $secretSanta, string $code, string
In case of trouble or if you need it for whatever reason, here is a way to **retrieve the secret repartition**:
- Copy the following content:
```%s```
- Copy the content of the `secret-santa-spoil-code.txt` file attached to this message
- Paste the content on <%s> then submit
Remember, with great power comes great responsibility!
Happy Secret Santa!',
$code,
$spoilUrl
);

try {
$this->apiHelper->sendMessage((int) $secretSanta->getConfig()->getAdmin()->getIdentifier(), $text);
$this->apiHelper->sendMessage((int) $secretSanta->getConfig()->getAdmin()->getIdentifier(), $text, new File('secret-santa-spoil-code.txt', $code));
} catch (ClientExceptionInterface $e) {
$precision = null;

Expand Down
21 changes: 21 additions & 0 deletions src/Model/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Secret Santa project.
*
* (c) JoliCode <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JoliCode\SecretSanta\Model;

readonly class File
{
public function __construct(
public string $name,
public string $content,
) {
}
}

0 comments on commit 3265ba5

Please sign in to comment.