Skip to content

Commit

Permalink
base code for slash commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
bariscodefxy committed Jul 30, 2023
1 parent a072daa commit ed8a387
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 46 deletions.
1 change: 0 additions & 1 deletion bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Hiro.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/parts/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace hiro\parts;

use hiro\interfaces\HiroInterface;

class Command extends \Discord\CommandClient\Command
{

public function __construct(
HiroInterface $client,
string $command,
callable $callable,
string $description,
string $longDescription,
string $usage,
int $cooldown,
string $cooldownMessage,
bool $showHelp = true
) {
$this->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;
}

}
108 changes: 64 additions & 44 deletions src/parts/CommandLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use hiro\database\Database;
use hiro\interfaces\HiroInterface;
use Discord\Builders\MessageBuilder;

/**
* CommandLoader
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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 . " ")));
// });
}

/**
Expand Down

0 comments on commit ed8a387

Please sign in to comment.