From 1aa26c836a0e47f67e0b221847ab9d3eb077dc95 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Wed, 7 Aug 2024 17:05:07 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Help=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/Bot.php | 19 ++++++++++++++----- bot/Commands/Help.php | 35 +++++++++++++++++++++++++++++++++++ bot/Commands/Icommands.php | 7 +++---- bot/Commands/Lorem.php | 7 ++++++- bot/Commands/Youtubedl.php | 6 +++++- nextcloud.php | 8 +++----- 6 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 bot/Commands/Help.php diff --git a/bot/Bot.php b/bot/Bot.php index cdfa45f..b42b944 100644 --- a/bot/Bot.php +++ b/bot/Bot.php @@ -20,6 +20,7 @@ class Bot $this->config = $config; } + // TODO public function isValid(Request $request): bool { $this->request = $request; @@ -97,17 +98,25 @@ class Bot curl_close($ch); } - private function sanitizeStringForTalkMessage(string $text): string { - return str_replace(['@', 'http://', 'https://'], ['👤', '🔗', '🔗🔒'], $text); - } - public function reply(string $message): void { $this->sendChatMessage('', $message); } - public function getConfig() + public function getConfig(): array { return $this->config; } + + public function listCommands(): \Generator + { + $commands_folder = __DIR__.DIRECTORY_SEPARATOR.'Commands'; + foreach (scandir($commands_folder) as $command) { + if ($command === '.' || $command === '..' || $command === 'Icommands.php') { + continue; + } + $class_name = explode('.', $command)[0]; + yield sprintf('Shikiryu\Bot\Commands\%s', $class_name); + } + } } diff --git a/bot/Commands/Help.php b/bot/Commands/Help.php new file mode 100644 index 0000000..605472c --- /dev/null +++ b/bot/Commands/Help.php @@ -0,0 +1,35 @@ +listCommands() as $command) { + $message .= sprintf( + '%s : %s (%s)%s', + $command, + call_user_func([$command, 'getDescription']), + call_user_func([$command, 'getPattern']), + "\n" + ); + } + + $bot->replyPolitely($message); + } + + public static function getDescription(): string + { + return 'Affiche les commandes à lancer'; + } + + public static function getPattern(): string + { + return '^(help|aide|liste des commandes)$'; + } +} \ No newline at end of file diff --git a/bot/Commands/Icommands.php b/bot/Commands/Icommands.php index 02b1c74..a6cfae4 100644 --- a/bot/Commands/Icommands.php +++ b/bot/Commands/Icommands.php @@ -8,8 +8,7 @@ interface Icommands { public static function getMessage(Bot $bot, array $data); - /** - * @return string - */ - public static function getDescription(); + public static function getDescription(): string; + + public static function getPattern(): string; } diff --git a/bot/Commands/Lorem.php b/bot/Commands/Lorem.php index dfa57c5..e684be5 100644 --- a/bot/Commands/Lorem.php +++ b/bot/Commands/Lorem.php @@ -14,8 +14,13 @@ class Lorem implements Icommands /** * @return string */ - public static function getDescription() + public static function getDescription(): string { return 'Génère du lorem ipsum'; } + + public static function getPattern(): string + { + return 'lorem( ipsum)?'; + } } diff --git a/bot/Commands/Youtubedl.php b/bot/Commands/Youtubedl.php index ae973da..16163b2 100644 --- a/bot/Commands/Youtubedl.php +++ b/bot/Commands/Youtubedl.php @@ -49,6 +49,11 @@ class Youtubedl implements Icommands return 'Youtubedl wrapper'; } + public static function getPattern(): string + { + return '(convertis|télécharges?) (https?:\/\/[^\s]+) en (audio|video|vidéo)'; + } + private static function getYoutubeDl(Bot $bot): string { $youtubedl_bin = $bot->getConfig()['youtube-dl']['bin']; @@ -64,5 +69,4 @@ class Youtubedl implements Icommands { return $bot->getConfig()['youtube-dl']['download_folder'] ?? __DIR__.'/../../'; } - } diff --git a/nextcloud.php b/nextcloud.php index e8a7697..d026ccf 100644 --- a/nextcloud.php +++ b/nextcloud.php @@ -1,8 +1,6 @@ hears('(convertis|télécharges?) (https?:\/\/[^\s]+) en (audio|video|vidéo)', Youtubedl::class); - -$bot->hears('lorem( ipsum)?', Lorem::class); +foreach ($bot->listCommands() as $command) { + $bot->hears(call_user_func([$command, 'getPattern']), $command); +} $bot->hears('(hello|hi|bonjour|salut)', function (Bot $bot) { $bot->replyPolitely('Bonjour');