DPP
DPP copied to clipboard
Invalid Form Body from co_thread_create
Git commit reference e97ae1903a3a7fc5e0a592b564390f501afd6e56 (v10.1.2)
Describe the bug
I'm getting an "Invalid Form Body" error from Discord when I try to create a thread with co_thread_create.
To Reproduce
- Run this command:
// config is a nlohmann::json object
bot.on_slashcommand([&config](const dpp::slashcommand_t &event) -> dpp::task<> {
if (event.command.get_command_name() == "create-ticket") {
dpp::async thinking = event.co_thinking(true);
// Create private thread for ticket
std::string title = std::get<std::string>(event.get_parameter("title")); // I have verified this is a valid title
dpp::snowflake channel_id = config["log_channel_ids"]["tickets"]; // I have verified this is a valid channel snowflake
dpp::auto_archive_duration_t auto_archive = dpp::arc_1_week; // I have also tried arc_1_day
dpp::channel_type thread_type = dpp::CHANNEL_PRIVATE_THREAD; // I have also tried a public thread
dpp::confirmation_callback_t confirmation = co_await event.owner->co_thread_create(title, channel_id, auto_archive, thread_type, false, 0);
if (confirmation.is_error()) {
co_await thinking;
event.edit_original_response(dpp::message(std::string("Failed to create ticket channel: ") + confirmation.get_error().message));
co_return;
}
dpp::thread thread = std::get<dpp::thread>(confirmation.value);
// Mention moderators to add them to ticket and notify them at the same time
event.owner->message_create(dpp::message(thread.id, event.command.get_issuing_user().get_mention()
+ ", your ticket has been created. Please explain your rationale and wait for a <@&"
+ static_cast<std::string>(config["role_ids"]["moderator"]) + "> to respond.").set_allowed_mentions(true, true));
// Notify user that ticket was successfully created
co_await thinking;
event.edit_original_response(dpp::message(std::string("Ticket created successfully at ") + thread.get_mention()));
}
}
- Receive response "Failed to create ticket channel: Invalid Form Body"
Expected behavior A private thread with the specified title is created (then the moderators and ticket creator are notified).
System Details:
- OS: Arch Linux
- Discord Client used for testing: desktop
but what is the rest of the error content? It'll actually tell you what you did wrong, if you output more than just the error summary message... "Invalid Form Body" is a generic error summary that nearly every discord error gives. They stash the error output in the detail array.
a couple of questions. what instantiates &config and is it globally accessible at all times?
Secondly im not sure this is right: dpp::async thinking = event.co_thinking(true);
dpp::async is templated, so its going to be dpp::async<somthing>, or just auto is probably nicer.
config is created from running json::parse on a file, it's declared in main scope.
I got the dpp::async straight from a docs example:
// Send a "<bot> is thinking..." message, to wait on later so we can edit
dpp::async thinking = event.co_thinking(false);
It seems to work fine in other commands.
Either way, it looks like none of these are the issue. When I read the full error, I get this: 50035: Invalid Form Body - auto_archive_duration: Value "4" is not a valid enum value. (ENUM_TYPE_COERCE).
So it looks like dpp::arc_1_week is being read as 4 rather than 10080.
https://github.com/brainboxdotcc/DPP/blob/74b3865ad831a3e632f27b06aaf2ec6fd42d169f/include/dpp/channel.h#L245-L249
The enum says it'll be converted to minutes, but in thread_create it's just passed directly:
https://github.com/brainboxdotcc/DPP/blob/74b3865ad831a3e632f27b06aaf2ec6fd42d169f/src/dpp/cluster/thread.cpp#L155-L165
However in another command thread_create_in_forum you're doing the conversion:
https://github.com/brainboxdotcc/DPP/blob/74b3865ad831a3e632f27b06aaf2ec6fd42d169f/src/dpp/cluster/thread.cpp#L118-L141
Even if I supply the actual value 10080 instead of the enum, now I get the error [json.exception.type_error.302] type must be string, but is number.
Here is the JSON being sent to Discord from the command: {"auto_archive_duration":10080,"invitable":false,"name":"test title","rate_limit_per_user":0,"type":12}
According to Discord docs, the only thing supposed to be a string is name, and it is clearly a string in the JSON.
Interestingly, when I run the command, the channel where the thread should be created shows unread but there's nothing there.
This JSON type error has magically stopped happening when I tested it again today, even though I have changed literally zero code and didn't even recompile it. I don't know what Discord's servers were smoking that week, but that error used to happen every time and now I can't reproduce it. Thanks for putting up with me!