Skip to content

Commit a4ad10e

Browse files
Minor Revamp
* Renamed `newPartDiscord` to `newDiscordPart` * Added `getDiscord` * Removed `createLocalFileAttachment` * Added `index.php` to files that are sniffed * Renamed `init` event class to `Init` * Helper Function docs are now generated
2 parents 2a74b6c + 08e5c30 commit a4ad10e

File tree

14 files changed

+566
-137
lines changed

14 files changed

+566
-137
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
env.json
33
.idea/
44
.vscode/
5+
tools/HelperDocs.md

Commands/Guild.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Commands;
4+
5+
use Discord\Builders\CommandBuilder;
6+
use Discord\Builders\Components\Button;
7+
use Discord\Builders\MessageBuilder;
8+
use Discord\Parts\Embed\Embed;
9+
use Discord\Parts\Interactions\Interaction;
10+
use Discord\Parts\User\User;
11+
use Throwable;
12+
13+
use function Common\buildActionRowWithButtons;
14+
use function Common\emptyEmbedField;
15+
use function Common\getDiscord;
16+
use function Common\newButton;
17+
use function Common\newDiscordPart;
18+
use function React\Async\await;
19+
20+
class Guild extends BaseCommand
21+
{
22+
public const VIEW_EMOJIS = 1;
23+
public const VIEW_ICON = 2;
24+
25+
protected static array|string $name = 'guild';
26+
27+
public static function handler(Interaction $interaction): void
28+
{
29+
/** @var Embed $embed */
30+
/** @var User $owner */
31+
$guild = $interaction->guild;
32+
$embed = newDiscordPart(Embed::class);
33+
$msg = new MessageBuilder;
34+
35+
36+
if ($owner = $guild->owner) {
37+
$embed->addFieldValues('Owner', $owner);
38+
}
39+
40+
$embed
41+
->setAuthor($guild->name, $guild->icon, $guild->invites->first())
42+
->setDescription($guild->description ?? "")
43+
->addFieldValues('Member Count', $guild->member_count, true)
44+
->addFieldValues('Channel Count', $guild->channels->count(), true)
45+
->addFieldValues("Emoji Count", $guild->emojis->count(), true)
46+
->addFieldValues("Snowflake", "`{$guild->id}`", true)
47+
48+
->setFooter("Created ")
49+
->setTimestamp($guild->createdTimestamp())
50+
;
51+
$msg->addEmbed($embed);
52+
53+
$newButton = fn(string $action, string $actionId) => newButton(Button::STYLE_PRIMARY, $action, "GuildInfo|{$actionId}");
54+
55+
$msg->addComponent(
56+
buildActionRowWithButtons(
57+
$newButton("View Icon", self::VIEW_ICON),
58+
$newButton("View Emojis", self::VIEW_EMOJIS),
59+
)
60+
);
61+
62+
$interaction->respondWithMessage($msg);
63+
}
64+
65+
public static function autocomplete(Interaction $interaction): void
66+
{
67+
}
68+
69+
public static function getConfig(): CommandBuilder|array
70+
{
71+
return (new CommandBuilder())
72+
->setName(self::getBaseCommandName())
73+
->setDescription('Get info about the current guild')
74+
;
75+
}
76+
}

Commands/Ping.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,17 @@
33
namespace Commands;
44

55
use Discord\Builders\CommandBuilder;
6-
use Discord\Builders\Components\ActionRow;
7-
use Discord\Builders\Components\Button;
8-
use Discord\Builders\MessageBuilder;
96
use Discord\Parts\Interactions\Interaction;
107

11-
use function Common\newButton;
12-
use function Common\buildActionRowWithButtons;
8+
use function Common\messageWithContent;
139

1410
class Ping extends BaseCommand
1511
{
1612
protected static string|array $name = "ping";
1713

1814
public static function handler(Interaction $interaction): void
1915
{
20-
$interaction->respondWithMessage(MessageBuilder::new()
21-
->setContent('Pong :ping_pong:')
22-
->addComponent(self::getActionRow(0, false)), true)
23-
;
24-
}
25-
26-
public static function getActionRow(int $times, bool $ping): ActionRow
27-
{
28-
$button = ($ping) ?
29-
newButton(Button::STYLE_PRIMARY, "Ping", "Ping|{$times}") :
30-
newButton(Button::STYLE_SECONDARY, "Pong", "Pong|{$times}");
31-
32-
return buildActionRowWithButtons($button);
16+
$interaction->respondWithMessage(messageWithContent("Ping :ping_pong:"), true);
3317
}
3418

3519
public static function getConfig(): CommandBuilder|array

Common/Helpers.php

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,79 @@
66
use Discord\Builders\Components\ActionRow;
77
use Discord\Builders\Components\Button;
88
use Discord\Builders\MessageBuilder;
9+
use Discord\Discord;
910
use Discord\Helpers\Collection;
10-
use Discord\Parts\Channel\Attachment;
1111
use Discord\Parts\Embed\Embed;
1212
use Discord\Parts\Interactions\Command\Choice;
1313
use Discord\Parts\Interactions\Command\Option;
1414
use Discord\Parts\Interactions\Interaction;
15-
use Discord\Parts\Interactions\Request\Option as RequestOption;
1615

17-
function newOption(string $name, string $description, int $type, bool $required = false): Option
16+
/**
17+
* Create a new Option used for building slash commands
18+
*/
19+
function newSlashCommandOption(string $name, string $description, int $type, bool $required = false): Option
1820
{
19-
return newPartDiscord(Option::class)
21+
return newDiscordPart(Option::class)
2022
->setName($name)
2123
->setDescription($description)
2224
->setType($type)
2325
->setRequired($required)
2426
;
2527
}
2628

27-
function newChoice(string $name, float|int|string $value): Choice
29+
/**
30+
* Create a new Choice used for building slash commands
31+
*/
32+
function newSlashCommandChoice(string $name, float|int|string $value): Choice
2833
{
29-
return newPartDiscord(Choice::class)
34+
return newDiscordPart(Choice::class)
3035
->setName($name)
3136
->setValue($value)
3237
;
3338
}
3439

35-
function newPartDiscord(string $class, mixed ...$args): mixed
40+
/**
41+
* Create a new instance of an object that requires `\Discord\Discord` as the first argument
42+
*
43+
* ```php
44+
* $embed = newDiscordPart("\Discord\Parts\Embed\Embed);
45+
* ```
46+
*/
47+
function newDiscordPart(string $class, mixed ...$args): mixed
3648
{
3749
return (new $class(Env::get()->discord, ...$args));
3850
}
3951

52+
/**
53+
* Create a new MessageBuilder object with the content define for creating simple MessageBuilders quickly
54+
*
55+
* ```php
56+
* $message = messageWithContent("Hello World");
57+
* ```
58+
*/
4059
function messageWithContent(string $content): MessageBuilder
4160
{
4261
return MessageBuilder::new()->setContent($content);
4362
}
4463

45-
function createLocalFileAttachment(string $fileName): Attachment
46-
{
47-
return new Attachment(Env::get()->discord, [
48-
"filename" => $fileName
49-
]);
50-
}
51-
64+
/**
65+
* Quickly build an action row with multiple buttons
66+
*
67+
* ```php
68+
* $banButton = (new Button(Button::STYLE_DANGER))->setLabel("Ban User");
69+
* $kickButton = (new Button(Button::STYLE_DANGER))->setLabel("Kick User");
70+
* $actionRow = buildActionRowWithButtons($banButton, $kickButton);
71+
* ```
72+
*
73+
* *This can also be paired with newButton*
74+
*
75+
* ```php
76+
* $actionRow = buildActionWithButtons(
77+
* newButton(Button::STYLE_DANGER, "Ban User")
78+
* newButton(Button::STYLE_DANGER, "Kick User")
79+
* );
80+
* ```
81+
*/
5282
function buildActionRowWithButtons(Button ...$buttons): ActionRow
5383
{
5484
$actionRow = new ActionRow();
@@ -60,12 +90,36 @@ function buildActionRowWithButtons(Button ...$buttons): ActionRow
6090
return $actionRow;
6191
}
6292

93+
/**
94+
* Quickly create button objects
95+
*
96+
* ```php
97+
* $button = newButton(Button::STYLE_DANGER, "Kick User", "Kick|Command_String");
98+
* ```
99+
*/
63100
function newButton(int $style, string $label, ?string $custom_id = null): Button
64101
{
65102
return (new Button($style, $custom_id))->setLabel($label);
66103
}
67104

68-
function getOptionFromInteraction(Collection|Interaction $options, string ...$names): ?RequestOption
105+
/**
106+
* Get an option from an Interaction/Interaction Repository by specifying the option(s) name
107+
*
108+
* For regular slash commands
109+
* `/ban :user`
110+
*
111+
* ```php
112+
* $user = getOptionFromInteraction($interaction, "user");
113+
* ```
114+
*
115+
* For sub commands / sub command groups you can stack the names
116+
* `/admin ban :user`
117+
*
118+
* ```php
119+
* $user = getOptionFromInteraction($interaction->data->options, "ban", "user");
120+
* ```
121+
*/
122+
function getOptionFromInteraction(Collection|Interaction $options, string ...$names): Option|null
69123
{
70124
if ($options instanceof Interaction) {
71125
$options = $options->data->options;
@@ -87,6 +141,22 @@ function getOptionFromInteraction(Collection|Interaction $options, string ...$na
87141
return $option;
88142
}
89143

144+
/**
145+
* Append to grab and empty array field. You can supply an embed to have the empty field added or
146+
* if you leave the `$embed` option `null` then an array containing the empty field will be returned
147+
*
148+
* ```php
149+
* $embed = newDiscordPart("\Discord\Parts\Embed\Embed");
150+
* emptyEmbedField($embed);
151+
* ```
152+
*
153+
* or
154+
*
155+
* ```php
156+
* $embed = newDiscordPart("\Discord\Parts\Embed\Embed");
157+
* $emptyField = emptyEmbedField();
158+
* ```
159+
*/
90160
function emptyEmbedField(?Embed $embed = null): array|Embed
91161
{
92162
$emptyField = ["name" => "\u{200b}", "value" => "\u{200b}"];
@@ -97,3 +167,11 @@ function emptyEmbedField(?Embed $embed = null): array|Embed
97167

98168
return $emptyField;
99169
}
170+
171+
/**
172+
* Retrieve the `\Discord\Discord` instance from Environment
173+
*/
174+
function getDiscord(): Discord
175+
{
176+
return Env::get()->discord;
177+
}

Interactions/GuildInfo.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Interactions;
4+
5+
use Commands\Guild as GuildCommand;
6+
use Discord\Builders\MessageBuilder;
7+
use Discord\Parts\Interactions\Interaction;
8+
use Discord\Discord;
9+
use Discord\Parts\Embed\Embed;
10+
use Discord\Parts\Guild\Emoji;
11+
12+
use function Common\messageWithContent;
13+
use function Common\newDiscordPart;
14+
15+
class GuildInfo extends BaseInteraction
16+
{
17+
protected static string $id = "GuildInfo";
18+
19+
public static function handler(Interaction $interaction, Discord $discord, int $actionId = 0)
20+
{
21+
/** @var Embed $embed */
22+
if ($actionId < 1 || $actionId > 2) {
23+
$interaction->respondWithMessage(messageWithContent("Invalid Action"), true);
24+
return;
25+
}
26+
27+
$msg = new MessageBuilder;
28+
$embed = newDiscordPart(Embed::class);
29+
$guild = $interaction->guild;
30+
31+
$embed->setAuthor($guild->name, $guild->icon, $guild->invites->first());
32+
33+
if ($actionId === GuildCommand::VIEW_EMOJIS) {
34+
/** @var Emoji $emoji */
35+
$emojis = $guild->emojis;
36+
37+
$emojiString = "";
38+
39+
foreach ($emojis as $emoji) {
40+
$emojiString .= "`{$emoji}` {$emoji}\n";
41+
}
42+
43+
$embed->setTitle("{$guild->name} Emojis")->setDescription($emojiString);
44+
} else if ($actionId === GuildCommand::VIEW_ICON) {
45+
$embed->setTitle("{$guild->name} Icon")->setImage($guild->icon);
46+
}
47+
48+
$msg->addEmbed($embed);
49+
50+
$interaction->respondWithMessage($msg, true);
51+
}
52+
}

Interactions/Ping.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

Interactions/Pong.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)