From a1267b75c68f7b6d44d8134022042ae88792f57d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 23 Jul 2023 16:47:19 +0200 Subject: [PATCH] Improve getReply --- examples/bot.php | 10 +++------- src/EventHandler/AbstractMessage.php | 5 +++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/bot.php b/examples/bot.php index 7743b7284..f6d88757f 100755 --- a/examples/bot.php +++ b/examples/bot.php @@ -267,16 +267,12 @@ class MyEventHandler extends SimpleEventHandler #[FilterCommand('dl')] public function downloadLink(Incoming&Message $message): void { - if (!$message->replyToMsgId) { + $reply = $message->getReply(Message::class); + if (!$reply?->media) { $message->reply("This command must reply to a media message!"); return; } - $message = $message->getReply(); - if (!$message instanceof Message || !$message->media) { - $message->reply("This command must reply to a media message!"); - return; - } - $message->reply("Download link: ".$message->media->getDownloadLink()); + $reply->reply("Download link: ".$reply->media->getDownloadLink()); } public static function getPluginPaths(): string|array|null diff --git a/src/EventHandler/AbstractMessage.php b/src/EventHandler/AbstractMessage.php index 4dd32697f..9e653eba7 100644 --- a/src/EventHandler/AbstractMessage.php +++ b/src/EventHandler/AbstractMessage.php @@ -131,13 +131,14 @@ abstract class AbstractMessage extends Update implements SimpleFilters } return $this->replyCache; } - $this->replyCache = $this->API->wrapMessage($this->API->methodCallAsyncRead( + $messages = $this->API->methodCallAsyncRead( API::isSupergroup($this->chatId) ? 'channels.getMessages' : 'messages.getMessages', [ 'channel' => $this->chatId, 'id' => [['_' => 'inputMessageReplyTo', 'id' => $this->id]] ] - )['messages'][0]); + )['messages']; + $this->replyCache = $messages ? $this->API->wrapMessage($messages[0]) : null; $this->replyCached = true; if (!$this->replyCache instanceof $class) { return null;