Skip to content

Commit

Permalink
feat: Add slash commands (#105)
Browse files Browse the repository at this point in the history
# New Features
- Added new class Respondable. What it does? It is for sending replies with same method for Message and Interaction classes. 86f86dd
- Some commands are updated for slash commands. Added $options property to them. c40c890
- CommandLoader class updates for automatic saving slash commands cafcf88
- Changed commands' $msg and $args variable types 2a25a04
- Added latest changes from master #104
- Added slash commands registration system. There is a variable named 'allowed_categories'. If command's category is allowed. It will be registered. If not it will be deleted from slash commands. d136a20
## Removed Features
- RPG commands has been deleted. d201d9e
  • Loading branch information
bariscodefxy authored Jun 2, 2024
1 parent d7ea4f0 commit 8e4ef06
Show file tree
Hide file tree
Showing 45 changed files with 772 additions and 1,086 deletions.
13 changes: 6 additions & 7 deletions bot.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

/**
* Copyright 2021-2024 bariscodefx
Expand Down Expand Up @@ -28,9 +28,9 @@
use Discord\WebSockets\Intents;
use hiro\Version;

if ( !isset( $_ENV['TOKEN'] ) ) {
$dotenv = Dotenv\Dotenv::createImmutable("./");
$dotenv->load();
if (!isset($_ENV['TOKEN'])) {
$dotenv = Dotenv\Dotenv::createImmutable("./");
$dotenv->load();
}

if ( Version::TYPE == 'development' )
Expand All @@ -40,6 +40,7 @@
error_reporting(0);
}

global $shard_id, $shard_count;
$ArgumentParser = new ArgumentParser($argv);
$shard_id = $ArgumentParser->getShardId();
$shard_count = $ArgumentParser->getShardCount();
Expand Down Expand Up @@ -78,11 +79,9 @@ function getPresenceState(): ?array
->startThread();

/** fix discord guild count */
$discord->getLoop()->addPeriodicTimer($presenceManager->looptime, function() use ($presenceManager, $discord)
{
$discord->getLoop()->addPeriodicTimer($presenceManager->looptime, function() use ($presenceManager) {
$presenceManager->setPresences(getPresenceState());
});

});

$bot->run();
15 changes: 12 additions & 3 deletions src/commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
use hiro\interfaces\CommandInterface;
use hiro\interfaces\HiroInterface;
use Discord\Discord;
use Discord\Helpers\Collection;
use hiro\parts\CommandLoader;
use hiro\parts\Respondable;

/**
* Command
Expand Down Expand Up @@ -58,6 +60,13 @@ class Command implements CommandInterface {
*/
public $aliases = [];

/**
* Options
*
* @var array<Option[]>
*/
public array $options = [];

/**
* CommandLoader
*
Expand Down Expand Up @@ -106,11 +115,11 @@ public function configure(): void
/**
* handle
*
* @param [type] $msg
* @param [type] $args
* @param Respondable $msg
* @param array|Collection $args
* @return void
*/
public function handle($msg, $args): void
public function handle(Respondable $msg, array|Collection $args): void
{

}
Expand Down
27 changes: 21 additions & 6 deletions src/commands/author/Botban.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

namespace hiro\commands;

use Discord\Helpers\Collection;
use hiro\security\AuthorCommand;
use hiro\database\Database;
use Discord\Parts\Interactions\Command\Option;

/**
* Botban
Expand All @@ -39,6 +41,13 @@ public function configure(): void
$this->description = "Ban/unban a player from bot. **ONLY FOR AUTHOR**";
$this->aliases = [];
$this->category = "author";
$this->options = [
(new Option($this->discord))
->setType(Option::USER)
->setName('user')
->setDescription('User to ban/unban')
->setRequired(true)
];
}

/**
Expand All @@ -50,29 +59,35 @@ public function configure(): void
*/
public function handle($msg, $args): void
{
$user = $msg->mentions->first();
$user = null;
if ($args instanceof Collection && $args->get('name', 'user') !== null) {
$user = $msg->mentions->first() ?? $this->discord->members->get('id', $args->get('name', 'user')->value);
} else if (is_array($args)) {
$user = $msg->mentions->first();
}

if (!$user) {
$msg->channel->sendMessage("You should mention a user to ban.");
$msg->reply("You should mention a user to ban.");
return;
}

if ($user->id == $msg->author->id) {
$msg->channel->sendMessage("You can't ban yourself.");
$msg->reply("You can't ban yourself.");
return;
}

$db = new Database();
if (!$db->isConnected) {
$msg->channel->sendMessage("Couldn't connect to database.");
$msg->reply("Couldn't connect to database.");
return;
}

if (!$db->isUserBannedFromBot($user->id)) {
$db->banUserFromBot($user->id);
$msg->channel->sendMessage("User has been banned.");
$msg->reply("{$user->username} has been banned.");
} else {
$db->unbanUserFromBot($user->id);
$msg->channel->sendMessage("User's ban has been removed.");
$msg->reply("{$user->username}'s ban has been removed.");
}
}
}
20 changes: 17 additions & 3 deletions src/commands/author/Exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace hiro\commands;

use Discord\Helpers\Collection;
use Discord\Parts\Interactions\Command\Option;
use hiro\security\AuthorCommand;
use React\ChildProcess\Process;

Expand All @@ -39,6 +41,13 @@ public function configure(): void
$this->description = "Executes an command **ONLY FOR AUTHOR**";
$this->aliases = ["execute", "shell-exec"];
$this->category = "author";
$this->options = [
(new Option($this->discord))
->setType(Option::STRING)
->setName('command')
->setDescription('Command to execute')
->setRequired(true)
];
}

/**
Expand All @@ -50,10 +59,15 @@ public function configure(): void
*/
public function handle($msg, $args): void
{
$ex = implode(' ', $args);

$ex = null;
if ($args instanceof Collection && $args->get('name', 'command') !== null) {
$ex = $args->get('name', 'command')->value;
} else if (is_array($args)) {
$ex = implode(' ', $args);
}

if (!$ex) $ex = " ";

$process = new Process($ex);
$process->start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

namespace hiro\commands;

use Discord\Parts\Embed\Embed;
use hiro\database\Database;
use Discord\Builders\MessageBuilder;
use Discord\Helpers\Collection;
use Discord\Parts\Interactions\Command\Option;

/**
* Coinflip
Expand All @@ -39,7 +40,14 @@ public function configure(): void
$this->command = "coinflip";
$this->description = "An economy game";
$this->aliases = ["cf"];
$this->category = "utility";
$this->category = "economy";
$this->options = [
(new Option($this->discord))
->setType(Option::INTEGER)
->setName('amount')
->setDescription('Amount of money to bet')
->setRequired(true)
];
}

/**
Expand Down Expand Up @@ -68,29 +76,36 @@ public function handle($msg, $args): void
$usermoney = 0;
}
}
if (!$args[0] || !is_numeric($args[0])) {

if ($args instanceof Collection && $args->get('name', 'amount') !== null) {
$amount = $args->get('name', 'amount')->value;
} else if (is_array($args)) {
$amount = $args[0] ?? null;
}
$amount ??= null;

if (!$amount || !is_numeric($amount)) {
$msg->reply($language->getTranslator()->trans('commands.coinflip.no_amount'));
} else {
if ($args[0] <= 0) {
if ($amount <= 0) {
$msg->reply($language->getTranslator()->trans('commands.coinflip.too_less_amount'));
} else if ($args[0] > $usermoney) {
} else if ($amount > $usermoney) {
$msg->reply($language->getTranslator()->trans('global.not_enough_money'));
} else {
$payamount = $args[0];
$rand = random_int(0, 1);

// delete user money from payamount
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney - $payamount);
$usermoney -= $payamount;
// delete user money from ammount
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney - $amount);
$usermoney -= $amount;

$msg->reply($language->getTranslator()->trans('commands.coinflip.coin_spinning') . " <a:hirocoinflip:1130395266105737256>")->then(function($botreply) use ($msg, $rand, $database, $usermoney, $payamount, $language){
$this->discord->getLoop()->addTimer(2.0, function() use ($botreply, $msg, $rand, $database, $usermoney, $payamount, $language){
$msg->reply($language->getTranslator()->trans('commands.coinflip.coin_spinning') . " <a:hirocoinflip:1130395266105737256>")->then(function ($botreply) use ($msg, $rand, $database, $usermoney, $amount, $language) {
$this->discord->getLoop()->addTimer(2.0, function () use ($botreply, $msg, $rand, $database, $usermoney, $amount, $language) {
setlocale(LC_MONETARY, 'en_US');
if ($rand) {
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney + $payamount * 2);
$botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.win') . " +" . $payamount*2 . " <:hirocoin:1130392530677157898>"));
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney + $amount * 2);
if ($botreply) $botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.win') . " +" . $amount * 2 . " <:hirocoin:1130392530677157898>"));
} else {
$botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.lose') . " -" . $payamount . " <:hirocoin:1130392530677157898>"));
if ($botreply) $botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.lose') . " -" . $amount . " <:hirocoin:1130392530677157898>"));
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function configure(): void
$this->command = "daily";
$this->description = "Daily moneys.";
$this->aliases = [];
$this->category = "utility";
$this->category = "economy";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function configure(): void
$this->command = "money";
$this->description = "Displays your money.";
$this->aliases = ["cash"];
$this->category = "utility";
$this->category = "economy";
}

/**
Expand Down
38 changes: 31 additions & 7 deletions src/commands/utility/Pay.php → src/commands/economy/Pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

namespace hiro\commands;

use Discord\Helpers\Collection;
use Discord\Parts\Embed\Embed;
use Discord\Parts\Interactions\Command\Option;
use hiro\database\Database;

class Pay extends Command
Expand All @@ -35,7 +37,19 @@ public function configure(): void
$this->command = "pay";
$this->description = "Send your money to anybody.";
$this->aliases = [];
$this->category = "utility";
$this->category = "economy";
$this->options = [
(new Option($this->discord))
->setType(Option::USER)
->setName('user')
->setDescription('User to send money')
->setRequired(true),
(new Option($this->discord))
->setType(Option::INTEGER)
->setName('amount')
->setDescription('Amount of money')
->setRequired(true)
];
}

/**
Expand All @@ -53,8 +67,12 @@ public function handle($msg, $args): void
$msg->reply($language->getTranslator()->trans('database.notconnect'));
return;
}
$embed = new Embed($this->discord);
$user = $msg->mentions->first();
if ($args instanceof Collection && $args->get('name', 'user') !== null) {
$user = $this->discord->users->get('id', $args->get('name', 'user')->value);
} else {
$user = $msg->mentions->first() ?? null;
}
$user ??= null;
if (!$user) {
$msg->reply($language->getTranslator()->trans('commands.pay.no_user'));
return;
Expand All @@ -63,20 +81,26 @@ public function handle($msg, $args): void
$msg->reply($language->getTranslator()->trans('commands.pay.selfsend'));
return;
}
if (!isset($args[1]) && !is_numeric($args[1])) {
if($args instanceof Collection && $args->get('name', 'amount') !== null) {
$amount = $args->get('name', 'amount')->value;
} else if (is_array($args)) {
$amount = explode("$user ", implode(' ', $args))[1] ?? null;
}
$amount ??= null;
if (!isset($amount) && !is_numeric($amount)) {
$msg->reply($language->getTranslator()->trans('commands.pay.no_numeric_arg'));
return;
}
if (!$database->pay($database->getUserIdByDiscordId($msg->author->id), $database->getUserIdByDiscordId($user->id), $args[1])) {
if (!$database->pay($database->getUserIdByDiscordId($msg->author->id), $database->getUserIdByDiscordId($user->id), $amount)) {
$msg->reply($language->getTranslator()->trans('commands.pay.fail_msg'));
return;
}
setlocale(LC_MONETARY, 'en_US');
$msg->reply(
sprintf(
$language->getTranslator()->trans('commands.pay.pay_msg'),
$msg->user->username, number_format($args[1], 2, ',', '.'), "<:hirocoin:1130392530677157898>",
$user->username, number_format($args[1], 2, ',', '.'), "<:hirocoin:1130392530677157898>"
$msg->user->username, number_format($amount, 2, ',', '.'), "<:hirocoin:1130392530677157898>",
$user->username, number_format($amount, 2, ',', '.'), "<:hirocoin:1130392530677157898>"
)
);
return;
Expand Down
Loading

0 comments on commit 8e4ef06

Please sign in to comment.