Add better ytdl

This commit is contained in:
2024-08-06 16:06:07 +02:00
parent a63538c768
commit 1dfcb4a4d5
8 changed files with 895 additions and 63 deletions

View File

@@ -4,61 +4,40 @@ namespace Shikiryu\Bot;
class Bot
{
/**
* @var Request
*/
protected $request;
protected $events = [];
protected $config = [];
/**
* @var string[]
*/
protected static $masters = [];
protected Request $request;
protected array $events = [];
protected array $config = [];
protected static array $masters = [];
/**
* @return string
*/
protected function getMaster()
protected function getMaster(): string
{
$masters = array_merge(self::$masters, $this->getConfig()['masters']);
return $masters[array_rand($masters, 1)];
return $masters[array_rand($masters)];
}
/**
* Bot constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
$this->config = $config;
}
/**
* @param Request $request
* @return bool
*/
public function isValid(Request $request)
public function isValid(Request $request): bool
{
$this->request = $request;
return $this->config['token'] === $request->getToken();
}
/**
* @param string $pattern the pattern to listen for
* @param string $pattern the pattern to listen for
* @param \Closure|string $callback the callback to execute. Either a closure or a Class@method notation
*
* @return $this
*/
public function hears($pattern, $callback)
public function hears(string $pattern, \Closure|string $callback): static
{
if (!array_key_exists($pattern, $this->events)) {
$this->events[$pattern] = $callback;
} else {
error_log(sprintf('Event %s déjà en place', $pattern));
}
// $this->events[$pattern][] = $callback;
return $this;
}
@@ -66,33 +45,30 @@ class Bot
* Try to match messages with the ones we should
* listen to.
*/
public function listen($sentence)
public function listen($sentence): void
{
foreach ($this->events as $pattern => $command) {
$preg_pattern = sprintf('#%s#i', $pattern);
if (preg_match($preg_pattern, $sentence, $matches)) {
if (is_callable($command)) {
call_user_func($command, $this);
$command($this);
} else {
call_user_func([$command, 'getMessage'], $this, $matches);
$command->getMessage($this, $matches);
}
return;
}
}
$this->replyPolitely('Je n\'ai pas compris');
}
/**
* @param string $message
*/
public function replyPolitely($message)
public function replyPolitely(string $message): void
{
$message .= ', '.$this->getMaster().'.';
$this->reply($message);
}
public function sendChatMessage(string $referenceId, string $message): void {
public function sendChatMessage(string $referenceId, string $message): void {
$body = [
'message' => $message,
'referenceId' => $referenceId,
@@ -109,7 +85,7 @@ class Bot
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'nextcloud-talk-hackerone-adapter/1.0');
curl_setopt($ch, CURLOPT_USERAGENT, 'alfred/1.0');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'OCS-APIRequest: true',
@@ -117,8 +93,7 @@ class Bot
'X-Nextcloud-Talk-Bot-Random: ' . $random,
'X-Nextcloud-Talk-Bot-Signature: ' . $hash,
]);
$var = curl_exec($ch);
$error = curl_error($ch);
curl_exec($ch);
curl_close($ch);
}
@@ -126,13 +101,8 @@ class Bot
return str_replace(['@', 'http://', 'https://'], ['👤', '🔗', '🔗🔒'], $text);
}
/**
* @param string $message
*/
public function reply($message)
public function reply(string $message): void
{
// header('Content-type: application/json');
// die('{"text": "'.$message.'"}');
$this->sendChatMessage('', $message);
}