Made a better target resolver function and fixed ban function
This commit is contained in:
parent
dcd1cc4c1f
commit
93138908b2
|
@ -24,7 +24,7 @@
|
|||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Bans the targetted user from the chat';
|
||||
protected $description = 'Bans the targeted user from the chat';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
|
@ -93,19 +93,31 @@
|
|||
]);
|
||||
}
|
||||
|
||||
$TargetUser = $DetectedClients->findTarget(true);
|
||||
try
|
||||
{
|
||||
$TargetUser = SynicalBot\Utilities::findTarget($this->getMessage(), $DetectedClients);
|
||||
}
|
||||
catch(SynicalBot\Exceptions\CannotFindTargetUserException $e)
|
||||
{
|
||||
return Request::sendMessage([
|
||||
'chat_id' => $this->getMessage()->getChat()->getId(),
|
||||
'reply_to_message_id' => $this->getMessage()->getMessageId(),
|
||||
'parse_mode' => 'html',
|
||||
'text' => 'Cannot resolve user. If you reply to one of their messages, I\'ll be able to interact with them.'
|
||||
]);
|
||||
}
|
||||
|
||||
if($TargetUser == null)
|
||||
{
|
||||
return Request::sendMessage([
|
||||
'chat_id' => $this->getMessage()->getChat()->getId(),
|
||||
'reply_to_message_id' => $this->getMessage()->getMessageId(),
|
||||
'parse_mode' => 'html',
|
||||
'text' => 'This command requires a user to be specified, try tagging the user or replying to said user.'
|
||||
'text' => 'You need to specify a user to ban'
|
||||
]);
|
||||
}
|
||||
$TargetID = ($TargetUser->User == null ? $TargetUser->Chat->ID : $TargetUser->User->ID);
|
||||
|
||||
$TargetPermissions = $ChatMemberCache->getAdministratorUser($TargetID);
|
||||
$TargetPermissions = $ChatMemberCache->getAdministratorUser($TargetUser->User->ID);
|
||||
if($TargetPermissions !== null)
|
||||
{
|
||||
if($TargetPermissions->IsOwner)
|
||||
|
@ -131,7 +143,7 @@
|
|||
|
||||
$BanResults = Request::banChatMember([
|
||||
'chat_id' => $this->getMessage()->getChat()->getId(),
|
||||
'user_id' => $TargetID
|
||||
'user_id' => $TargetUser->User->ID
|
||||
]);
|
||||
|
||||
if($BanResults->isOk())
|
||||
|
@ -148,7 +160,7 @@
|
|||
'chat_id' => $this->getMessage()->getChat()->getId(),
|
||||
'reply_to_message_id' => $this->getMessage()->getMessageId(),
|
||||
'parse_mode' => 'html',
|
||||
'text' => $BanResults->getDescription()
|
||||
'text' => $BanResults->getDescription() . ' (' . $BanResults->getErrorCode() . ')'
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace SynicalBot\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class CannotFindTargetUserException extends Exception
|
||||
{
|
||||
/**
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param Throwable|null $previous
|
||||
*/
|
||||
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->code = $code;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
namespace SynicalBot;
|
||||
|
||||
use Exception;
|
||||
use Longman\TelegramBot\Entities\Message;
|
||||
use pop\pop;
|
||||
use SynicalBot;
|
||||
use TelegramClientManager\Abstracts\SearchMethods\TelegramClientSearchMethod;
|
||||
use TelegramClientManager\Exceptions\DatabaseException;
|
||||
use TelegramClientManager\Exceptions\InvalidSearchMethod;
|
||||
use TelegramClientManager\Exceptions\TelegramClientNotFoundException;
|
||||
use TelegramClientManager\Objects\DetectedClients;
|
||||
use TelegramClientManager\Objects\TelegramClient;
|
||||
use TelegramClientManager\Utilities\Hashing;
|
||||
|
||||
class Utilities
|
||||
{
|
||||
/**
|
||||
* Attempts to resolve the targeted user
|
||||
*
|
||||
* @param Message $message
|
||||
* @param DetectedClients $clients
|
||||
* @param bool $reply_only
|
||||
* @return TelegramClient|null
|
||||
* @throws DatabaseException
|
||||
* @throws InvalidSearchMethod
|
||||
* @throws SynicalBot\Exceptions\CannotFindTargetUserException
|
||||
*/
|
||||
public static function findTarget(Message $message, DetectedClients $clients, bool $reply_only=true): ?TelegramClient
|
||||
{
|
||||
if(
|
||||
strlen(trim($message->getText(true))) == 0 &&
|
||||
$message->getReplyToMessage() == null
|
||||
)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if($message->getText(true) !== null && strlen($message->getText(true)) > 0)
|
||||
{
|
||||
$Username = trim(str_ireplace('@', (string)null, $message->getText(true)));
|
||||
|
||||
if(strlen($Username) > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byUsername, $Username
|
||||
);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
unset($e);
|
||||
}
|
||||
}
|
||||
|
||||
if(stripos(strtoupper(trim($message->getText(true))), 'TEL-') !== false)
|
||||
{
|
||||
try
|
||||
{
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byPublicId, trim($message->getText(true))
|
||||
);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
unset($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($clients->ReplyToUserClient !== null)
|
||||
{
|
||||
if($clients->ReplyToSenderChatClient !== null)
|
||||
return $clients->ReplyToSenderChatClient;
|
||||
|
||||
return $clients->ReplyToUserClient;
|
||||
}
|
||||
|
||||
if($clients->MentionUserClients !== null)
|
||||
{
|
||||
if(count($clients->MentionUserClients) > 0)
|
||||
return $clients->MentionUserClients[array_keys($clients->MentionUserClients)[0]];
|
||||
}
|
||||
|
||||
if($reply_only == false)
|
||||
{
|
||||
if($clients->SenderChatClient !== null)
|
||||
return $clients->SenderChatClient;
|
||||
|
||||
if($clients->UserClient !== null)
|
||||
return $clients->UserClient;
|
||||
}
|
||||
|
||||
// Final attempt with pop
|
||||
if($message->getText(true) !== null && strlen($message->getText(true)) > 0)
|
||||
{
|
||||
// NOTE: Argument parsing is done with pop now.
|
||||
$options = pop::parse($message->getText(true));
|
||||
|
||||
$TargetTelegramParameter = array_values($options)[(count($options)-1)];
|
||||
if(is_bool($TargetTelegramParameter))
|
||||
{
|
||||
$TargetTelegramParameter = array_keys($options)[(count($options)-1)];
|
||||
}
|
||||
|
||||
$EstimatedPrivateID = Hashing::telegramClientPublicID((int)$TargetTelegramParameter, (int)$TargetTelegramParameter);
|
||||
|
||||
try
|
||||
{
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byPublicId, $EstimatedPrivateID
|
||||
);
|
||||
}
|
||||
catch(TelegramClientNotFoundException $e)
|
||||
{
|
||||
unset($e);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byPublicId, $TargetTelegramParameter
|
||||
);
|
||||
}
|
||||
catch(TelegramClientNotFoundException $e)
|
||||
{
|
||||
unset($e);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byUsername, str_ireplace('@', (string)null, $TargetTelegramParameter)
|
||||
);
|
||||
}
|
||||
catch(TelegramClientNotFoundException $e)
|
||||
{
|
||||
unset($e);
|
||||
}
|
||||
}
|
||||
|
||||
throw new SynicalBot\Exceptions\CannotFindTargetUserException('Cannot resolve user ' . $message->getText(true));
|
||||
}
|
||||
}
|
|
@ -99,6 +99,14 @@
|
|||
"required": true,
|
||||
"file": "AdminCommands/AdminCacheCommand.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "SynicalBot/Utilities.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "SynicalBot/Exceptions/CannotFindTargetUserException.php"
|
||||
},
|
||||
{
|
||||
"required": true,
|
||||
"file": "commands/InfoCommand.php"
|
||||
|
|
Loading…
Reference in New Issue