diff --git a/bot.php b/bot.php index 138e208..3875c19 100644 --- a/bot.php +++ b/bot.php @@ -1,4 +1,4 @@ - load(); +if (!isset($_ENV['TOKEN'])) { + $dotenv = Dotenv\Dotenv::createImmutable("./"); + $dotenv->load(); } if ( Version::TYPE == 'development' ) @@ -40,6 +40,7 @@ error_reporting(0); } +global $shard_id, $shard_count; $ArgumentParser = new ArgumentParser($argv); $shard_id = $ArgumentParser->getShardId(); $shard_count = $ArgumentParser->getShardCount(); @@ -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(); diff --git a/src/commands/Command.php b/src/commands/Command.php index 40bc5d6..d6d7443 100644 --- a/src/commands/Command.php +++ b/src/commands/Command.php @@ -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 @@ -58,6 +60,13 @@ class Command implements CommandInterface { */ public $aliases = []; + /** + * Options + * + * @var array + */ + public array $options = []; + /** * CommandLoader * @@ -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 { } diff --git a/src/commands/author/Botban.php b/src/commands/author/Botban.php index b43cac1..303e0ad 100644 --- a/src/commands/author/Botban.php +++ b/src/commands/author/Botban.php @@ -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 @@ -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) + ]; } /** @@ -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."); } } } diff --git a/src/commands/author/Exec.php b/src/commands/author/Exec.php index a88ce3f..62d1cae 100644 --- a/src/commands/author/Exec.php +++ b/src/commands/author/Exec.php @@ -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; @@ -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) + ]; } /** @@ -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(); diff --git a/src/commands/utility/Coinflip.php b/src/commands/economy/Coinflip.php similarity index 63% rename from src/commands/utility/Coinflip.php rename to src/commands/economy/Coinflip.php index d7ae9a4..a83bbad 100644 --- a/src/commands/utility/Coinflip.php +++ b/src/commands/economy/Coinflip.php @@ -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 @@ -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) + ]; } /** @@ -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') . " ")->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') . " ")->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>")); } }); }); diff --git a/src/commands/utility/Daily.php b/src/commands/economy/Daily.php similarity index 98% rename from src/commands/utility/Daily.php rename to src/commands/economy/Daily.php index d3d4b26..0d6eadb 100644 --- a/src/commands/utility/Daily.php +++ b/src/commands/economy/Daily.php @@ -38,7 +38,7 @@ public function configure(): void $this->command = "daily"; $this->description = "Daily moneys."; $this->aliases = []; - $this->category = "utility"; + $this->category = "economy"; } /** diff --git a/src/commands/utility/Money.php b/src/commands/economy/Money.php similarity index 98% rename from src/commands/utility/Money.php rename to src/commands/economy/Money.php index 92940e1..9c646be 100644 --- a/src/commands/utility/Money.php +++ b/src/commands/economy/Money.php @@ -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"; } /** diff --git a/src/commands/utility/Pay.php b/src/commands/economy/Pay.php similarity index 59% rename from src/commands/utility/Pay.php rename to src/commands/economy/Pay.php index 14fe63b..97721c7 100644 --- a/src/commands/utility/Pay.php +++ b/src/commands/economy/Pay.php @@ -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 @@ -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) + ]; } /** @@ -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; @@ -63,11 +81,17 @@ 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; } @@ -75,8 +99,8 @@ public function handle($msg, $args): void $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; diff --git a/src/commands/utility/Slots.php b/src/commands/economy/Slots.php similarity index 88% rename from src/commands/utility/Slots.php rename to src/commands/economy/Slots.php index 75eb2de..d97c80b 100644 --- a/src/commands/utility/Slots.php +++ b/src/commands/economy/Slots.php @@ -23,6 +23,8 @@ use hiro\database\Database; use Discord\Parts\Channel\Message; use Discord\Builders\MessageBuilder; +use Discord\Helpers\Collection; +use Discord\Parts\Interactions\Command\Option; /** * Slots @@ -39,7 +41,14 @@ public function configure(): void $this->command = "slots"; $this->description = "An economy game."; $this->aliases = ["slot"]; - $this->category = "utility"; + $this->category = "economy"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::INTEGER) + ->setName('amount') + ->setDescription('Amount of money to pay for slots.') + ->setRequired(true) + ]; } /** @@ -62,11 +71,16 @@ public function handle($msg, $args): void $msg->channel->sendMessage($language->getTranslator()->trans('database.notconnect')); return; } - if (!isset($args[0])) { + if($args instanceof Collection && $args->get('name', 'amount') !== null) { + $payamount = $args->get('name', 'amount')->value; + } else { + $payamount = $args[0] ?? null; + } + $payamount ??= null; + if (!isset($payamount)) { $msg->reply($language->getTranslator()->trans('commands.slots.no_amount')); return; } - $payamount = $args[0]; if (!is_numeric($payamount)) { $msg->reply($language->getTranslator()->trans('commands.slots.no_numeric_arg')); return; diff --git a/src/commands/music/Join.php b/src/commands/music/Join.php index effab8d..2bc0710 100644 --- a/src/commands/music/Join.php +++ b/src/commands/music/Join.php @@ -51,7 +51,8 @@ public function handle($msg, $args): void $channel = $msg->member->getVoiceChannel(); if (!$channel) { - $msg->channel->sendMessage($language->getTranslator()->trans('commands.join.no_channel')); + $msg->reply($language->getTranslator()->trans('commands.join.no_channel')); + return; } $this->discord->joinVoiceChannel($channel, false, false, null, true)->done(function (VoiceClient $vc) use ($channel) { @@ -61,11 +62,11 @@ public function handle($msg, $args): void $voiceSettings[$channel->guild_id] = $settings; - $vc->on('exit', function() use ($voiceSettings) { + $vc->on('exit', function() use ($channel, $voiceSettings) { unset($voiceSettings[$channel->guild_id]); }); }, function ($e) use ($msg, $language) { - $msg->channel->sendMessage(sprintf($language->getTranslator()->trans('commands.join.on_error'), $e->getMessage())); + $msg->reply(sprintf($language->getTranslator()->trans('commands.join.on_error'), $e->getMessage())); }); } } diff --git a/src/commands/music/Play.php b/src/commands/music/Play.php index a6a6894..84ef126 100644 --- a/src/commands/music/Play.php +++ b/src/commands/music/Play.php @@ -23,8 +23,10 @@ use hiro\security\MusicCommand; use React\ChildProcess\Process; use Discord\Builders\MessageBuilder; +use Discord\Helpers\Collection; use hiro\parts\voice\VoiceFile; use React\Http\Browser; +use Discord\Parts\Interactions\Command\Option; class Play extends MusicCommand { @@ -34,7 +36,7 @@ class Play extends MusicCommand * @var Browser */ public Browser $browser; - + public function configure(): void { $this->command = "play"; @@ -42,67 +44,69 @@ public function configure(): void $this->aliases = []; $this->category = "music"; $this->browser = new Browser(null, $this->discord->getLoop()); + $this->options = [ + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('url') + ->setDescription('Youtube video url.') + ->setRequired(true) + ]; } public function playMusic($text_channel, $settings, $language) { $voice_client = $settings->getVoiceClient(); $current_voice_file = $settings->getQueue()[0] ?? null; - - if (!$current_voice_file) - { + + if (!$current_voice_file) { $text_channel->sendMessage($language->getTranslator()->trans('commands.play.no_queue')); return; } $author_id = $settings->getQueue()[0]->getAuthorId(); - + @unlink($author_id . ".m4a"); @unlink($author_id . ".info.json"); - + $command = "./yt-dlp -f bestaudio[ext=m4a] --ignore-config --ignore-errors --write-info-json --output=./{$author_id}.m4a --audio-quality=0 \"{$settings->getQueue()[0]->getUrl()}\""; $process = new Process($command); $process->start(); $editmsg = $text_channel->sendMessage($language->getTranslator()->trans('commands.play.downloading')); - $process->on('exit', function($code, $term) use ($voice_client, $author_id, $editmsg, $settings, $text_channel, $language) { + $process->on('exit', function ($code, $term) use ($voice_client, $author_id, $editmsg, $settings, $text_channel, $language) { if (is_file($author_id . ".m4a")) { $play_file_promise = $voice_client->playFile($author_id . ".m4a"); } - - $editmsg->then(function($m) use ($text_channel, $author_id, $play_file_promise, $settings, $language) { - + + $editmsg->then(function ($m) use ($text_channel, $author_id, $play_file_promise, $settings, $language) { + if (!is_file($author_id . ".m4a")) { $m->edit(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.play.couldnt_download'))); return; } - + $jsondata = json_decode(file_get_contents($author_id . ".info.json")); - $m->edit(MessageBuilder::new()->setContent(sprintf($language->getTranslator()->trans('commands.play.playing'), $jsondata->title) . " :musical_note: :tada:"))->then(function() use ($m, $play_file_promise, $settings, $text_channel, $language){ - $play_file_promise->then(function() use ($m, $settings, $text_channel, $language) { - if(@$settings->getQueue()[0]) - { - if (!$settings->getLoopEnabled()) - { + $m->edit(MessageBuilder::new()->setContent(sprintf($language->getTranslator()->trans('commands.play.playing'), $jsondata->title) . " :musical_note: :tada:"))->then(function () use ($m, $play_file_promise, $settings, $text_channel, $language) { + $play_file_promise->then(function () use ($m, $settings, $text_channel, $language) { + if (@$settings->getQueue()[0]) { + if (!$settings->getLoopEnabled()) { $settings->nextSong(); } $this->playMusic($text_channel, $settings, $language); } else { $m->channel->sendMessage(MessageBuilder::new()->setContent($language->getTranslator()->trans('commands.play.no_queue'))); } - + $m->delete(); }); }); - }); - - $this->discord->getLoop()->addTimer(0.5, function() use ($author_id) { + + $this->discord->getLoop()->addTimer(0.5, function () use ($author_id) { @unlink($author_id . ".m4a"); @unlink($author_id . ".info.json"); }); - }); } @@ -111,7 +115,12 @@ public function handle($msg, $args): void global $language; global $voiceSettings; - $url = substr($msg->content, strlen($_ENV['PREFIX'] . "play ")); + $url = null; + if ($args instanceof Collection && $args->get('name', 'url') !== null) { + $url = $args->get('name', 'url')->value; + } else if (is_array($args)) { + $url = substr($msg->content, strlen($_ENV['PREFIX'] . "play ")); + } if (!$url) { $msg->reply($language->getTranslator()->trans('commands.play.no_url')); @@ -126,39 +135,35 @@ public function handle($msg, $args): void return; } - preg_match('/https?:\/\/(www\.)?youtube\.com\/watch\?v\=([A-Za-z0-9-_]+)/', $url, $matches); - preg_match('/https?:\/\/(www\.)?youtu\.be\/([A-Za-z0-9-_]+)/', $url, $matches2); - preg_match('/https?:\/\/(www\.)?youtube\.com\/shorts\/([A-Za-z0-9-_]+)/', $url, $matches3); - if(!@$matches[0] && !@$matches2[0] && !@$matches3[0]) - { - $msg->reply($language->getTranslator()->trans('commands.play.no_youtube_url')); - return; - } - $url = $matches[0] ?? $matches2[0] ?? $matches3[0]; - - if(sizeof($settings->getQueue()) >= 10) - { + preg_match('/https?:\/\/(www\.)?youtube\.com\/watch\?v\=([A-Za-z0-9-_]+)/', $url, $matches); + preg_match('/https?:\/\/(www\.)?youtu\.be\/([A-Za-z0-9-_]+)/', $url, $matches2); + preg_match('/https?:\/\/(www\.)?youtube\.com\/shorts\/([A-Za-z0-9-_]+)/', $url, $matches3); + if (!@$matches[0] && !@$matches2[0] && !@$matches3[0]) { + $msg->reply($language->getTranslator()->trans('commands.play.no_youtube_url')); + return; + } + $url = $matches[0] ?? $matches2[0] ?? $matches3[0]; + + if (sizeof($settings->getQueue()) >= 10) { $msg->reply($language->getTranslator()->trans('commands.play.queue_overflow')); return; } - + $settings->addToQueue($voice_file = new VoiceFile(null, $url, $msg->author->id)); - + $this->browser->get('https://noembed.com/embed?url=' . $url)->then(function (\Psr\Http\Message\ResponseInterface $response) use ($voice_file) { $data = json_decode((string) $response->getBody()); - if(isset($data->title)) - { + if (isset($data->title)) { $voice_file->setTitle($data->title); } }, function (\Exception $e) { }); - if( @$settings->getQueue()[1] ) - { + if (@$settings->getQueue()[1]) { $msg->reply($language->getTranslator()->trans('commands.play.added_to_queue')); return; } - + $this->playMusic($msg->channel, $settings, $language); } } diff --git a/src/commands/music/Queue.php b/src/commands/music/Queue.php index 83d6136..4355c20 100644 --- a/src/commands/music/Queue.php +++ b/src/commands/music/Queue.php @@ -62,6 +62,6 @@ public function handle($msg, $args): void $embed->addFieldValues($song->title, sprintf($language->getTranslator()->trans('commands.queue.field'), $song->author_id, $song->url)); } $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } \ No newline at end of file diff --git a/src/commands/reactions/Avatar.php b/src/commands/reactions/Avatar.php index e751497..e7a6dac 100644 --- a/src/commands/reactions/Avatar.php +++ b/src/commands/reactions/Avatar.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; class Avatar extends Command { @@ -35,6 +37,13 @@ public function configure(): void $this->description = "Shows your avatar."; $this->aliases = []; $this->category = "reactions"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to show avatar') + ->setRequired(false) + ]; } /** @@ -47,21 +56,25 @@ public function configure(): void public function handle($msg, $args): void { global $language; - $user = $msg->mentions->first(); - if($user) - { + if ($args instanceof Collection && $args->get('name', 'user') !== null) { + $user = $this->discord->users->get('id', $args->get('name', 'user')->value); + } else if (is_array($args)) { + $user = $msg->mentions->first(); + } + $user ??= null; + if ($user) { $avatar = $user->avatar; - }else { + } else { $avatar = $msg->author->avatar; } - if (strpos($avatar, 'a_') !== false){ - $avatar= str_replace('jpg', 'gif', $avatar); + if (strpos($avatar, 'a_') !== false) { + $avatar = str_replace('jpg', 'gif', $avatar); } $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setTitle($language->getTranslator()->trans('commands.avatar.title')); $embed->setImage($avatar); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/reactions/Howgay.php b/src/commands/reactions/Howgay.php index ec9fcce..087aba6 100644 --- a/src/commands/reactions/Howgay.php +++ b/src/commands/reactions/Howgay.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; /** * Howgay @@ -38,6 +40,13 @@ public function configure(): void $this->description = "How much u are gay?"; $this->aliases = ["gay"]; $this->category = "reactions"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to check') + ->setRequired(false) + ]; } /** @@ -50,13 +59,18 @@ public function configure(): void public function handle($msg, $args): void { global $language; - $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 if (is_array($args)) { + $user = $msg->mentions->first(); + } + $user ??= null; if (!$user) $user = $msg->author; $random = rand(0, 100); $embed = new Embed($this->discord); $embed->setColor("#EB00EA"); - $embed->setDescription(sprintf("%s :gay_pride_flag:", sprintf($language->getTranslator()->trans('commands.howgay.description'), $user, $random . "%"))); + $embed->setDescription(sprintf("%s :gay_pride_flag:", sprintf($language->getTranslator()->trans('commands.howgay.description'), $user->username, $random . "%"))); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/reactions/Hug.php b/src/commands/reactions/Hug.php index 6cf4417..f6f1d85 100644 --- a/src/commands/reactions/Hug.php +++ b/src/commands/reactions/Hug.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; /** * Hug @@ -38,6 +40,13 @@ public function configure(): void $this->description = "You can hug everybody."; $this->aliases = ["sarıl"]; $this->category = "reactions"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to hug') + ->setRequired(true) + ]; } /** @@ -60,20 +69,25 @@ public function handle($msg, $args): void ]; $random = $gifs[rand(0, sizeof($gifs) - 1)]; $self = $msg->author; - $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 if (is_array($args)) { + $user = $msg->mentions->first(); + } + $user ??= null; if (empty($user)) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.hug.no_user')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } else if ($user->id == $self->id) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.hug.selfhug')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } $embed = new Embed($this->discord); @@ -81,6 +95,6 @@ public function handle($msg, $args): void $embed->setDescription(sprintf($language->getTranslator()->trans('commands.hug.success'), $self, $user)); $embed->setImage($random); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/reactions/Kiss.php b/src/commands/reactions/Kiss.php index b6f4742..1452031 100644 --- a/src/commands/reactions/Kiss.php +++ b/src/commands/reactions/Kiss.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; /** * Kiss @@ -38,6 +40,13 @@ public function configure(): void $this->description = "You can kiss everybody."; $this->aliases = ["öp"]; $this->category = "reactions"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to kiss') + ->setRequired(true) + ]; } /** @@ -61,20 +70,25 @@ public function handle($msg, $args): void ]; $random = $gifs[rand(0, sizeof($gifs) - 1)]; $self = $msg->author; - $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 if (is_array($args)) { + $user = $msg->mentions->first(); + } + $user ??= null; if (empty($user)) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.kiss.no_user')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } else if ($user->id == $self->id) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.kiss.selfkiss')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } $embed = new Embed($this->discord); @@ -82,6 +96,6 @@ public function handle($msg, $args): void $embed->setDescription(sprintf($language->getTranslator()->trans('commands.kiss.success'), $self, $user)); $embed->setImage($random); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/reactions/Marry.php b/src/commands/reactions/Marry.php index e35a156..9f0d49b 100644 --- a/src/commands/reactions/Marry.php +++ b/src/commands/reactions/Marry.php @@ -21,7 +21,8 @@ namespace hiro\commands; use Discord\Parts\Embed\Embed; -use hiro\interfaces\HiroInterface; +use Discord\Parts\Interactions\Command\Option; +use Discord\Helpers\Collection; /** * Marry @@ -39,6 +40,13 @@ public function configure(): void $this->description = "You can marry with everybody."; $this->aliases = []; $this->category = "reactions"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to marry') + ->setRequired(true) + ]; } /** @@ -61,20 +69,25 @@ public function handle($msg, $args): void ]; $random = $gifs[rand(0, sizeof($gifs) - 1)]; $self = $msg->author; - $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 if (is_array($args)) { + $user = $msg->mentions->first(); + } + $user ??= null; if (empty($user)) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.marry.no_user')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } else if ($user->id == $self->id) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.marry.selfmarry')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } $embed = new Embed($this->discord); @@ -82,6 +95,6 @@ public function handle($msg, $args): void $embed->setDescription($language->getTranslator()->trans('commands.marry.success')); $embed->setImage($random); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/reactions/Nekos.php b/src/commands/reactions/Nekos.php index ff73c6f..9ba1768 100644 --- a/src/commands/reactions/Nekos.php +++ b/src/commands/reactions/Nekos.php @@ -20,9 +20,11 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; use Psr\Http\Message\ResponseInterface; use React\Http\Browser; +use Discord\Parts\Interactions\Command\Option; /** * Nekos @@ -48,6 +50,13 @@ public function configure(): void $this->aliases = ["neko"]; $this->category = "reactions"; $this->browser = new Browser(null, $this->discord->getLoop()); + $this->options = [ + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('category') + ->setDescription('Category to get image or gif') + ->setRequired(false) + ]; } /** @@ -61,7 +70,12 @@ public function handle($msg, $args): void { global $language; $type_array = []; - $type = $args[0] ?? "waifu"; + if($args instanceof Collection && $args->get('name', 'category') !== null) { + $type = $args->get('name', 'category')->value; + } else if (is_array($args)) { + $type = $args[0] ?? null; + } + $type ??= "waifu"; $this->browser->get("https://nekos.best/api/v2/endpoints")->then(function (ResponseInterface $response) use ($msg, $args, $type, $type_array, $language) { $result = json_decode((string)$response->getBody(), true); @@ -87,7 +101,7 @@ function (ResponseInterface $response) use ($msg) { $embed->setImage($api[0]->url); $embed->setAuthor($msg->author->username, $msg->author->avatar); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); }, function (\Exception $e) use ($msg, $language) { $msg->reply($language->getTranslator()->trans('commands.nekos.api_error')); diff --git a/src/commands/reactions/Slap.php b/src/commands/reactions/Slap.php index 321fc00..3d27158 100644 --- a/src/commands/reactions/Slap.php +++ b/src/commands/reactions/Slap.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; /** * Slap @@ -38,6 +40,13 @@ public function configure(): void $this->description = "You can slap everybody."; $this->aliases = ["tokat"]; $this->category = "reactions"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to slap') + ->setRequired(true) + ]; } /** @@ -60,20 +69,25 @@ public function handle($msg, $args): void ]; $random = $gifs[rand(0, sizeof($gifs) - 1)]; $self = $msg->author; - $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 if (is_array($args)) { + $user = $msg->mentions->first(); + } + $user ??= null; if (empty($user)) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.slap.no_user')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } else if ($user->id == $self->id) { $embed = new Embed($this->discord); $embed->setColor("#ff0000"); $embed->setDescription($language->getTranslator()->trans('commands.slap.selfslap')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } $embed = new Embed($this->discord); @@ -81,6 +95,6 @@ public function handle($msg, $args): void $embed->setDescription(sprintf($language->getTranslator()->trans('commands.slap.success'), $self, $user)); $embed->setImage($random); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/reactions/Waifu.php b/src/commands/reactions/Waifu.php index cec06ed..a89da2b 100644 --- a/src/commands/reactions/Waifu.php +++ b/src/commands/reactions/Waifu.php @@ -20,9 +20,11 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; use Psr\Http\Message\ResponseInterface; use React\Http\Browser; +use Discord\Parts\Interactions\Command\Option; /** * Waifu @@ -48,6 +50,13 @@ public function configure(): void $this->aliases = ["wfu"]; $this->category = "reactions"; $this->browser = new Browser(null, $this->discord->getLoop()); + $this->options = [ + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('category') + ->setDescription('Category of waifu') + ->setRequired(false) + ]; } /** @@ -93,18 +102,18 @@ public function handle($msg, $args): void "dance", "cringe" ]; - if (!isset($args[0])) - { - $type = "waifu"; - } - if(isset($args[0])) - { - if (!in_array($args[0], $type_array)) { - $msg->reply(sprintf($language->getTranslator()->trans('commands.waifu.not_available_category'), $args[0]) . " \n" . sprintf($language->getTranslator()->trans('commands.waifu.available_categories'), implode(", ", $type_array))); - return; - } - $type = $args[0]; + if($args instanceof Collection && $args->get('name', 'category') !== null) { + $type = $args->get('name', 'category')->value; + } else if (is_array($args)) { + $type = $args[0] ?? null; + } + + $type ??= "waifu"; + + if (!in_array($type, $type_array)) { + $msg->reply(sprintf($language->getTranslator()->trans('commands.waifu.not_available_category'), $type) . " \n" . sprintf($language->getTranslator()->trans('commands.waifu.available_categories'), implode(", ", $type_array))); + return; } $this->browser->get("https://api.waifu.pics/sfw/$type")->then( @@ -117,7 +126,7 @@ function (ResponseInterface $response) use ($msg, $language) { $embed->setDescription(sprintf($language->getTranslator()->trans('commands.waifu.success'), $msg->author->username)); $embed->setImage($api->url); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); }, function (\Exception $e) use ($msg, $language) { $msg->reply($language->getTranslator()->trans('commands.waifu.api_error')); diff --git a/src/commands/reactions/WaifuNSFW.php b/src/commands/reactions/WaifuNSFW.php index e1eafc0..cc9771c 100644 --- a/src/commands/reactions/WaifuNSFW.php +++ b/src/commands/reactions/WaifuNSFW.php @@ -20,9 +20,11 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; use Psr\Http\Message\ResponseInterface; use React\Http\Browser; +use Discord\Parts\Interactions\Command\Option; /** * WaifuNSFW @@ -48,6 +50,13 @@ public function configure(): void $this->aliases = ["wnsfw", "wn"]; $this->category = "reactions"; $this->browser = new Browser(null, $this->discord->getLoop()); + $this->options = [ + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('category') + ->setDescription('Category of the waifu') + ->setRequired(false) + ]; } /** @@ -64,25 +73,25 @@ public function handle($msg, $args): void $msg->reply($language->getTranslator()->trans('commands.waifunsfw.no_nsfw_channel')); return; } + $type_array = [ "waifu", "neko", "trap", "blowjob" ]; - - if (!isset($args[0])) - { - $type = "waifu"; + + if($args instanceof Collection && $args->get('name', 'category') !== null) { + $type = $args->get('name', 'category')->value; + } else if (is_array($args)) { + $type = $args[0] ?? null; } - if(isset($args[0])) - { - if (!in_array($args[0], $type_array)) { - $msg->reply(sprintf($language->getTranslator()->trans('commands.waifunsfw.not_available_category'), $args[0]) . " \n" . sprintf($language->getTranslator()->trans('commands.waifunsfw.available_categories'), implode(", ", $type_array))); - return; - } - $type = $args[0]; + $type ??= "waifu"; + + if (!in_array($type, $type_array)) { + $msg->reply(sprintf($language->getTranslator()->trans('commands.waifunsfw.not_available_category'), $type) . " \n" . sprintf($language->getTranslator()->trans('commands.waifunsfw.available_categories'), implode(", ", $type_array))); + return; } $this->browser->get("https://api.waifu.pics/nsfw/$type")->then( @@ -95,7 +104,7 @@ function (ResponseInterface $response) use ($msg, $language) { $embed->setDescription(sprintf($language->getTranslator()->trans('commands.waifunsfw.success'), $msg->author->username)); $embed->setImage($api->url); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); }, function (\Exception $e) use ($msg, $language) { $msg->reply($language->getTranslator()->trans('commands.waifunsfw.api_error')); diff --git a/src/commands/rpg/CreateChar.php b/src/commands/rpg/CreateChar.php deleted file mode 100644 index 934d2fe..0000000 --- a/src/commands/rpg/CreateChar.php +++ /dev/null @@ -1,150 +0,0 @@ -command = "createchar"; - $this->description = "Creates your character."; - $this->aliases = ["createcharacter"]; - $this->category = "rpg"; - } - - /** - * handle - * - * @param [type] $msg - * @param [type] $args - * @return void - */ - public function handle($msg, $args): void - { - global $language; - $database = new Database(); - if (!$database->isConnected) { - $msg->channel->sendMessage($language->getTranslator()->trans('database.notconnect')); - return; - } - - $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($language->getTranslator()->trans('commands.createchar.already_created')); - return; - } - - if (!isset($args[2])) { - $prefix = @$_ENV['PREFIX']; - $races_string = ""; - foreach($races = RPG::getRacesAsArray(true) as $race) - { - if (end($races) === $race) - { - $races_string .= $race; - break; - } - $races_string .= $race . ", "; - } - $msg->reply(sprintf($language->getTranslator()->trans('commands.createchar.description'), $prefix, $races_string, $prefix)); - return; - } - - $char = 0; - if (strtolower($args[0]) == "warrior") { - $char = RPG::WARRIOR_CHAR; - } elseif (strtolower($args[0]) == "ranger") { - $char = RPG::RANGER_CHAR; - } elseif (strtolower($args[0]) == "mage") { - $char = RPG::MAGE_CHAR; - } elseif (strtolower($args[0]) == "healer") { - $char = RPG::HEALER_CHAR; - } - - if (!$char) { - $msg->reply($language->getTranslator()->trans('commands.createchar.unknown_type')); - return; - } - - $races = RPG::getRacesAsArray(); - $race = 0; - if (isset($races[strtolower($args[1])])) { - $race = $races[strtolower($args[1])]; - } - - if (!$char) { - $msg->reply($language->getTranslator()->trans('commands.createchar.unknown_race')); - return; - } - - $gender = 0; - if (strtolower($args[2]) == "male") { - $gender = RPG::MALE_GENDER; - } elseif (strtolower($args[2]) == "female") { - $gender = RPG::FEMALE_GENDER; - } - - if (!$gender) { - $msg->reply($language->getTranslator()->trans('commands.createchar.unknown_gender')); - return; - } - - // database progress - - $msg_str = ""; - if ($t_state = $database->setRPGCharType($database->getUserIdByDiscordId($msg->author->id), $char)) { - $msg_str .= sprintf($language->getTranslator()->trans('commands.createchar.set_type'), $args[0]) . "\n"; - } else { - $msg_str .= $language->getTranslator()->trans('global.unknown_error'); - goto send_reply; - } - - if ($r_state = $database->setRPGCharRace($database->getUserIdByDiscordId($msg->author->id), $race)) { - $msg_str .= sprintf($language->getTranslator()->trans('commands.createchar.set_race'), $args[1]) . "\n"; - } else { - $msg_str .= $language->getTranslator()->trans('global.unknown_error'); - goto send_reply; - } - - if ($g_state = $database->setRPGCharGender($database->getUserIdByDiscordId($msg->author->id), $gender)) { - $msg_str .= sprintf($language->getTranslator()->trans('commands.createchar.set_gender'), $args[2]) . "\n"; - } else { - $msg_str .= $language->getTranslator()->trans('global.unknown_error'); - goto send_reply; - } - - send_reply: - $msg->reply($msg_str . - ($t_state && $r_state && $g_state) ? $language->getTranslator()->trans('commands.createchar.on_success') : $language->getTranslator()->trans('commands.createchar.on_failure') - ); - } -} diff --git a/src/commands/rpg/Hunt.php b/src/commands/rpg/Hunt.php deleted file mode 100644 index 5fe55a2..0000000 --- a/src/commands/rpg/Hunt.php +++ /dev/null @@ -1,237 +0,0 @@ -command = "hunt"; - $this->description = "Hunting."; - $this->aliases = ["hunting"]; - $this->category = "rpg"; - } - - /** - * handle - * - * @param [type] $msg - * @param [type] $args - * @return void - */ - public function handle($msg, $args): void - { - global $language; - $database = new Database(); - if (!$database->isConnected) { - $msg->channel->sendMessage($language->getTranslator()->trans('database.notconnect')); - return; - } - - $startMessage = $this->getStartMessage($msg->author, $language); - $custom_id = $startMessage[1]; - $btn = $startMessage[2]; - - $msg->channel->sendMessage($startMessage[0])->then(function ($msg) use ($custom_id, $btn) { - $this->discord->getLoop()->addTimer(5.0, function () use ($msg, $custom_id, $btn) { - foreach($msg->components as $collection) - { - foreach($collection->components as $component) - { - if($component['custom_id'] == $custom_id) - { - $btn->setListener(null, $this->discord); // fill null listener if user didnt contact with button - } - } - } - }); - }); - } - - /** - * attackHandle - * - * @var ?Interaction $interaction - * @var GeneratorReturn $monster - * @var bool $attack - * @var Language $language - */ - public function attackHandle(Interaction $interaction = null, User $user, GeneratorReturn $monster, bool $attack, Language $language): ?MessageBuilder - { - $embed = new Embed($this->discord); - $embed - ->setTitle($monster->getName()) - ->setDescription(<<getTranslator()->trans('commands.hunt.monster_hp')}: {$monster->getHealth()} -EOF) - ->setImage(GithubImageGenerator::generate($monster->getName())) - ->setTimestamp(); - - $database = new \hiro\database\Database(); - - $uId = $database->getUserIdByDiscordId( - $user->id - ); - $uLvl = $database->getUserLevel( - $uId - ); - $uExp = $database->getUserExperience( - $uId - ); - - // attack event - if($interaction && $attack) - { - $monster->setHealth( - $monster->getHealth() - AttackSystem::getAttackDamage($uLvl) - ); - - if ($monster->getHealth() <= 0) { - $exp = $monster->getXp(); - - $database->setUserExperience( - $uId, - $uExp + $exp - ); - - if ($uExp + $exp >= LevelSystem::getRequiredExperiences($uLvl)) { - $database->setUserExperience($uId, abs($uExp - LevelSystem::getRequiredExperiences($uLvl))); - $database->setUserLevel($uId, $uLvl + 1); - - $interaction->channel->sendMessage($language->getTranslator()->trans('commands.hunt.level_up')); - } - - $interaction->updateMessage( - MessageBuilder::new() - ->setContent(sprintf($language->getTranslator()->trans('commands.hunt.gain_exp'), $exp)) - ->setEmbeds([]) - ->setComponents([]) - ); - - $this->discord->getLoop()->addTimer(2.0, function () use ($interaction, $language) { - $startMessage = $this->getStartMessage($interaction->user, $language); - $custom_id = $startMessage[1]; - $btn = $startMessage[2]; - - $interaction->channel->sendMessage($startMessage[0])->then(function ($msg) use ($custom_id, $btn) { - $this->discord->getLoop()->addTimer(5.0, function () use ($msg, $custom_id, $btn) { - foreach($msg->components as $collection) - { - foreach($collection->components as $component) - { - if($component['custom_id'] == $custom_id) - { - $btn->setListener(null, $this->discord); // fill null listener if user didnt contact with button - } - } - } - }); - }); - }); - - return null; - } - } - - $buildedMsg = MessageBuilder::new() - ->addComponent( - ActionRow::new()->addComponent( - Button::new(Button::STYLE_DANGER)->setLabel($language->getTranslator()->trans('commands.hunt.attack_button')) - ->setCustomId(sprintf("for-%s", $user->id)) - ->setListener( - function (Interaction $i) use ($user, $monster, $language) { - if (!str_starts_with($i->data->custom_id, "for-{$i->user->id}")) { - return; - } - $this->attackHandle($i, $user, $monster, true, $language); - }, - $this->discord, - true - ) - ) - ) - ->addEmbed($embed); - - if ($interaction) { - $interaction->updateMessage($buildedMsg); - } - - return $buildedMsg; - } - - /** - * getStartMessage - * - * @var User $user - * @var Language $language - * @return MessageBuilder - */ - public function getStartMessage(User $user, Language $language): array - { - $embed = new Embed($this->discord); - $embed->setTitle($language->getTranslator()->trans('commands.hunt.start_msg.title')); - $embed->setDescription($language->getTranslator()->trans('commands.hunt.start_msg.description')); - $embed->setTimestamp(); - $random_hex = bin2hex(random_bytes(6)); - $custom_id = "hunting-{$random_hex}-{$user->id}"; - - $buildedMsg = MessageBuilder::new() - ->addEmbed($embed) - ->addComponent( - ActionRow::new()->addComponent( - $btn = Button::new(Button::STYLE_DANGER) - ->setLabel($language->getTranslator()->trans('commands.hunt.start_button')) - ->setCustomId($custom_id) - ->setListener( - function (Interaction $interaction) use ($custom_id, $user, $language) { - if (!str_starts_with($interaction->data->custom_id, $custom_id)) { - return; - } - $generator = new MonsterGenerator(); - $monster = $generator->generateRandom(); - $interaction->message->edit($this->attackHandle(null, $user, $monster, true, $language)); - }, - $this->discord, - true - ) - ) - ); - - return [$buildedMsg, $custom_id, $btn]; - } -} diff --git a/src/commands/rpg/Inventory.php b/src/commands/rpg/Inventory.php deleted file mode 100644 index a922dc0..0000000 --- a/src/commands/rpg/Inventory.php +++ /dev/null @@ -1,108 +0,0 @@ -command = "inventory"; - $this->description = "Opens your inventory."; - $this->aliases = ["inv", "i"]; - $this->category = "rpg"; - } - - /** - * handle - * - * @param [type] $msg - * @param [type] $args - * @return void - */ - public function handle($msg, $args): void - { - global $language; - $database = new Database(); - if (!$database->isConnected) { - $msg->channel->sendMessage($language->getTranslator()->trans('database.notconnect')); - return; - } - - $user = $msg->member; - $user_id = $database->getUserIdByDiscordId($user->id); - $money = $database->getUserMoney($user_id); - $gender = $database->getRPGCharGenderAsText($user_id); - $race = $database->getRPGCharRaceAsText($user_id); - $type = $database->getRPGCharTypeAsText($user_id); - $items = $database->getRPGUserItems($user->id); - $character = $gender . "_" . $race . "_" . $type; - - $embed = new Embed($this->discord); - $embed->setTitle($user->username . " " . $language->getTranslator()->trans('commands.inventory.title')); - $embed->setAuthor($user->username, $msg->author->avatar); - $level = Padding::mb_str_pad($language->getTranslator()->trans('commands.inventory.level'), 10); - $exp = Padding::mb_str_pad($language->getTranslator()->trans('commands.inventory.experience'), 10); - $race = Padding::mb_str_pad($language->getTranslator()->trans('commands.inventory.race'), 10); - $gender = Padding::mb_str_pad($language->getTranslator()->trans('commands.inventory.gender'), 10); - $type = Padding::mb_str_pad($language->getTranslator()->trans('commands.inventory.type'), 10); - $embed->setDescription( - vsprintf( - <<", $database->getUserLevel($user_id), // level - "", $database->getUserExperience($user_id), // experience, - "<:race_Z:1107036549255790592>", $race, // race - "<:gender:1107036557271113728>", $gender, // gender - "<:skill:1107037343610835006>", $type, // type - ] - ) - ); - $embed->setImage("https://raw.githubusercontent.com/bariscodefxy/Hiro-Discord-Bot/master/src/images/rpg/characters/". $character .".png"); - $embed->setTimestamp(); - - $msg->reply( - MessageBuilder::new() - ->addEmbed($embed) - ); - } -} diff --git a/src/commands/rpg/ReleaseItem.php.wip b/src/commands/rpg/ReleaseItem.php.wip deleted file mode 100644 index b8dcb3b..0000000 --- a/src/commands/rpg/ReleaseItem.php.wip +++ /dev/null @@ -1,104 +0,0 @@ -command = "releaseitem"; - $this->description = "Releases an item from your uses."; - $this->aliases = ["unuse", "unuseitem"]; - $this->category = "rpg"; - } - - /** - * handle - * - * @param [type] $msg - * @param [type] $args - * @return void - */ - public function handle($msg, $args): void - { - $database = new Database(); - if (!$database->isConnected) { - $msg->channel->sendMessage("Couldn't connect to database."); - return; - } - - $slot = $args[0] ?? null; - switch(strtolower($slot)) { - case "boots": - $slot = RPG::ITEM_ARMOR_BOOTS; - break; - case "gloves": - $slot = RPG::ITEM_ARMOR_GLOVES; - break; - case "helmet": - $slot = RPG::ITEM_ARMOR_HELMET; - break; - case "pants": - $slot = RPG::ITEM_ARMOR_PANTS; - break; - case "pauldron": - $slot = RPG::ITEM_ARMOR_PAULDRON; - break; - case "weapon": - $slot = RPG::ITEM_WEAPON; - break; - default: - $msg->reply('Available argument(s): boots, gloves, helmet, pants, pauldron'); - return; - break; - } - - $toslot = $database->findRPGEmptyInventorySlot($msg->author->id); - if ($toslot === false) { - $msg->reply('You inventory is full!'); - return; - } - - if( $database->getRPGUsingItemByType($msg->author->id, $slot | RPG::ITEM_WEAPON_ONEHANDED | RPG::ITEM_USED_LEFT) || $database->getRPGUsingItemByType($msg->author->id, $slot | RPG::ITEM_WEAPON_ONEHANDED | RPG::ITEM_USED_RIGHT) ) { - $slot |= RPG::ITEM_WEAPON_ONEHANDED; - } - - if( $database->getRPGUsingItemByType($msg->author->id, $slot | RPG::ITEM_USED_RIGHT) ) { - $slot |= RPG::ITEM_USED_RIGHT; - } elseif ($database->getRPGUsingItemByType($msg->author->id, $slot | RPG::ITEM_USED_LEFT)) { - $slot |= RPG::ITEM_USED_LEFT; - } - $msg->reply($slot); - - if (!$database->releaseRPGUserItem($msg->author->id, $slot, $toslot)) { - $msg->reply('Couldn\'t release item.'); - return; - } - } -} \ No newline at end of file diff --git a/src/commands/rpg/SetRPGChannel.php b/src/commands/rpg/SetRPGChannel.php deleted file mode 100644 index d72d11c..0000000 --- a/src/commands/rpg/SetRPGChannel.php +++ /dev/null @@ -1,78 +0,0 @@ -command = "setrpgchannel"; - $this->description = "Sets RPG channel for the server."; - $this->aliases = []; - $this->category = "rpg"; - } - - /** - * handle - * - * @param [type] $msg - * @param [type] $args - * @return void - */ - public function handle($msg, $args): void - { - global $language; - $database = new Database(); - if (!$database->isConnected) { - $msg->channel->sendMessage($language->getTranslator()->trans('database.notconnect')); - return; - } - - if (!$msg->member->getPermissions()['manage_channels']) { - $msg->reply($language->getTranslator()->trans('commands.setrpgchannel.no_perm')); - return; - } - - if (isset($args[0])) { - preg_match('@<#([0-9]+)>@', $args[0], $result); - } - $channel = $result[1] ?? $msg->channel->id; - - if (!isset($msg->guild->channels[$channel])) { - $msg->reply($language->getTranslator()->trans('commands.setrpgchannel.no_channel')); - return; - } - - if (!$database->setServerRPGChannel($database->getServerIdByDiscordId($msg->guild->id), $channel)) { - $msg->reply($language->getTranslator()->trans('global.unknown_error')); - return; - } - - $msg->reply(sprintf($language->getTranslator()->trans('commands.setrpgchannel.success'), $channel)); - } -} diff --git a/src/commands/rpg/SetRPGEnabled.php b/src/commands/rpg/SetRPGEnabled.php deleted file mode 100644 index d5d6997..0000000 --- a/src/commands/rpg/SetRPGEnabled.php +++ /dev/null @@ -1,76 +0,0 @@ -command = "setrpgenabled"; - $this->description = "Sets RPG enabled for the server."; - $this->aliases = []; - $this->category = "rpg"; - } - - /** - * handle - * - * @param [type] $msg - * @param [type] $args - * @return void - */ - public function handle($msg, $args): void - { - global $language; - $database = new Database(); - if (!$database->isConnected) { - $msg->channel->sendMessage($language->getTranslator()->trans('database.notconnect')); - return; - } - - if (!$msg->member->getPermissions()['manage_channels']) { - $msg->reply($language->getTranslator()->trans('commands.setrpgenabled.no_perm')); - return; - } - - $enabled = $args[0] ?? null; - - if ($enabled === null || ($enabled != 0 && $enabled != 1)) { - $msg->reply($language->getTranslator()->trans('commands.setrpgenabled.invalid_parameter')); - return; - } - - if (!$database->setServerRPGEnabled($database->getServerIdByDiscordId($msg->guild->id), $enabled)) { - $msg->reply($language->getTranslator()->trans('global.unknown_error')); - return; - } - - $msg_str = $enabled ? $language->getTranslator()->trans('commands.setrpgenabled.on_enable') : $language->getTranslator()->trans('commands.setrpgenabled.on_disable'); - $msg->reply($msg_str); - } -} diff --git a/src/commands/rpg/UseItem.php.wip b/src/commands/rpg/UseItem.php.wip deleted file mode 100644 index c743b79..0000000 --- a/src/commands/rpg/UseItem.php.wip +++ /dev/null @@ -1,72 +0,0 @@ -command = "useitem"; - $this->description = "Uses an item from your inventory."; - $this->aliases = ["u", "use"]; - $this->category = "rpg"; - } - - /** - * handle - * - * @param [type] $msg - * @param [type] $args - * @return void - */ - public function handle($msg, $args): void - { - $database = new Database(); - if (!$database->isConnected) { - $msg->channel->sendMessage("Couldn't connect to database."); - return; - } - - $slot = $args[0] ?? null; - if (!$slot || !is_numeric($slot) || ($slot < 0 && $slot >= RPG::MAX_ITEM_SLOT)) { - $msg->reply('You should give a valid slot number!'); - return; - } - - $item = $database->getRPGUserItemBySlot($msg->author->id, $slot-1); - if (!$item) { - $msg->reply('Item not found.'); - return; - } - - if (!$database->useRPGUserItem($msg->author->id, $slot-1)) { - $msg->reply('Couldn\'t use item.'); - } - } -} diff --git a/src/commands/utility/Apod.php b/src/commands/utility/Apod.php index 15b32e8..123110d 100644 --- a/src/commands/utility/Apod.php +++ b/src/commands/utility/Apod.php @@ -82,7 +82,7 @@ public function handle($msg, $args): void $embed->addField($this->discord->makeField($language->getTranslator()->trans('commands.apod.service_version'), $result->service_version)); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); }, function (\Exception $e) use ($msg) { global $language; $msg->reply($language->getTranslator()->trans('commands.apod.api_error')); diff --git a/src/commands/utility/Ban.php b/src/commands/utility/Ban.php index e4403f3..84c4196 100644 --- a/src/commands/utility/Ban.php +++ b/src/commands/utility/Ban.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; class Ban extends Command { @@ -36,8 +38,15 @@ public function configure(): void $this->description = "Bans mentioned user."; $this->aliases = []; $this->category = "utility"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to ban') + ->setRequired(true) + ]; } - + /** * handle * @@ -48,77 +57,75 @@ public function configure(): void public function handle($msg, $args): void { global $language; - if(@$msg->member->getPermissions()['ban_members']) - { - $user = @$msg->mentions->first(); - if($user) - { + if (@$msg->member->getPermissions()['ban_members']) { + if ($args instanceof Collection && $args->get('name', 'user') !== null) { + $user = $msg->guild->members->get($args->get('name', 'user')->value); + } else if (is_array($args)) { + $user = $msg->mentions->first() ?? null; + } + $user ??= null; + if ($user) { $banner = $msg->author; - if(!isset($msg->channel->guild->members[$user->id])) - { + if (!isset($msg->guild->members[$user->id])) { $embed = new Embed($this->discord); $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('global.user_not_found')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } - $roles_men = $this->rolePositionsMap($msg->channel->guild->members[$user->id]->roles); + $roles_men = $this->rolePositionsMap($msg->guild->members[$user->id]->roles); $roles_self = $this->rolePositionsMap($msg->member->roles); - if( $roles_men ) - { + if ($roles_men) { $roles_men = max($roles_men); } else { $roles_men = 0; } - if( $roles_self ) - { + if ($roles_self) { $roles_self = max($roles_self); } else { $roles_men = 0; } - if($banner->id == $user->id) - { + if ($banner->id == $user->id) { $embed = new Embed($this->discord); $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.ban.selfban')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; - }else { - if( ($roles_self < $roles_men) && !($msg->channel->guild->owner_id == $msg->member->id) ) - { + } else { + if (($roles_self < $roles_men) && !($msg->guild->owner_id == $msg->member->id)) { $embed = new Embed($this->discord); $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.ban.role_pos_low')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); - }else { - $msg->channel->guild->members[$user->id]->ban(null, null) - ->then(function() use ( $msg, $user, $banner, $language ) { + $msg->reply($embed); + } else { + $msg->guild->members[$user->id]->ban(null, null) + ->then(function () use ($msg, $user, $banner, $language) { $embed = new Embed($this->discord); $embed->setColor('#ff0000'); $embed->setDescription(sprintf($language->getTranslator()->trans('commands.ban.banned'), $user, $banner)); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); - }, function (\Throwable $reason) use ( $msg, $language ) { + $msg->reply($embed); + }, function (\Throwable $reason) use ($msg, $language) { $msg->reply($reason->getCode() === 50013 ? $language->getTranslator()->trans('commands.ban.no_bot_perm') : $language->getTranslator()->trans('global.unknown_error')); - }); + }); } } - }else { + } else { $embed = new Embed($this->discord); $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.ban.no_user')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } - }else { + } else { $embed = new Embed($this->discord); $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.ban.no_perm')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } @@ -134,8 +141,7 @@ protected function rolePositionsMap($rolesCollision) { $rolesArray = $rolesCollision->toArray(); $new = []; - foreach($rolesArray as $role) - { + foreach ($rolesArray as $role) { $new[] = $role->position; } return $new; diff --git a/src/commands/utility/Botinfo.php b/src/commands/utility/Botinfo.php index 8b3eddb..2d9d532 100644 --- a/src/commands/utility/Botinfo.php +++ b/src/commands/utility/Botinfo.php @@ -48,7 +48,7 @@ public function configure(): void * @param [type] $args * @return void */ - public function handle($msg, $args): void + public function handle(\hiro\parts\Respondable $msg, $args): void { global $language; $guilds = $this->discord->formatNumber(sizeof($this->discord->guilds)); @@ -61,10 +61,9 @@ public function handle($msg, $args): void $embed->addField($this->discord->makeField($language->getTranslator()->trans('commands.botinfo.members'), $members)); $embed->addField($this->discord->makeField($language->getTranslator()->trans('commands.botinfo.commands'), $this->loader->getCommandsCount())); $embed->addField($this->discord->makeField($language->getTranslator()->trans('commands.botinfo.version'), sprintf("%s %s", Version::VERSION, Version::TYPE))); - $embed->addField($this->discord->makeField($language->getTranslator()->trans('commands.botinfo.latency'), intval($msg->timestamp->floatDiffInRealSeconds() * 1000) . "ms")); $embed->setThumbnail($this->discord->avatar); $embed->setAuthor($msg->author->username, $msg->author->avatar); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/utility/Clear.php b/src/commands/utility/Clear.php index 5aacdab..877699d 100644 --- a/src/commands/utility/Clear.php +++ b/src/commands/utility/Clear.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; /** * Clear @@ -38,6 +40,13 @@ public function configure(): void $this->description = "Clears messages"; $this->aliases = ["purge"]; $this->category = "utility"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::INTEGER) + ->setName('amount') + ->setDescription('Amount of messages to clear') + ->setRequired(true) + ]; } /** @@ -56,17 +65,22 @@ public function handle($msg, $args): void $embed->setDescription($language->getTranslator()->trans('commands.clear.no_perm')); $embed->setColor("#ff000"); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } - $limit = $args[0]; + if($args instanceof Collection && $args->get('name', 'amount') !== null) { + $limit = $args->get('name', 'amount')->value; + } else if (is_array($args)) { + $limit = $args[0] ?? null; + } + $limit ??= null; if (!isset($limit)) { $embed = new Embed($this->discord); $embed->setTitle($language->getTranslator()->trans('commands.clear.error')); $embed->setDescription($language->getTranslator()->trans('commands.clear.no_amount')); $embed->setColor("#ff000"); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } else if (!is_numeric($limit)) { $embed = new Embed($this->discord); @@ -74,7 +88,7 @@ public function handle($msg, $args): void $embed->setDescription($language->getTranslator()->trans('commands.clear.no_numeric_arg')); $embed->setColor("#ff000"); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } else if ($limit < 1 || $limit > 100) { $embed = new Embed($this->discord); @@ -82,18 +96,20 @@ public function handle($msg, $args): void $embed->setDescription($language->getTranslator()->trans('commands.clear.limit')); $embed->setColor("#ff000"); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } - $msg->channel->limitDelete($limit)->then(function() use ($msg, $limit, $language) { + $msg->channel->limitDelete($limit)->then(function () use ($msg, $limit, $language) { $embed = new Embed($this->discord); $embed->setTitle("Clear Command"); $embed->setDescription(sprintf($language->getTranslator()->trans('commands.clear.deleted'), $limit)); $embed->setColor("#5558E0"); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed)->then(function ($msg) { + $msg->reply($embed)->then(function ($msg) { $this->discord->getLoop()->addTimer(3.0, function () use ($msg) { - $msg->delete(); + if($msg) { + $msg->delete(); + } }); }); }, function (\Throwable $reason) use ($msg, $language) { diff --git a/src/commands/utility/Git.php b/src/commands/utility/Git.php index bc59866..8705ec3 100644 --- a/src/commands/utility/Git.php +++ b/src/commands/utility/Git.php @@ -56,7 +56,7 @@ public function handle($msg, $args): void $embed->setURL("https://github.com/hiro-team/hiro-bot.git"); $embed->setDescription($language->getTranslator()->trans('commands.git.description')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/utility/Help.php b/src/commands/utility/Help.php index e3dc90c..c2fe38b 100644 --- a/src/commands/utility/Help.php +++ b/src/commands/utility/Help.php @@ -20,8 +20,10 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; use Discord\Parts\Embed\Field; +use Discord\Parts\Interactions\Command\Option; /** * Help @@ -40,6 +42,13 @@ public function configure(): void $this->description = "Displays commands."; $this->aliases = ["?"]; $this->category = "utility"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('command') + ->setDescription('Specific command for help') + ->setRequired(false) + ]; } /** @@ -52,8 +61,13 @@ public function configure(): void public function handle($msg, $args): void { global $language; - if (isset($args[0])) { - $command = $args[0]; + if($args instanceof Collection && $args->get('name', 'command') !== null) { + $command = $args->get('name', 'command')->value; + } else { + $command = $args[0] ?? null; + } + $command ??= null; + if ($command) { if ($cmd = $this->findCommand($command)) { $description = $cmd->description ?? $language->getTranslator()->trans('commands.help.specific_command.no_description'); $cooldown = $cmd->cooldown ? $cmd->cooldown / 1000 . $language->getTranslator()->trans('commands.help.specific_command.seconds') : $language->getTranslator()->trans('commands.help.specific_command.no_cooldown'); @@ -69,7 +83,7 @@ public function handle($msg, $args): void EOF ); $embed->setAuthor($msg->author->username, $msg->author->avatar); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } else { $msg->channel->sendMessage(sprintf($language->getTranslator()->trans('commands.help.specific_command.not_found'), $command)); } @@ -118,7 +132,7 @@ public function handle($msg, $args): void } $embed->setAuthor($msg->author->username, $msg->author->avatar); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } /** diff --git a/src/commands/utility/Kick.php b/src/commands/utility/Kick.php index d397828..adac9b9 100644 --- a/src/commands/utility/Kick.php +++ b/src/commands/utility/Kick.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; class Kick extends Command { @@ -36,6 +38,13 @@ public function configure(): void $this->description = "Kicks mentioned user."; $this->aliases = []; $this->category = "utility"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to kick') + ->setRequired(true) + ]; } /** @@ -50,7 +59,12 @@ public function handle($msg, $args): void global $language; if(@$msg->member->getPermissions()['kick_members']) { - $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 if(is_array($args)) { + $user = $msg->mentions->first() ?? null; + } + $user ??= null; if($user) { $kicker = $msg->author; @@ -60,7 +74,7 @@ public function handle($msg, $args): void $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('global.user_not_found')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; } $roles_men = $this->rolePositionsMap($msg->channel->guild->members[$user->id]->roles); @@ -83,7 +97,7 @@ public function handle($msg, $args): void $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.kick.selfkick')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); return; }else { if( ($roles_self < $roles_men) && !($msg->channel->guild->owner_id == $msg->member->id) ) @@ -92,7 +106,7 @@ public function handle($msg, $args): void $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.kick.role_pos_low')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } else { $msg->channel->guild->members->delete($msg->channel->guild->members[$user->id]) ->then(function() use ( $msg, $user, $kicker, $language ) { @@ -106,7 +120,7 @@ public function handle($msg, $args): void ) ); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); }, function (\Throwable $reason) use ( $msg, $language ) { $msg->reply($reason->getCode() === 50013 ? $language->getTranslator()->trans('commands.kick.no_bot_perm') : $language->getTranslator()->trans('global.unknown_error')); }); @@ -117,14 +131,14 @@ public function handle($msg, $args): void $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.kick.no_user')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } }else { $embed = new Embed($this->discord); $embed->setColor('#ff0000'); $embed->setDescription($language->getTranslator()->trans('commands.kick.no_perm')); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/utility/Language.php b/src/commands/utility/Language.php index dc15761..0b7030a 100644 --- a/src/commands/utility/Language.php +++ b/src/commands/utility/Language.php @@ -19,7 +19,10 @@ */ namespace hiro\commands; + +use Discord\Helpers\Collection; use hiro\database\Database; +use Discord\Parts\Interactions\Command\Option; class Language extends Command { @@ -35,6 +38,13 @@ public function configure(): void $this->description = "Select your language."; $this->aliases = ["language"]; $this->category = "utility"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('language') + ->setDescription('Language to select.') + ->setRequired(true) + ]; } /** @@ -60,7 +70,16 @@ public function handle($msg, $args): void "kr_KR" => "Korean", "ja_JP" => "Japanese", ]; - $selected = $args[0] ?? null; + + if($args instanceof Collection && $args->get('name', 'language') !== null) + { + $selected = $args->get('name', 'language')->value; + } else { + $selected = $args[0] ?? null; + } + + $selected ??= null; + if(!$selected) { $lang_msg = "Available languages; "; diff --git a/src/commands/utility/Nick.php b/src/commands/utility/Nick.php index b090e88..9c35fcd 100644 --- a/src/commands/utility/Nick.php +++ b/src/commands/utility/Nick.php @@ -20,7 +20,8 @@ namespace hiro\commands; -use Discord\Parts\Embed\Embed; +use Discord\Helpers\Collection; +use Discord\Parts\Interactions\Command\Option; /** * Nick @@ -35,9 +36,21 @@ class Nick extends Command public function configure(): void { $this->command = "nick"; - $this->description = "Change users nick."; + $this->description = "You can change nickname of everybody."; $this->aliases = ["nickname"]; $this->category = "utility"; + $this->options = [ + (new Option($this->discord)) + ->setType(Option::USER) + ->setName('user') + ->setDescription('User to change nickname') + ->setRequired(true), + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('nickname') + ->setDescription('New nickname') + ->setRequired(true) + ]; } /** @@ -51,9 +64,19 @@ public function handle($msg, $args): void { global $language; if ($msg->member->getPermissions()['manage_nicknames']) { - $user = $msg->mentions->first(); + if($args instanceof Collection && $args->get('name', 'user') !== null) { + $user = $args->get('name', 'user')->value; + } else { + $user = $msg->mentions->first() ?? null; + } + $user ??= null; if ($user) { - $newname = explode("$user ", implode(' ', $args))[1] ?? ""; // else set to default + if($args instanceof Collection && $args->get('name', 'nickname') !== null ) { + $newname = $args->get('name', 'nickname')->value; + } else if (is_array($args)) { + $newname = explode("$user ", implode(' ', $args))[1] ?? null; + } + $newname ??= ""; // else set to default $msg->channel->guild->members[$user->id]->setNickname($newname)->then(function() use ($msg, $language) { $msg->reply($language->getTranslator()->trans('commands.nick.changed')); }, function( \Throwable $reason ) use ($msg, $language) { diff --git a/src/commands/utility/Ping.php b/src/commands/utility/Ping.php index 6aab699..126ad5d 100644 --- a/src/commands/utility/Ping.php +++ b/src/commands/utility/Ping.php @@ -61,6 +61,6 @@ public function configure(): void public function handle($msg, $args): void { global $language; - $msg->channel->sendMessage(sprintf($language->getTranslator()->trans('commands.ping.reply'), $this->lastPing ?? "...")); + $msg->reply(sprintf($language->getTranslator()->trans('commands.ping.reply'), $this->lastPing ?? "...")); } } diff --git a/src/commands/utility/ServerAvatar.php b/src/commands/utility/ServerAvatar.php index 8da10ee..ac66695 100644 --- a/src/commands/utility/ServerAvatar.php +++ b/src/commands/utility/ServerAvatar.php @@ -59,6 +59,6 @@ public function handle($msg, $args): void $embed->setDescription($msg->channel->guild->getUpdatableAttributes()['name'] . " Avatar"); $embed->setImage($msg->channel->guild->getIconAttribute("png", 1024)); $embed->setTimestamp(); - $msg->channel->sendEmbed($embed); + $msg->reply($embed); } } diff --git a/src/commands/utility/Wikipedia.php b/src/commands/utility/Wikipedia.php index fa544f9..d944367 100644 --- a/src/commands/utility/Wikipedia.php +++ b/src/commands/utility/Wikipedia.php @@ -20,7 +20,9 @@ namespace hiro\commands; +use Discord\Helpers\Collection; use Discord\Parts\Embed\Embed; +use Discord\Parts\Interactions\Command\Option; use hiro\commands\Command; use hiro\Version; use Psr\Http\Message\ResponseInterface; @@ -51,6 +53,13 @@ public function configure(): void $this->aliases = ["wiki", "w"]; $this->category = "utility"; $this->browser = new Browser(null, $this->discord->getLoop()); + $this->options = [ + (new Option($this->discord)) + ->setType(Option::STRING) + ->setName('query') + ->setDescription('The search query for the Wikipedia page.') + ->setRequired(true) + ]; } /** @@ -63,15 +72,20 @@ public function configure(): void public function handle($msg, $args): void { global $language; - // Make sure the user provided a search query + + if($args instanceof Collection && $args->get('name', 'query') !== null) { + $query = $args->get('name', 'query')->value; + } else { + $query = implode(" ", $args); + } + + $query ??= null; + if (empty($args)) { $msg->channel->sendMessage($language->getTranslator->trans('commands.wikipedia.no_query')); return; } - // Get the search query - $query = implode(" ", $args); - $this->browser->get("https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srprop=snippet&srsearch=$query")->then( function( ResponseInterface $response ) use ( $msg, $language ) { $body = (string)$response->getBody(); @@ -94,7 +108,7 @@ function( ResponseInterface $response ) use ( $msg, $language ) { $embed->setTimestamp(); // Send the embed - $msg->channel->sendEmbed($embed); + $msg->reply($embed); }, function (\Exception $e) use ($msg, $language) { $msg->reply($language->getTranslator->trans('commands.wikipedia.api_error')); diff --git a/src/interfaces/CommandInterface.php b/src/interfaces/CommandInterface.php index fba89e3..9645d72 100644 --- a/src/interfaces/CommandInterface.php +++ b/src/interfaces/CommandInterface.php @@ -20,7 +20,7 @@ namespace hiro\interfaces; -use hiro\interfaces\HiroInterface; +use hiro\parts\Respondable; /** * CommandInterface @@ -33,15 +33,15 @@ interface CommandInterface * @return void */ public function configure(): void; - + /** * handle * - * @param [type] $msg - * @param [type] $args + * @param Respondable $msg + * @param array $args * @return void */ - public function handle($msg, $args): void; + public function handle(Respondable $msg, array $args): void; /** * __get diff --git a/src/parts/CommandLoader.php b/src/parts/CommandLoader.php index 6f30bac..535005a 100644 --- a/src/parts/CommandLoader.php +++ b/src/parts/CommandLoader.php @@ -20,10 +20,19 @@ namespace hiro\parts; +use Discord\Builders\MessageBuilder; use hiro\database\Database; use hiro\interfaces\HiroInterface; use hiro\interfaces\SecurityCommandInterface; +use hiro\parts\Respondable; use Wujunze\Colors; +use Discord\Discord; +use Discord\Helpers\Collection; +use Discord\Parts\Channel\Message; +use Discord\Parts\Interactions\Interaction; +use Discord\Builders\CommandBuilder; +use Discord\Parts\Interactions\Command\Option; +use Discord\Repository\Interaction\GlobalCommandRepository; /** * CommandLoader @@ -33,9 +42,9 @@ class CommandLoader /** * client * - * @var HiroInterface + * @var Discord */ - protected HiroInterface $client; + protected Discord $client; /** * CLI Colors @@ -83,6 +92,9 @@ public function loadAllCommands() $this->loadDir($this->dir); $this->print_color("All commands has been loaded.", "green"); + $this->print_color("Trying to register commands.", "green"); + $this->registerCommands(); + $this->print_color("Bot is ready for use!", "green"); } @@ -167,35 +179,40 @@ public function loadCommand($cmd) { $command = $cmd->command; - $closure = function ($msg, $args) use ($cmd) { + $closure = function (Respondable|Message $respondable, array|Collection $args) use ($cmd) { + if($respondable instanceof Message) + { + $respondable = new Respondable($respondable); + } + try { if ($cmd->category == "rpg") { $database = new Database(); if ($database->isConnected) { - $rpgenabled = $database->getRPGEnabledForServer($database->getServerIdByDiscordId($msg->guild->id)); - $rpgchannel = $database->getRPGChannelForServer($database->getServerIdByDiscordId($msg->guild->id)); + $rpgenabled = $database->getRPGEnabledForServer($database->getServerIdByDiscordId($respondable->guild->id)); + $rpgchannel = $database->getRPGChannelForServer($database->getServerIdByDiscordId($respondable->guild->id)); if ($cmd->command != "setrpgchannel" && $cmd->command != "setrpgenabled") { if (!$rpgenabled) { - $msg->reply('RPG commands is not enabled in this server.'); + $respondable->reply('RPG commands is not enabled in this server.'); return; - } elseif (!$rpgchannel) { - $msg->reply('RPG commands channel is not available for this server.'); + } else if (!$rpgchannel) { + $respondable->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. + } else if ($rpgchannel != $respondable->channel->id) { + $respondable->reply('You should use this command in <#' . $rpgchannel . '>'); 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)); + $charType = $database->getRPGCharType($database->getUserIdByDiscordId($respondable->user->id)); + $charNation = $database->getRPGCharRace($database->getUserIdByDiscordId($respondable->user->id)); + $charGender = $database->getRPGCharGender($database->getUserIdByDiscordId($respondable->user->id)); if (!$charType || !$charNation || !$charGender) { - $msg->reply('You must create your character first!'); + $respondable->reply('You must create your character first!'); return; } } @@ -205,25 +222,25 @@ public function loadCommand($cmd) $database = new Database(); - if (!$database->isUserBannedFromBot($msg->author->id)) { + if (!$database->isUserBannedFromBot($respondable->user->id)) { if( $cmd instanceof SecurityCommandInterface ) { - if( !$cmd->securityChecks(['msg' => $msg, 'client' => $this->client]) ) + if( !$cmd->securityChecks(['respondable' => $respondable, 'client' => $this->client]) ) { return; } } global $language; - $language = new Language($database->getUserLocale($database->getUserIdByDiscordId($msg->author->id)) ?? "en_EN"); + $language = new Language($database->getUserLocale($database->getUserIdByDiscordId($respondable->user->id)) ?? "en_EN"); - $cmd->handle($msg, $args); + $cmd->handle($respondable, $args); } } catch (\Throwable $e) { if (\hiro\Version::TYPE == 'development') { echo $e; } - $msg->reply("ERROR: `" . $e->getMessage() . "`"); + $respondable->reply("ERROR: `" . $e->getMessage() . "`"); } }; @@ -238,19 +255,54 @@ public function loadCommand($cmd) $closure, $options ); + + $this->client->listenCommand($command, function(Interaction $interaction) use ($closure) { + $respondable = new Respondable($interaction); + $closure($respondable, $interaction->data->options); + }); + } + + /** + * registerCommands + * + * @return void + */ + public function registerCommands(): void + { + $this->client->application->commands->freshen()->then(function(GlobalCommandRepository $commands): void { + $allCommands = []; + $allowedCategories = ["music", "reactions", "utility"]; - // $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 . " "))); - // }); + foreach($this->categories as $cat) { + foreach($cat as $command) { + $allCommands[] = $command; + } + } + foreach($allCommands as $cmd) { + if(in_array($cmd->category, $allowedCategories) && !$commands->get('name', $cmd->command)) { + $builder = CommandBuilder::new(); + + $builder->setName($cmd->command) + ->setDescription($cmd->description); + + $builder->options = $cmd->options; + + $this->client->application->commands->save( + $this->client->application->commands->create( + $builder->toArray() + ) + ); + $this->print_color("Command registered: {$cmd->command}", "green"); + } + } + foreach($commands as $command) { + $cmd = $this->getCmd($command->name); + if(!$cmd || ($cmd && !in_array($cmd->category, $allowedCategories))) { + $this->client->application->commands->delete($command->id)->done(); + $this->print_color("Command deleted: {$command->name}", "red"); + } + } + }); } /** diff --git a/src/parts/Respondable.php b/src/parts/Respondable.php new file mode 100644 index 0000000..6c753a7 --- /dev/null +++ b/src/parts/Respondable.php @@ -0,0 +1,138 @@ +respondable = $respondable; + $this->channel ??= $respondable->channel; + $this->member ??= $respondable->member; + $this->user ??= $respondable->user; + $this->author = $respondable->author ?? $respondable->user; + $this->guild ??= $respondable->guild; + $this->mentions = $respondable->mentions ?? new Collection(); + $this->content ??= $respondable->content; + + if (isset($this->author) && !isset($this->user)) $this->user = $this->author; + if (isset($this->user) && !isset($this->author)) $this->author = $this->user; + + if (isset($respondable->user)) $this->user_id ??= $respondable->user->id; + if (isset($respondable->guild)) $this->guild_id ??= $respondable->guild->id; + if (isset($respondable->channel)) $this->channel_id ??= $respondable->channel->id; + } + + /** + * Reply method to reply to the message or interaction. + * + * @param string|Embed $message + * @return ExtendedPromiseInterface|null + */ + public function reply(string|Embed $message): ?ExtendedPromiseInterface + { + if ($this->respondable instanceof Interaction) { + if ($message instanceof Embed) { + return $this->respondable->respondWithMessage(MessageBuilder::new()->addEmbed($message)); + } else { + return $this->respondable->respondWithMessage(MessageBuilder::new()->setContent($message)); + } + } elseif ($this->respondable instanceof Message) { + if($message instanceof Embed) { + return $this->respondable->channel->sendMessage(MessageBuilder::new()->addEmbed($message)); + } else { + return $this->respondable->channel->sendMessage($message); + } + } else { + throw new \InvalidArgumentException('Invalid respondable object.'); + } + } +} diff --git a/src/security/AuthorCommand.php b/src/security/AuthorCommand.php index c6f3789..45ee665 100644 --- a/src/security/AuthorCommand.php +++ b/src/security/AuthorCommand.php @@ -20,8 +20,11 @@ namespace hiro\security; +use Discord\Builders\MessageBuilder; use hiro\interfaces\SecurityCommandInterface; use hiro\commands\Command; +use Discord\Parts\Interactions\Interaction; +use Discord\Parts\Channel\Message; /** * AuthorCommand @@ -37,14 +40,14 @@ class AuthorCommand extends Command implements SecurityCommandInterface */ public function securityChecks(array $args): bool { - if(!isset($args['msg'])) + if(!isset($args['respondable'])) { return false; } - if(!($args['msg']->author->id == @$_ENV['AUTHOR'])) + if(!($args['respondable']->user->id == @$_ENV['AUTHOR'])) { - $args['msg']->reply('Only bot author can use this command.'); + $args['respondable']->reply('Only bot author can use this command.'); return false; } diff --git a/src/security/MusicCommand.php b/src/security/MusicCommand.php index 542f67b..77df7ef 100644 --- a/src/security/MusicCommand.php +++ b/src/security/MusicCommand.php @@ -22,6 +22,9 @@ use hiro\interfaces\SecurityCommandInterface; use hiro\commands\Command; +use Discord\Parts\Interactions\Interaction; +use Discord\Parts\Channel\Message; +use Discord\Builders\MessageBuilder; /** * MusicCommand @@ -38,29 +41,29 @@ class MusicCommand extends Command implements SecurityCommandInterface public function securityChecks(array $args): bool { global $voiceSettings; - if(!isset($args['msg'])) + if(!isset($args['respondable'])) { return false; } - if (!$args['msg']->member->getVoiceChannel()) { - $args['msg']->channel->sendMessage("You must be in a voice channel."); + if (!$args['respondable']->member->getVoiceChannel()) { + $args['respondable']->channel->sendMessage("You're must be in a voice channel."); return false; } - if (!$args['client']->getVoiceClient($args['msg']->guild_id)) { - $args['msg']->channel->sendMessage("You should use join command first."); + if (!$args['client']->getVoiceClient($args['respondable']->guild_id)) { + $args['respondable']->reply("You should use join command first."); return false; } - if ($args['client']->getVoiceClient($args['msg']->guild_id) && $args['msg']->member->getVoiceChannel()->id !== $args['client']->getVoiceClient($args['msg']->guild_id)->getChannel()->id) { - $args['msg']->channel->sendMessage("You must be in same channel with me."); + if ($args['client']->getVoiceClient($args['respondable']->guild_id) && $args['respondable']->member->getVoiceChannel()->id !== $args['client']->getVoiceClient($args['respondable']->guild_id)->getChannel()->id) { + $args['respondable']->reply("You must be in same channel with me."); return false; } - if (!isset($voiceSettings[$args['msg']->guild_id])) + if (!isset($voiceSettings[$args['respondable']->guild_id])) { - $args['msg']->reply("Voice options couldn't found."); + $args['respondable']->reply("Voice options couldn't found."); return false; }