Skip to content

Commit 3af0f36

Browse files
committed
intents collector
1 parent 7624ab9 commit 3af0f36

File tree

6 files changed

+124
-11
lines changed

6 files changed

+124
-11
lines changed

acptemplates/discordBotAddManager.tpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858
{/if}
5959
</div>
6060

61+
{if $step == 1}
62+
{if $neededIntents|count}
63+
<p class="warning">
64+
{lang}wcf.acp.discordBotAddManager.gatewaysNeeded{/lang} {', '|implode:$neededIntents}
65+
</p>
66+
{else}
67+
<p class="info">{lang}wcf.acp.discordBotAddManager.noGatewaysNeeded{/lang}</p>
68+
{/if}
69+
{/if}
70+
6171
{event name='message'}
6272

6373
{if $step != 6 && $step != 0}

files/lib/acp/form/DiscordBotAddManagerForm.class.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Override;
66
use wcf\data\discord\bot\DiscordBotAction;
7+
use wcf\event\discord\DiscordIntentsCollecting;
78
use wcf\event\discord\DiscordOAuthRequiredCollecting;
89
use wcf\event\discord\DiscordPublicKeyRequiredCollecting;
910
use wcf\form\AbstractForm;
@@ -63,6 +64,8 @@ class DiscordBotAddManagerForm extends AbstractFormBuilderForm
6364
*/
6465
protected array $guilds = [];
6566

67+
protected array $neededIntents = [];
68+
6669
#[Override]
6770
public function readParameters()
6871
{
@@ -106,6 +109,10 @@ protected function createForm()
106109

107110
protected function createFormStep1()
108111
{
112+
$intentsCollection = new DiscordIntentsCollecting();
113+
EventHandler::getInstance()->fire($intentsCollection);
114+
$this->neededIntents = $intentsCollection->neededIntents();
115+
109116
$this->form->appendChildren([
110117
FormContainer::create('data')
111118
->appendChildren([
@@ -119,8 +126,12 @@ protected function createFormStep1()
119126
$botToken = $formField->getValue();
120127

121128
$discord = new DiscordApi(0, $botToken);
122-
$bot = $discord->getCurrentUser();
123-
if (!isset($bot['body']['id'])) {
129+
$bot = $discord->getCurrentApplication();
130+
if (
131+
!isset($bot['body']['bot']['id'])
132+
|| !isset($bot['body']['bot']['username'])
133+
|| !isset($bot['body']['bot']['discriminator'])
134+
) {
124135
$formField->addValidationError(new FormFieldValidationError(
125136
'invalidBotToken',
126137
'wcf.acp.discordBotAddManager.botToken.invalid'
@@ -143,12 +154,12 @@ protected function createFormStep2()
143154
->value($requestData['botToken'])
144155
->required(),
145156
HiddenFormField::create('clientID')
146-
->value($requestData['clientID'] ?? $this->tempInfo['id'])
157+
->value($requestData['clientID'] ?? $this->tempInfo['bot']['id'])
147158
->required(),
148159
HiddenFormField::create('botName')
149160
->value(
150-
$requestData['botName'] ?? $this->tempInfo['username'] . '#'
151-
. $this->tempInfo['discriminator']
161+
$requestData['botName'] ?? $this->tempInfo['bot']['username'] . '#'
162+
. $this->tempInfo['bot']['discriminator']
152163
)
153164
->required(),
154165
]),
@@ -320,6 +331,7 @@ public function assignVariables()
320331
WCF::getTPL()->assign([
321332
'step' => $this->step,
322333
'tempInfo' => $this->tempInfo,
334+
'neededIntents' => $this->neededIntents,
323335
]);
324336
}
325337
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace wcf\event\discord;
4+
5+
use wcf\event\IPsr14Event;
6+
7+
final class DiscordIntentsCollecting implements IPsr14Event
8+
{
9+
private bool $presenceIntent = false;
10+
11+
private bool $serverMembersIntent = false;
12+
13+
private bool $messageContentIntent = false;
14+
15+
public function presenceIntent(): void
16+
{
17+
$this->presenceIntent = true;
18+
}
19+
20+
public function serverMembersIntent(): void
21+
{
22+
$this->serverMembersIntent = true;
23+
}
24+
25+
public function messageContentIntent(): void
26+
{
27+
$this->messageContentIntent = true;
28+
}
29+
30+
public function needPresenceIntent(): bool
31+
{
32+
return $this->presenceIntent;
33+
}
34+
35+
public function needServerMembersIntent(): bool
36+
{
37+
return $this->serverMembersIntent;
38+
}
39+
40+
public function needMessageContentIntent(): bool
41+
{
42+
return $this->messageContentIntent;
43+
}
44+
45+
public function neededIntents(): array
46+
{
47+
$data = [];
48+
49+
if ($this->presenceIntent) {
50+
$data[] = 'Presence Intent';
51+
}
52+
if ($this->serverMembersIntent) {
53+
$data[] = 'Server Members Intent';
54+
}
55+
if ($this->messageContentIntent) {
56+
$data[] = 'Message Content Intent';
57+
}
58+
59+
return $data;
60+
}
61+
}

files/lib/system/discord/DiscordApi.class.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,9 +3363,9 @@ public function getGatewayBot()
33633363
* returns encoded user flag informations
33643364
*
33653365
* @param integer $flag Benutzer Flag als Decimalwert
3366-
* @return array
3366+
* @return string[]
33673367
*/
3368-
public function getUserFlagsArray($flag)
3368+
public function getUserFlagsArray(int $flag): array
33693369
{
33703370
$userFlags = [
33713371
1 => 'Discord Employee', // 1 << 0
@@ -3391,6 +3391,34 @@ public function getUserFlagsArray($flag)
33913391
return $flags;
33923392
}
33933393

3394+
/**
3395+
* @return string[]
3396+
*/
3397+
public function getApplicationFlags(int $flag): array
3398+
{
3399+
$applicationFlags = [
3400+
1 << 6 => 'APPLICATION_AUTO_MODERATION_RULE_CREATE_BADGE',
3401+
1 << 12 => 'GATEWAY_PRESENCE',
3402+
1 << 13 => 'GATEWAY_PRESENCE_LIMITED',
3403+
1 << 14 => 'GATEWAY_GUILD_MEMBERS',
3404+
1 << 15 => 'GATEWAY_GUILD_MEMBERS_LIMITED',
3405+
1 << 16 => 'VERIFICATION_PENDING_GUILD_LIMIT',
3406+
1 << 17 => 'EMBEDDED',
3407+
1 << 18 => 'GATEWAY_MESSAGE_CONTENT',
3408+
1 << 19 => 'GATEWAY_MESSAGE_CONTENT_LIMITED',
3409+
1 << 23 => 'APPLICATION_COMMAND_BADGE',
3410+
];
3411+
$flags = [];
3412+
3413+
foreach ($applicationFlags as $applicationFlag => $name) {
3414+
if (($flag & $applicationFlag) == $applicationFlag) {
3415+
$flags[] = $name;
3416+
}
3417+
}
3418+
3419+
return $flags;
3420+
}
3421+
33943422
/**
33953423
* returns encoded snowflake informations
33963424
*

language/de.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@
7676
{if LANGUAGE_USE_INFORMAL_VARIANT}Gehe{else}Gehen Sie{/if} in das <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Entwickler-Portal</a> von Discord. {if LANGUAGE_USE_INFORMAL_VARIANT}Wähle deine{else}Wählen Sie Ihre{/if} zuvor erstellte Anwendung aus. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} links im Menü auf den Menüpunkt „<b>Bot</b>“. Hier dürfte nun noch kein Bot zu sehen sein. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} dafür auf den Button „<b>Add Bot</b>“. Nun öffnet sich eine Nachfrage ob {if LANGUAGE_USE_INFORMAL_VARIANT}du{else}Sie{/if} das wirklich {if LANGUAGE_USE_INFORMAL_VARIANT}möchtest{else}möchten{/if}, diese {if LANGUAGE_USE_INFORMAL_VARIANT}bestätigst du{else}bestätigen Sie{/if} mit „<b>Yes, do it!</b>“. {if LANGUAGE_USE_INFORMAL_VARIANT}Dein{else}Ihr{/if} Bot wurde erfolgreich erstellt.<br><br>
7777
7878
<b>3. Privileged Gateway Intents aktivieren</b><br>
79-
{if LANGUAGE_USE_INFORMAL_VARIANT}Gehe{else}Gehen Sie{/if} in das <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Entwickler-Portal</a> von Discord. {if LANGUAGE_USE_INFORMAL_VARIANT}Wähle deine{else}Wählen Sie Ihre{/if} zuvor erstellte Anwendung aus. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} links im Menü auf den Menüpunkt „<b>Bot</b>“. {if LANGUAGE_USE_INFORMAL_VARIANT}Scrolle{else}Scrollen Sie{/if} zu der Zwischenüberschrift „<b>Privileged Gateway Intents</b>“ runter und {if LANGUAGE_USE_INFORMAL_VARIANT}aktiviere{else}aktivieren Sie{/if} die Slider bei „<b>Server Member Intents</b>“ und „<b>Message Content Intents</b>“. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} danach auf „<b>Save Changes</b>“.<br>
80-
<i>(Server Member Intents wird für das Plugin Discord-Sync benötigt um die Benutzer aus dem Server auslesen und deren Rechte verwalten zu können. Message Content Intents wird für das Plugin Discord-Shoutbox benötigt, um Nachrichten aus den Kanälen auslesen und in der Shoutbox anzeigen zu können.)</i><br><br>
79+
{if LANGUAGE_USE_INFORMAL_VARIANT}Gehe{else}Gehen Sie{/if} in das <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Entwickler-Portal</a> von Discord. {if LANGUAGE_USE_INFORMAL_VARIANT}Wähle deine{else}Wählen Sie Ihre{/if} zuvor erstellte Anwendung aus. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} links im Menü auf den Menüpunkt „<b>Bot</b>“. {if LANGUAGE_USE_INFORMAL_VARIANT}Scrolle{else}Scrollen Sie{/if} zu der Zwischenüberschrift „<b>Privileged Gateway Intents</b>“ runter und {if LANGUAGE_USE_INFORMAL_VARIANT}aktiviere{else}aktivieren Sie{/if} die Slider bei den benötigten Intents (siehe weiter unten). {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} danach auf „<b>Save Changes</b>“.<br><br>
8180
8281
<b>4. Bot Token finden</b><br>
8382
{if LANGUAGE_USE_INFORMAL_VARIANT}Gehe{else}Gehen Sie{/if} in das <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Entwickler-Portal</a> von Discord. {if LANGUAGE_USE_INFORMAL_VARIANT}Wähle deine{else}Wählen Sie Ihre{/if} zuvor erstellte Anwendung aus. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} links im Menü auf den Menüpunkt „<b>Bot</b>“. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} auf den Button „<b>Reset Token</b>“. Nun öffnet sich eine Nachfrage ob {if LANGUAGE_USE_INFORMAL_VARIANT}du{else}Sie{/if} das wirklich {if LANGUAGE_USE_INFORMAL_VARIANT}möchtest{else}möchten{/if}, diese {if LANGUAGE_USE_INFORMAL_VARIANT}bestätigst du{else}bestätigen Sie{/if} mit „<b>Yes, do it!</b>“. Falls {if LANGUAGE_USE_INFORMAL_VARIANT}du{else}Sie{/if} die Zwei-Faktor-Authentifizierung bei Discord aktiviert {if LANGUAGE_USE_INFORMAL_VARIANT}hast, musst du jetzt deinen{else}haben, müssen Sie jetzt Ihren{/if} Code eingeben und „<b>Submit</b>“ drücken. {if LANGUAGE_USE_INFORMAL_VARIANT}Du siehst nun deinen{else}Sie sehen nun Ihren{/if} Token. Mit einem Klick auf den Button „<b>Copy</b>“, {if LANGUAGE_USE_INFORMAL_VARIANT}kannst du{else}können Sie{/if} den Token direkt in die Zwischenablage einfügen.]]></item>
@@ -97,6 +96,8 @@
9796
{if LANGUAGE_USE_INFORMAL_VARIANT}Gehe{else}Gehen Sie{/if} in das <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Entwickler-Portal</a> von Discord. {if LANGUAGE_USE_INFORMAL_VARIANT}Wähle deine{else}Wählen Sie Ihre{/if} zuvor erstellte Anwendung aus. {if LANGUAGE_USE_INFORMAL_VARIANT}Klicke{else}Klicken Sie{/if} links im Menü auf den Menüpunkt „<b>General Information</b>“. Hier {if LANGUAGE_USE_INFORMAL_VARIANT}findest du{else}finden Sie{/if} auch schon den Public Key. Mit einem Klick auf den Button „<b>Copy</b>“, {if LANGUAGE_USE_INFORMAL_VARIANT}kannst du{else}können Sie{/if} den Public Key direkt in die Zwischenablage einfügen. {if LANGUAGE_USE_INFORMAL_VARIANT}Füge{else}Fügen Sie{/if} außerdem bei „Interactions Endpoint Url“ die URL <kbd>{link controller='DiscordInteraction' forceFrontend=true application='wcf'}{/link}</kbd> ein.]]></item>
9897
<item name="wcf.acp.discordBotAddManager.step6Intro"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Dein{else}Ihr{/if} Discord Bot wurde erfolgreich angelegt und konfiguriert.]]></item>
9998
<item name="wcf.acp.discordBotAddManager.step6Outro"><![CDATA[Herzlichen Glückwunsch, {if LANGUAGE_USE_INFORMAL_VARIANT}du bist{else}Sie sind{/if} nun fertig mit dem Konfigurationsassistenten und {if LANGUAGE_USE_INFORMAL_VARIANT}kannst deinen{else}können Ihren{/if} Discord Bot nutzen.]]></item>
99+
<item name="wcf.acp.discordBotAddManager.noGatewaysNeeded"><![CDATA[Für die installierten Plugins ist es nicht notwendig Privilaged Gateway Intents zu aktivieren.]]></item>
100+
<item name="wcf.acp.discordBotAddManager.gatewaysNeeded"><![CDATA[Für die installierten Plugins müssen folgende Privilaged Gateway Intents aktiviert werden:]]></item>
100101
</category>
101102
<category name="wcf.acp.discordBotList">
102103
<item name="wcf.acp.discordBotList.botName"><![CDATA[Bot-Name]]></item>

language/en.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@
7676
Go the <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Developer Portal</a> from Discord. Select your previously created application. Click on the menu item "<b>Bot</b>" in the left menu. No bot should be visible here yet. Click on the "<b>Add Bot</b>" button. Now you will be asked if you really want to do it, confirm with "<b>Yes, do it!</b>". Your bot was successfully created.<br><br>
7777
7878
<b>3. Privileged Gateway Intents aktivieren</b><br>
79-
Go the <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Developer Portal</a> from Discord. Select your previously created application. Click on the menu item "<b>Bot</b>" in the left menu. Scroll to the subheading „<b>Privileged Gateway Intents</b>“ and activate the sliders at „<b>Server Member Intents</b>“ and „<b>Message Content Intents</b>“. Click on „<b>Save Changes</b>“.<br>
80-
<i>(Server Member Intents is required for the Discord Sync plugin to read users from the server and manage their rights. Message Content Intents is required for the Discord Shoutbox plugin to read messages from the channels and display them in the Shoutbox.)</i><br><br>
79+
Go the <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Developer Portal</a> from Discord. Select your previously created application. Click on the menu item "<b>Bot</b>" in the left menu. Scroll to the subheading „<b>Privileged Gateway Intents</b>“ and activate the sliders for the required intents (see below). Click on „<b>Save Changes</b>“.<br><br>
8180
8281
<b>4. Find Bot Token</b><br>
8382
Go the <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Developer Portal</a> from Discord. Select your previously created application. Click on the menu item "<b>Bot</b>" in the left menu. Click on the button "<b>Reset Token</b>". Now you will be asked if you really want to do it, confirm with "<b>Yes, do it!</b>". If you have enabled two-factor authentication on Discord, you will now need to enter your code and press "<b>Submit</b>". You will now see your token. With a click on the button "<b>Copy</b>", you can paste the token directly into the clipboard.]]></item>
@@ -97,6 +96,8 @@
9796
Go the <a href="https://discord.com/developers/applications" class="externalURL" rel="noopener noreferrer">Developer Portal</a> from Discord. Select your previously created application. Click on the menu item "<b>General Information</b>" in the left menu. Here you will also find the public key. With a click on the button "<b>Copy</b>", you can paste the public key directly into the clipboard. Also, for "Interactions Endpoint Url" add the URL <kbd>{link controller='DiscordInteraction' forceFrontend=true application='wcf'}{/link}</kbd>.]]></item>
9897
<item name="wcf.acp.discordBotAddManager.step6Intro"><![CDATA[Your Discord bot has been successfully created and configured.]]></item>
9998
<item name="wcf.acp.discordBotAddManager.step6Outro"><![CDATA[Congratulations, you are now done with the configuration wizard and ready to use your Discord Bot.]]></item>
99+
<item name="wcf.acp.discordBotAddManager.noGatewaysNeeded"><![CDATA[It is not necessary to activate Privilaged Gateway Intents for the installed plugins.]]></item>
100+
<item name="wcf.acp.discordBotAddManager.gatewaysNeeded"><![CDATA[The following Privilaged Gateway intents must be activated for the installed plugins:]]></item>
100101
</category>
101102
<category name="wcf.acp.discordBotList">
102103
<item name="wcf.acp.discordBotList.botName"><![CDATA[Bot Name]]></item>

0 commit comments

Comments
 (0)