Skip to content

Commit 42bae8e

Browse files
committed
Fix Discord field value min length check
1 parent 5f49d0b commit 42bae8e

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

src/Controllers/Document/Create.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ protected function handlePost(): void
9494

9595
if ($event->commit())
9696
{
97+
if (empty($brief)) $brief = '*empty*';
9798
$embed = Logger::initDiscordEmbed($event, $document->getURI(), [
9899
'Title' => $title,
99100
'Brief' => $brief,

src/Controllers/Document/Delete.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,26 @@ public function invoke(?array $args): bool
5757

5858
if ($event->commit())
5959
{
60+
$brief = $this->model->document->getBrief(false);
61+
if (empty($brief)) $brief = '*empty*';
62+
6063
$content = $this->model->document->getContent(false);
6164
$markdown = $this->model->document->isMarkdown();
6265
$user = $this->model->document->getUser();
66+
6367
$embed = Logger::initDiscordEmbed($event, $this->model->document->getURI(), [
6468
'Title' => $this->model->document->getTitle(),
65-
'Brief' => $this->model->document->getBrief(false),
69+
'Brief' => $brief,
6670
'Markdown' => $markdown ? ':white_check_mark:' : ':x:',
6771
'Authored by' => !\is_null($user) ? $user->getAsMarkdown() : '*Anonymous*',
6872
'Deleted by' => $this->model->active_user->getAsMarkdown(),
6973
]);
74+
7075
$desc = \substr($content, 0, \min(\BNETDocs\Libraries\Discord\Embed::MAX_DESCRIPTION - 13, \strlen($content) - 13));
7176
if (\strlen($desc) != \strlen($content)) $desc .= '';
7277
if (!$markdown) $desc = '```' . \PHP_EOL . $desc . \PHP_EOL . '```';
7378
$embed->setDescription($desc);
79+
7480
Logger::logToDiscord($event, $embed);
7581
}
7682
}

src/Controllers/Document/Edit.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,26 @@ protected function handlePost(): void
107107

108108
if ($event->commit())
109109
{
110+
$brief = $this->model->document->getBrief(false);
111+
if (empty($brief)) $brief = '*empty*';
112+
110113
$content = $this->model->document->getContent(false);
111114
$markdown = $this->model->document->isMarkdown();
112115
$user = $this->model->document->getUser();
116+
113117
$embed = Logger::initDiscordEmbed($event, $this->model->document->getURI(), [
114118
'Title' => $this->model->document->getTitle(),
115-
'Brief' => $this->model->document->getBrief(false),
119+
'Brief' => $brief,
116120
'Markdown' => $markdown ? ':white_check_mark:' : ':x:',
117121
'Authored by' => !\is_null($user) ? $user->getAsMarkdown() : '*Anonymous*',
118122
'Edited by' => $this->model->active_user->getAsMarkdown(),
119123
]);
124+
120125
$desc = \substr($content, 0, \min(\BNETDocs\Libraries\Discord\Embed::MAX_DESCRIPTION - 13, \strlen($content) - 13));
121126
if (\strlen($desc) != \strlen($content)) $desc .= '';
122127
if (!$markdown) $desc = '```' . \PHP_EOL . $desc . \PHP_EOL . '```';
123128
$embed->setDescription($desc);
129+
124130
Logger::logToDiscord($event, $embed);
125131
}
126132
}

src/Libraries/Discord/EmbedField.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class EmbedField implements \JsonSerializable
1111
{
1212
public const MAX_NAME = 256;
1313
public const MAX_VALUE = 1024;
14+
public const MIN_VALUE = 1;
1415

1516
protected bool $inline;
1617
protected string $name;
@@ -53,11 +54,23 @@ public function setName(string $name): void
5354

5455
public function setValue(int|float|string|bool $value): void
5556
{
56-
if (is_string($value) && strlen($value) > self::MAX_VALUE)
57+
if (is_string($value))
5758
{
58-
throw new LengthException(sprintf(
59-
'Discord forbids value longer than %d characters', self::MAX_VALUE
60-
));
59+
if (strlen($value) > self::MAX_VALUE)
60+
{
61+
throw new LengthException(sprintf(
62+
'Discord forbids value longer than %d characters', self::MAX_VALUE
63+
));
64+
}
65+
else if (strlen($value) < self::MIN_VALUE)
66+
{
67+
// It's necessary to check for short values because Discord replies with a 400 Bad Request
68+
// if the `field.value` is empty; this is *NOT* documented in Discord's Developers Docs:
69+
// <https://discord.com/developers/docs/resources/message#embed-object-embed-field-structure>
70+
throw new LengthException(sprintf(
71+
'Discord forbids value shorter than %d characters', self::MIN_VALUE
72+
));
73+
}
6174
}
6275

6376
$this->value = $value;

0 commit comments

Comments
 (0)