Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 8e4ef06

Browse files
author
bariscodefx
authored
feat: Add slash commands (#105)
# 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
1 parent d7ea4f0 commit 8e4ef06

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+772
-1086
lines changed

bot.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
/**
44
* Copyright 2021-2024 bariscodefx
@@ -28,9 +28,9 @@
2828
use Discord\WebSockets\Intents;
2929
use hiro\Version;
3030

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

3636
if ( Version::TYPE == 'development' )
@@ -40,6 +40,7 @@
4040
error_reporting(0);
4141
}
4242

43+
global $shard_id, $shard_count;
4344
$ArgumentParser = new ArgumentParser($argv);
4445
$shard_id = $ArgumentParser->getShardId();
4546
$shard_count = $ArgumentParser->getShardCount();
@@ -78,11 +79,9 @@ function getPresenceState(): ?array
7879
->startThread();
7980

8081
/** fix discord guild count */
81-
$discord->getLoop()->addPeriodicTimer($presenceManager->looptime, function() use ($presenceManager, $discord)
82-
{
82+
$discord->getLoop()->addPeriodicTimer($presenceManager->looptime, function() use ($presenceManager) {
8383
$presenceManager->setPresences(getPresenceState());
8484
});
85-
8685
});
8786

8887
$bot->run();

src/commands/Command.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
use hiro\interfaces\CommandInterface;
2424
use hiro\interfaces\HiroInterface;
2525
use Discord\Discord;
26+
use Discord\Helpers\Collection;
2627
use hiro\parts\CommandLoader;
28+
use hiro\parts\Respondable;
2729

2830
/**
2931
* Command
@@ -58,6 +60,13 @@ class Command implements CommandInterface {
5860
*/
5961
public $aliases = [];
6062

63+
/**
64+
* Options
65+
*
66+
* @var array<Option[]>
67+
*/
68+
public array $options = [];
69+
6170
/**
6271
* CommandLoader
6372
*
@@ -106,11 +115,11 @@ public function configure(): void
106115
/**
107116
* handle
108117
*
109-
* @param [type] $msg
110-
* @param [type] $args
118+
* @param Respondable $msg
119+
* @param array|Collection $args
111120
* @return void
112121
*/
113-
public function handle($msg, $args): void
122+
public function handle(Respondable $msg, array|Collection $args): void
114123
{
115124

116125
}

src/commands/author/Botban.php

+21-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
namespace hiro\commands;
2222

23+
use Discord\Helpers\Collection;
2324
use hiro\security\AuthorCommand;
2425
use hiro\database\Database;
26+
use Discord\Parts\Interactions\Command\Option;
2527

2628
/**
2729
* Botban
@@ -39,6 +41,13 @@ public function configure(): void
3941
$this->description = "Ban/unban a player from bot. **ONLY FOR AUTHOR**";
4042
$this->aliases = [];
4143
$this->category = "author";
44+
$this->options = [
45+
(new Option($this->discord))
46+
->setType(Option::USER)
47+
->setName('user')
48+
->setDescription('User to ban/unban')
49+
->setRequired(true)
50+
];
4251
}
4352

4453
/**
@@ -50,29 +59,35 @@ public function configure(): void
5059
*/
5160
public function handle($msg, $args): void
5261
{
53-
$user = $msg->mentions->first();
62+
$user = null;
63+
if ($args instanceof Collection && $args->get('name', 'user') !== null) {
64+
$user = $msg->mentions->first() ?? $this->discord->members->get('id', $args->get('name', 'user')->value);
65+
} else if (is_array($args)) {
66+
$user = $msg->mentions->first();
67+
}
68+
5469
if (!$user) {
55-
$msg->channel->sendMessage("You should mention a user to ban.");
70+
$msg->reply("You should mention a user to ban.");
5671
return;
5772
}
5873

5974
if ($user->id == $msg->author->id) {
60-
$msg->channel->sendMessage("You can't ban yourself.");
75+
$msg->reply("You can't ban yourself.");
6176
return;
6277
}
6378

6479
$db = new Database();
6580
if (!$db->isConnected) {
66-
$msg->channel->sendMessage("Couldn't connect to database.");
81+
$msg->reply("Couldn't connect to database.");
6782
return;
6883
}
6984

7085
if (!$db->isUserBannedFromBot($user->id)) {
7186
$db->banUserFromBot($user->id);
72-
$msg->channel->sendMessage("User has been banned.");
87+
$msg->reply("{$user->username} has been banned.");
7388
} else {
7489
$db->unbanUserFromBot($user->id);
75-
$msg->channel->sendMessage("User's ban has been removed.");
90+
$msg->reply("{$user->username}'s ban has been removed.");
7691
}
7792
}
7893
}

src/commands/author/Exec.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
namespace hiro\commands;
2222

23+
use Discord\Helpers\Collection;
24+
use Discord\Parts\Interactions\Command\Option;
2325
use hiro\security\AuthorCommand;
2426
use React\ChildProcess\Process;
2527

@@ -39,6 +41,13 @@ public function configure(): void
3941
$this->description = "Executes an command **ONLY FOR AUTHOR**";
4042
$this->aliases = ["execute", "shell-exec"];
4143
$this->category = "author";
44+
$this->options = [
45+
(new Option($this->discord))
46+
->setType(Option::STRING)
47+
->setName('command')
48+
->setDescription('Command to execute')
49+
->setRequired(true)
50+
];
4251
}
4352

4453
/**
@@ -50,10 +59,15 @@ public function configure(): void
5059
*/
5160
public function handle($msg, $args): void
5261
{
53-
$ex = implode(' ', $args);
54-
62+
$ex = null;
63+
if ($args instanceof Collection && $args->get('name', 'command') !== null) {
64+
$ex = $args->get('name', 'command')->value;
65+
} else if (is_array($args)) {
66+
$ex = implode(' ', $args);
67+
}
68+
5569
if (!$ex) $ex = " ";
56-
70+
5771
$process = new Process($ex);
5872
$process->start();
5973

src/commands/utility/Coinflip.php src/commands/economy/Coinflip.php

+29-14
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121
namespace hiro\commands;
2222

23-
use Discord\Parts\Embed\Embed;
2423
use hiro\database\Database;
2524
use Discord\Builders\MessageBuilder;
25+
use Discord\Helpers\Collection;
26+
use Discord\Parts\Interactions\Command\Option;
2627

2728
/**
2829
* Coinflip
@@ -39,7 +40,14 @@ public function configure(): void
3940
$this->command = "coinflip";
4041
$this->description = "An economy game";
4142
$this->aliases = ["cf"];
42-
$this->category = "utility";
43+
$this->category = "economy";
44+
$this->options = [
45+
(new Option($this->discord))
46+
->setType(Option::INTEGER)
47+
->setName('amount')
48+
->setDescription('Amount of money to bet')
49+
->setRequired(true)
50+
];
4351
}
4452

4553
/**
@@ -68,29 +76,36 @@ public function handle($msg, $args): void
6876
$usermoney = 0;
6977
}
7078
}
71-
if (!$args[0] || !is_numeric($args[0])) {
79+
80+
if ($args instanceof Collection && $args->get('name', 'amount') !== null) {
81+
$amount = $args->get('name', 'amount')->value;
82+
} else if (is_array($args)) {
83+
$amount = $args[0] ?? null;
84+
}
85+
$amount ??= null;
86+
87+
if (!$amount || !is_numeric($amount)) {
7288
$msg->reply($language->getTranslator()->trans('commands.coinflip.no_amount'));
7389
} else {
74-
if ($args[0] <= 0) {
90+
if ($amount <= 0) {
7591
$msg->reply($language->getTranslator()->trans('commands.coinflip.too_less_amount'));
76-
} else if ($args[0] > $usermoney) {
92+
} else if ($amount > $usermoney) {
7793
$msg->reply($language->getTranslator()->trans('global.not_enough_money'));
7894
} else {
79-
$payamount = $args[0];
8095
$rand = random_int(0, 1);
8196

82-
// delete user money from payamount
83-
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney - $payamount);
84-
$usermoney -= $payamount;
97+
// delete user money from ammount
98+
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney - $amount);
99+
$usermoney -= $amount;
85100

86-
$msg->reply($language->getTranslator()->trans('commands.coinflip.coin_spinning') . " <a:hirocoinflip:1130395266105737256>")->then(function($botreply) use ($msg, $rand, $database, $usermoney, $payamount, $language){
87-
$this->discord->getLoop()->addTimer(2.0, function() use ($botreply, $msg, $rand, $database, $usermoney, $payamount, $language){
101+
$msg->reply($language->getTranslator()->trans('commands.coinflip.coin_spinning') . " <a:hirocoinflip:1130395266105737256>")->then(function ($botreply) use ($msg, $rand, $database, $usermoney, $amount, $language) {
102+
$this->discord->getLoop()->addTimer(2.0, function () use ($botreply, $msg, $rand, $database, $usermoney, $amount, $language) {
88103
setlocale(LC_MONETARY, 'en_US');
89104
if ($rand) {
90-
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney + $payamount * 2);
91-
$botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.win') . " +" . $payamount*2 . " <:hirocoin:1130392530677157898>"));
105+
$database->setUserMoney($database->getUserIdByDiscordId($msg->author->id), $usermoney + $amount * 2);
106+
if ($botreply) $botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.win') . " +" . $amount * 2 . " <:hirocoin:1130392530677157898>"));
92107
} else {
93-
$botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.lose') . " -" . $payamount . " <:hirocoin:1130392530677157898>"));
108+
if ($botreply) $botreply->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.coinflip.lose') . " -" . $amount . " <:hirocoin:1130392530677157898>"));
94109
}
95110
});
96111
});

src/commands/utility/Daily.php src/commands/economy/Daily.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function configure(): void
3838
$this->command = "daily";
3939
$this->description = "Daily moneys.";
4040
$this->aliases = [];
41-
$this->category = "utility";
41+
$this->category = "economy";
4242
}
4343

4444
/**

src/commands/utility/Money.php src/commands/economy/Money.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function configure(): void
3838
$this->command = "money";
3939
$this->description = "Displays your money.";
4040
$this->aliases = ["cash"];
41-
$this->category = "utility";
41+
$this->category = "economy";
4242
}
4343

4444
/**

src/commands/utility/Pay.php src/commands/economy/Pay.php

+31-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
namespace hiro\commands;
2222

23+
use Discord\Helpers\Collection;
2324
use Discord\Parts\Embed\Embed;
25+
use Discord\Parts\Interactions\Command\Option;
2426
use hiro\database\Database;
2527

2628
class Pay extends Command
@@ -35,7 +37,19 @@ public function configure(): void
3537
$this->command = "pay";
3638
$this->description = "Send your money to anybody.";
3739
$this->aliases = [];
38-
$this->category = "utility";
40+
$this->category = "economy";
41+
$this->options = [
42+
(new Option($this->discord))
43+
->setType(Option::USER)
44+
->setName('user')
45+
->setDescription('User to send money')
46+
->setRequired(true),
47+
(new Option($this->discord))
48+
->setType(Option::INTEGER)
49+
->setName('amount')
50+
->setDescription('Amount of money')
51+
->setRequired(true)
52+
];
3953
}
4054

4155
/**
@@ -53,8 +67,12 @@ public function handle($msg, $args): void
5367
$msg->reply($language->getTranslator()->trans('database.notconnect'));
5468
return;
5569
}
56-
$embed = new Embed($this->discord);
57-
$user = $msg->mentions->first();
70+
if ($args instanceof Collection && $args->get('name', 'user') !== null) {
71+
$user = $this->discord->users->get('id', $args->get('name', 'user')->value);
72+
} else {
73+
$user = $msg->mentions->first() ?? null;
74+
}
75+
$user ??= null;
5876
if (!$user) {
5977
$msg->reply($language->getTranslator()->trans('commands.pay.no_user'));
6078
return;
@@ -63,20 +81,26 @@ public function handle($msg, $args): void
6381
$msg->reply($language->getTranslator()->trans('commands.pay.selfsend'));
6482
return;
6583
}
66-
if (!isset($args[1]) && !is_numeric($args[1])) {
84+
if($args instanceof Collection && $args->get('name', 'amount') !== null) {
85+
$amount = $args->get('name', 'amount')->value;
86+
} else if (is_array($args)) {
87+
$amount = explode("$user ", implode(' ', $args))[1] ?? null;
88+
}
89+
$amount ??= null;
90+
if (!isset($amount) && !is_numeric($amount)) {
6791
$msg->reply($language->getTranslator()->trans('commands.pay.no_numeric_arg'));
6892
return;
6993
}
70-
if (!$database->pay($database->getUserIdByDiscordId($msg->author->id), $database->getUserIdByDiscordId($user->id), $args[1])) {
94+
if (!$database->pay($database->getUserIdByDiscordId($msg->author->id), $database->getUserIdByDiscordId($user->id), $amount)) {
7195
$msg->reply($language->getTranslator()->trans('commands.pay.fail_msg'));
7296
return;
7397
}
7498
setlocale(LC_MONETARY, 'en_US');
7599
$msg->reply(
76100
sprintf(
77101
$language->getTranslator()->trans('commands.pay.pay_msg'),
78-
$msg->user->username, number_format($args[1], 2, ',', '.'), "<:hirocoin:1130392530677157898>",
79-
$user->username, number_format($args[1], 2, ',', '.'), "<:hirocoin:1130392530677157898>"
102+
$msg->user->username, number_format($amount, 2, ',', '.'), "<:hirocoin:1130392530677157898>",
103+
$user->username, number_format($amount, 2, ',', '.'), "<:hirocoin:1130392530677157898>"
80104
)
81105
);
82106
return;

0 commit comments

Comments
 (0)