brainboxdotcc / DPP

C++ Discord API Bot Library - D++ is Lightweight and scalable for small and huge bots!
https://dpp.dev/
Apache License 2.0
1.04k stars 158 forks source link

set_presence() giving error 4002: decode error #1113

Closed ThiaudioTT closed 4 months ago

ThiaudioTT commented 5 months ago

Git commit reference https://github.com/brainboxdotcc/DPP/tree/v10.0.29

Describe the bug if we do:

    bot.on_ready([&bot](auto event) {
        if (dpp::run_once<struct register_bot_commands>()) {
            bot.global_command_create(
                dpp::slashcommand("ping", "Ping pong!", bot.me.id)
            );
        }
        // bot.set_presence(dpp::presence(dpp::ps_online, dpp::at_game, "with your heart"));
        bot.set_presence(dpp::presence()); // this will cause the error
    });

The bot will start to going online and offline in an infinite loop because of this.

WARN: OOF! Error from underlying websocket: 4002: Decode error

To Reproduce

Call: bot.set_presence(dpp::presence());

Expected behavior

It should produce a runtime error to indicate failure. Options to address this issue could be removing this constructor or implementing a precondition in cluster.cpp.

System Details:

Additional context

void cluster::set_presence(const dpp::presence &p) {
    json pres = p.to_json();
    std::string presence = pres.dump();
    std::cout << "Setting presence: " << presence << std::endl; // Setting presence: {"d":{"afk":false,"since":null,"status":"offline"},"op":3}
    for (auto& s : shards) {
        if (s.second->is_connected()) {
            s.second->queue_message(s.second->jsonobj_to_string(pres));
        }
    }
}
Jaskowicz1 commented 5 months ago

I'm not quite sure I understand this, are you saying doing bot.set_presence(dpp::presence()); causes the infinite loop?

ThiaudioTT commented 5 months ago

I'm not quite sure I understand this, are you saying doing bot.set_presence(dpp::presence()); causes the infinite loop?

I think not. The loop happens because bot.set_presence(dpp::presence()); is called inside bot.on_ready(). bot.start tries to connect again when a fail occurs, resulting in the loop.

The main problem here is that calling bot.set_presence(dpp::presence()); returns an error. That's all.

ThiaudioTT commented 5 months ago

I used the same snippet in https://github.com/brainboxdotcc/DPP?tab=readme-ov-file#example

I didn't mention that.

Jaskowicz1 commented 5 months ago

I'm not quite sure I understand this, are you saying doing bot.set_presence(dpp::presence()); causes the infinite loop?

I think not. The loop happens because bot.set_presence(dpp::presence()); is called inside bot.on_ready(). bot.start tries to connect again when a fail occurs, resulting in the loop.

The main problem here is that calling bot.set_presence(dpp::presence()); returns an error. That's all.

Ah yeah I meant that, figured it was looping cuz the on_ready, just meant does the empty presence cause the issue lol

Yeah, this doesn't really seem like a bug as it's right (and some might argue it's completely fine as is), it just could be presented better and maybe not force a bot restart!

I'll work on a PR for that tomorrow :)

Jaskowicz1 commented 4 months ago

A PR is now open for this!

Jaskowicz1 commented 4 months ago

This is now fixed! Sending an empty presence will now pass An empty presence was passed to set_presence. to your console!