Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't try-catch deleteMessage with messageId "message->replyToMessage->messageId" #239

Open
Amirhan-Taipovjan-Greatest-I opened this issue Oct 27, 2022 · 3 comments

Comments

@Amirhan-Taipovjan-Greatest-I
try {
    bot.getEvents().onCommand("delete", [&bot](Message::Ptr message) {
        if (StringTools::startsWith(message->text, "/start")) {
            return;
        }
        int numb = rand50();
        if (numb == 1) {
            try {
                bot.getApi().deleteMessage(message->chat->id, message->replyToMessage->messageId);
            }
            catch (exception& e) {
                printf("XATA BULMADY: %s\n", e.what());
            }
        }
        else bot.getApi().sendAnimation(message->chat->id, InputFile::fromFile("examples/cyrt.gif", "image/gif"), 0, 0, 0, "", "", message->messageId);
        });
}
catch (exception& e) {
    printf("XATA BULMADY: %s\n", e.what());
}

Even with try-catch block I keep getting this exception message like this:
image

@Amirhan-Taipovjan-Greatest-I Amirhan-Taipovjan-Greatest-I changed the title I can't try-catch deleteMessage using messageId "message->replyToMessage->messageId" I can't try-catch deleteMessage with messageId "message->replyToMessage->messageId" Oct 28, 2022
@llnulldisk
Copy link
Contributor

I don't understand the error message. I assume it's something about memory access? If you debug this code section, then the error appears at the line with the deleteMessage()?
I have tested the code on my computer and it works: I set numb = 1. Fist, the bot sends a message. Then I reply to this message with the text "/delete" and the message is deleted.

@Amirhan-Taipovjan-Greatest-I
Copy link
Author

I don't understand the error message. I assume it's something about memory access? If you debug this code section, then the error appears at the line with the deleteMessage()? I have tested the code on my computer and it works: I set numb = 1. Fist, the bot sends a message. Then I reply to this message with the text "/delete" and the message is deleted.

When someone tries to delete already requested to delete message this exception'll be thrown...
And yes, It's about memory access.

@llnulldisk
Copy link
Contributor

Yes, there is an error because replyToMessage (the already deleted message in this case) doesn't exist and points to zero. You would first have to check whether the message that is being replied to (replyToMessage) is valid at all and if so, then take the id of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants