reo7sp / tgbot-cpp

C++ library for Telegram bot API
http://reo7sp.github.io/tgbot-cpp
MIT License
1.01k stars 246 forks source link

deleteMessage() causes problem #101

Closed Spixmaster closed 5 years ago

Spixmaster commented 5 years ago
if (args.at(0) == "Dx" + std::to_string(constants::ENGAGEMENT_AMOUNT) || args.at(0) == "dx" + std::to_string(constants::ENGAGEMENT_AMOUNT))
{
     //###irrelavant code for now
}
else
{
    if (!tools::isAdmin(bot, message->from, message))
    {
        if (tools::isAdmin(bot, bot.getApi().getMe(), message))
        {
            std::cout << "Deleted message in group:\n" << message->from->username << ":\n" << 
                        message->text << '\n' << std::endl;
            bot.getApi().deleteMessage(message->chat->id, message->messageId);
        }
        else
        {
            TgBot::Message::Ptr msg = bot.getApi().sendMessage(message->chat->id, "\xe2\x9d\x8cThe 
                     bot does not have the permission to delete messages. Make him admin to solve the 
                     problem!");
             //to delete message after a while
             cleanChat::add(msg->messageId);
        }
}

This is the relevant code. The intention is that it is only allowed to write messages in a group which start with "Dx10".

Following problem occurs. I happens from time to time that the application crashes. The programm can be started again if the code above is commented out. I highly assume that the program crashes due to the function deleteMessage() which tries to delete a non-existent message. The only reason for this is that a user deletes a message before the program does it. Deleting non-existent messages lets the app crash.

My question: Is there a possibility to check whether the message with the corresponding messageId is still existent?

JellyBrick commented 5 years ago

Use try-catch.

Spixmaster commented 5 years ago

@JellyBrick Thanks! That helps me a lot. I was despairing.

Nevertheless, I find the definition at in Api.cpp at line 1142 but where is the exception that can occur thrown? What exception is even thrown?

I'd be so grateful if you could help me with this problem.

JellyBrick commented 5 years ago

@Spixmaster TgBot::TgException is thrown.