Added support for Synical's engine and AdminCache

This commit is contained in:
Zi Xing 2021-12-25 19:16:24 -05:00
parent 9c4623f033
commit 7dcdbe0939
4 changed files with 158 additions and 8 deletions

View File

@ -0,0 +1,114 @@
<?php
namespace AdminCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
use Synical\Exceptions\CannotUpdateChatMembersCacheException;
use Synical\Exceptions\DatabaseException;
use SynicalBot;
use TelegramClientManager\Abstracts\TelegramChatType;
use TelegramClientManager\Exceptions\InvalidSearchMethod;
use TelegramClientManager\Exceptions\TelegramClientNotFoundException;
use TelegramClientManager\Objects\DetectedClients;
class AdminCacheCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'AdminCache';
/**
* @var string
*/
protected $description = 'Update the admin cache, to take into account new admins/admin permissions.';
/**
* @var string
*/
protected $usage = '/admincache';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* Command execute method
*
* @return ServerResponse
* @throws TelegramException
* @throws \TelegramClientManager\Exceptions\DatabaseException
* @throws InvalidSearchMethod
* @throws TelegramClientNotFoundException
* @noinspection DuplicatedCode
*/
public function execute(): ServerResponse
{
DetectedClients::findClients(SynicalBot::getTelegramClientManager(), $this->getMessage(), $this->getCallbackQuery());
// Ignore forwarded commands
if($this->getMessage()->getForwardFrom() !== null || $this->getMessage()->getForwardFromChat())
{
return Request::emptyResponse();
}
// Prevent the use of this command in private chats
if($this->getMessage()->getChat()->isPrivateChat())
{
return Request::sendMessage([
'chat_id' => $this->getMessage()->getChat()->getId(),
'reply_to_message_id' => $this->getMessage()->getMessageId(),
'parse_mode' => 'html',
'text' => 'This command is made to be used in group chats, not in private messages!'
]);
}
// Ignore the request if it's not a group or supergroup (eg; channels or new features that wasn't added)
if(
$this->getMessage()->getChat()->getType() !== TelegramChatType::Group &&
$this->getMessage()->getChat()->getType() !== TelegramChatType::SuperGroup
)
{
return Request::emptyResponse();
}
try
{
$ChatMemberCache = SynicalBot::getSynicalEngine()->getChatMemberCacheManager()->getChatMemberCache($this->getMessage()->getChat(), true);
return Request::sendMessage([
'chat_id' => $this->getMessage()->getChat()->getId(),
'reply_to_message_id' => $this->getMessage()->getMessageId(),
'parse_mode' => 'html',
'text' => 'Success! updated ' . count($ChatMemberCache->AdministratorPermissions) . ' user(s)'
]);
}
catch (CannotUpdateChatMembersCacheException $e)
{
return Request::sendMessage([
'chat_id' => $this->getMessage()->getChat()->getId(),
'reply_to_message_id' => $this->getMessage()->getMessageId(),
'parse_mode' => 'html',
'text' => 'Failed to update chat member cache, try again later.'
]);
}
catch (DatabaseException $e)
{
$ReferenceID = SynicalBot::getLogHandler()->logException($e, 'Worker');
/** @noinspection PhpUnhandledExceptionInspection */
return Request::sendMessage([
'chat_id' => $this->getMessage()->getChat()->getId(),
'reply_to_message_id' => $this->getMessage()->getMessageId(),
'parse_mode' => 'html',
'text' =>
'Oops! Something went wrong! contact someone in @IntellivoidDiscussions\n\n' .
'Error Code: <code>' . $ReferenceID . '</code>\n' .
'Object: <code>AdminCache</code>'
]);
}
}
}

View File

@ -5,7 +5,8 @@
use acm2\acm2;
use acm2\Objects\Schema;
use BackgroundWorker\BackgroundWorker;
use TelegramClientManager\TelegramClientManager;
use Synical\Synical;
use TelegramClientManager\TelegramClientManager;
use VerboseAdventure\VerboseAdventure;
/**
@ -32,6 +33,11 @@
*/
public static $TelegramClientManager;
/**
* @var Synical
*/
public static $SynicalEngine;
/**
* @var BackgroundWorker
*/
@ -218,6 +224,7 @@
if(self::isSleeping() == false)
{
self::getTelegramClientManager()->disconnectDatabase();
self::getSynicalEngine()->disconnectDatabase();
self::setIsSleeping(true);
}
}
@ -226,8 +233,17 @@
if(self::isSleeping() == true)
{
self::getTelegramClientManager()->connectDatabase();
self::getSynicalEngine()->connectDatabase();
self::setIsSleeping(false);
}
}
}
/**
* @return Synical
*/
public static function getSynicalEngine(): Synical
{
return self::$SynicalEngine;
}
}

View File

@ -67,6 +67,12 @@
"version": "latest",
"source": "default@github/intellivoid/ProcLib",
"required": true
},
{
"package": "net.intellivoid.synical",
"version": "latest",
"source": "default@github/intellivoid/Synical",
"required": true
}
],
"configuration": {
@ -81,6 +87,14 @@
}
},
"components": [
{
"required": true,
"file": "SynicalBot.php"
},
{
"required": true,
"file": "AdminCommands/AdminCacheCommand.php"
},
{
"required": true,
"file": "commands/InfoCommand.php"
@ -88,16 +102,12 @@
{
"required": true,
"file": "commands/StartCommand.php"
},
{
"required": true,
"file": "SynicalBot.php"
}
],
"files": [
".htaccess",
"main",
"package.json",
"worker"
"worker",
".htaccess",
"main"
]
}

View File

@ -15,6 +15,7 @@
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Request;
use ppm\ppm;
use Synical\Synical;
use TelegramClientManager\TelegramClientManager;
use VerboseAdventure\Abstracts\EventType;
use VerboseAdventure\Classes\ErrorHandler;
@ -99,10 +100,12 @@
if(file_exists($current_directory . DIRECTORY_SEPARATOR . 'SynicalBot.php'))
{
$telegram->addCommandsPaths([$current_directory . DIRECTORY_SEPARATOR . 'commands']);
$telegram->addCommandsPaths([$current_directory . DIRECTORY_SEPARATOR . 'AdminCommands']);
}
elseif(file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'SynicalBot.php'))
{
$telegram->addCommandsPaths([__DIR__ . DIRECTORY_SEPARATOR . 'commands']);
$telegram->addCommandsPaths([__DIR__ . DIRECTORY_SEPARATOR . 'AdminCommands']);
}
else
{
@ -150,6 +153,13 @@
exit(255);
}
SynicalBot::$SynicalEngine = new Synical();
if(SynicalBot::$SynicalEngine->getDatabase()->connect_error)
{
SynicalBot::getLogHandler()->log(EventType::ERROR, 'Failed to initialize SynicalEngine, ' . SynicalBot::$SynicalEngine->getDatabase()->connect_error, 'Worker');
exit(255);
}
try
{
$BackgroundWorker = new BackgroundWorker();