Add Help command

This commit is contained in:
Clement Desmidt 2024-08-07 17:05:07 +02:00
parent 60f18f2f76
commit 1aa26c836a
6 changed files with 66 additions and 16 deletions

View File

@ -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);
}
}
}

35
bot/Commands/Help.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Shikiryu\Bot\Commands;
use Shikiryu\Bot\Bot;
class Help implements Icommands
{
public static function getMessage(Bot $bot, array $data): void
{
$message = 'Voici la liste de mes commandes : '."\n";
foreach ($bot->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)$';
}
}

View File

@ -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;
}

View File

@ -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)?';
}
}

View File

@ -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__.'/../../';
}
}

View File

@ -1,8 +1,6 @@
<?php
use Shikiryu\Bot\Bot;
use Shikiryu\Bot\Commands\Lorem;
use Shikiryu\Bot\Commands\Youtubedl;
require 'vendor/autoload.php';
@ -23,9 +21,9 @@ $event = $_SERVER['HTTP_X_H1_EVENT'] ?? '';
$signature = $_SERVER['HTTP_X_H1_SIGNATURE'] ?? '';
// Give the bot something to listen for.
$bot->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');