From ed8a3871adfeac3f5eee8b0019b2a16737ca08a4 Mon Sep 17 00:00:00 2001 From: bariscodefx Date: Sun, 30 Jul 2023 21:08:10 +0300 Subject: [PATCH] base code for slash commands. --- bot.php | 1 - src/Hiro.php | 2 +- src/parts/Command.php | 32 +++++++++++ src/parts/CommandLoader.php | 108 +++++++++++++++++++++--------------- 4 files changed, 97 insertions(+), 46 deletions(-) create mode 100644 src/parts/Command.php diff --git a/bot.php b/bot.php index 3b44101..e84a6ab 100644 --- a/bot.php +++ b/bot.php @@ -25,7 +25,6 @@ use hiro\parts\CommandLoader; use hiro\parts\ArgumentParser; use hiro\parts\PresenceManager; -use hiro\parts\voice\VoiceSettings; use Discord\WebSockets\Intents; use hiro\Version; diff --git a/src/Hiro.php b/src/Hiro.php index 978ec85..eb9869b 100644 --- a/src/Hiro.php +++ b/src/Hiro.php @@ -22,7 +22,7 @@ use Discord\Discord; use hiro\interfaces\HiroInterface; -use Discord\CommandClient\Command; +use hiro\parts\Command; use hiro\database\Database; use Discord\Parts\Embed\Embed; use Discord\WebSockets\Intents; diff --git a/src/parts/Command.php b/src/parts/Command.php new file mode 100644 index 0000000..8a603bf --- /dev/null +++ b/src/parts/Command.php @@ -0,0 +1,32 @@ +client = $client; + $this->command = $command; + $this->callable = $callable; + $this->description = $description; + $this->longDescription = $longDescription; + $this->usage = $usage; + $this->cooldown = $cooldown; + $this->cooldownMessage = $cooldownMessage; + $this->showHelp = $showHelp; + } + +} \ No newline at end of file diff --git a/src/parts/CommandLoader.php b/src/parts/CommandLoader.php index 8940640..331066c 100644 --- a/src/parts/CommandLoader.php +++ b/src/parts/CommandLoader.php @@ -22,6 +22,7 @@ use hiro\database\Database; use hiro\interfaces\HiroInterface; +use Discord\Builders\MessageBuilder; /** * CommandLoader @@ -56,7 +57,7 @@ class CommandLoader public function __construct(HiroInterface $client) { $this->client = $client; - $this->dir = __DIR__ . "/commands"; + $this->dir = dirname(__DIR__, 1) . "/commands"; $this->loadAllCommands(); } @@ -155,62 +156,81 @@ public function getCmd($cmd_name) */ public function loadCommand($cmd) { - $this->client->registerCommand( - $cmd->command, - function ($msg, $args) use ($cmd) { - try { - if ($cmd->category == "rpg") { - $database = new Database(); + $command = $cmd->command; - if ($database->isConnected) { - $rpgenabled = $database->getRPGEnabledForServer($database->getServerIdByDiscordId($msg->guild->id)); - $rpgchannel = $database->getRPGChannelForServer($database->getServerIdByDiscordId($msg->guild->id)); + $closure = function ($msg, $args) use ($cmd) { + try { + if ($cmd->category == "rpg") { + $database = new Database(); - if ($cmd->command != "setrpgchannel" && $cmd->command != "setrpgenabled") { - if (!$rpgenabled) { - $msg->reply('RPG commands is not enabled in this server.'); - return; - } elseif (!$rpgchannel) { - $msg->reply('RPG commands channel is not available for this server.'); - return; - } elseif ($rpgchannel != $msg->channel->id) { - $msg->reply('You should use this command in <#' . $rpgchannel . '>'); // may be problems if channel was deleted. - return; - } + if ($database->isConnected) { + $rpgenabled = $database->getRPGEnabledForServer($database->getServerIdByDiscordId($msg->guild->id)); + $rpgchannel = $database->getRPGChannelForServer($database->getServerIdByDiscordId($msg->guild->id)); + + if ($cmd->command != "setrpgchannel" && $cmd->command != "setrpgenabled") { + if (!$rpgenabled) { + $msg->reply('RPG commands is not enabled in this server.'); + return; + } elseif (!$rpgchannel) { + $msg->reply('RPG commands channel is not available for this server.'); + return; + } elseif ($rpgchannel != $msg->channel->id) { + $msg->reply('You should use this command in <#' . $rpgchannel . '>'); // may be problems if channel was deleted. + return; + } - if ($cmd->command != "createchar") { - $charType = $database->getRPGCharType($database->getUserIdByDiscordId($msg->author->id)); - $charNation = $database->getRPGCharRace($database->getUserIdByDiscordId($msg->author->id)); - $charGender = $database->getRPGCharGender($database->getUserIdByDiscordId($msg->author->id)); + if ($cmd->command != "createchar") { + $charType = $database->getRPGCharType($database->getUserIdByDiscordId($msg->author->id)); + $charNation = $database->getRPGCharRace($database->getUserIdByDiscordId($msg->author->id)); + $charGender = $database->getRPGCharGender($database->getUserIdByDiscordId($msg->author->id)); - if (!$charType || !$charNation || !$charGender) { - $msg->reply('You must create your character first!'); - return; - } + if (!$charType || !$charNation || !$charGender) { + $msg->reply('You must create your character first!'); + return; } } } } + } - $database = new Database(); + $database = new Database(); - if (!$database->isUserBannedFromBot($msg->author->id)) { - $cmd->handle($msg, $args); - } - } catch (\Throwable $e) { - if (\hiro\Version::TYPE == 'development') { - echo $e; - } - $msg->reply("ERROR: `" . $e->getMessage() . "`"); + if (!$database->isUserBannedFromBot($msg->author->id)) { + $cmd->handle($msg, $args); + } + } catch (\Throwable $e) { + if (\hiro\Version::TYPE == 'development') { + echo $e; } - }, - [ - 'aliases' => $cmd->aliases, - 'description' => $cmd->description, - 'cooldown' => $cmd->cooldown ?? 0 - ] + $msg->reply("ERROR: `" . $e->getMessage() . "`"); + } + }; + + $options = [ + 'aliases' => $cmd->aliases, + 'description' => $cmd->description, + 'cooldown' => $cmd->cooldown ?? 0 + ]; + + $this->client->registerCommand( + $command, + $closure, + $options ); + + // $command_for_slash = Discord\Parts\Interactions\Command\Command($this->client, $options); + // $this->client->application->commands->save( + // $this->client->application->commands->create( + // CommandBuilder::new() + // ->setName($command) + // ->setDescription($cmd->description) + // ->toArray() + // ) + // ); + // $this->client->listenCommand($command, function(Interaction $interaction) use ($closure, $command) { + // {$closure}($interaction->message, substr($interaction->message->content, strlen($this->client->prefix . $command . " "))); + // }); } /**