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

feat: support subcommand autocomplete #963

Merged
merged 2 commits into from
Oct 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 40 additions & 35 deletions src/dpp/events/interaction_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@

namespace dpp::events {

namespace {

void fill_options(dpp::json option_json, std::vector<dpp::command_option>& options) {
for (auto & o : option_json) {
dpp::command_option opt;
opt.name = string_not_null(&o, "name");
opt.type = (dpp::command_option_type)int8_not_null(&o, "type");
switch (opt.type) {
case co_boolean:
opt.value = o.at("value").get<bool>();
break;
case co_channel:
case co_role:
case co_user:
case co_attachment:
case co_mentionable:
opt.value = dpp::snowflake(snowflake_not_null(&o, "value"));
break;
case co_integer:
opt.value = o.at("value").get<int64_t>();
break;
case co_string:
opt.value = o.at("value").get<std::string>();
break;
case co_number:
opt.value = o.at("value").get<double>();
break;
case co_sub_command:
case co_sub_command_group:
fill_options(o["options"], opt.options);
break;
}
opt.focused = bool_not_null(&o, "focused");
options.emplace_back(opt);
}
}

}

/**
* @brief Handle event
Expand Down Expand Up @@ -96,40 +134,7 @@ void interaction_create::handle(discord_client* client, json &j, const std::stri
dpp::autocomplete_t ac(client, raw);
ac.id = snowflake_not_null(&(d["data"]), "id");
ac.name = string_not_null(&(d["data"]), "name");
for (auto & o : d["data"]["options"]) {
dpp::command_option opt;
opt.name = string_not_null(&o, "name");
opt.type = (dpp::command_option_type)int8_not_null(&o, "type");
if (o.contains("value") && !o.at("value").is_null()) {
switch (opt.type) {
case co_boolean:
opt.value = o.at("value").get<bool>();
break;
case co_channel:
case co_role:
case co_user:
case co_attachment:
case co_mentionable:
opt.value = dpp::snowflake(snowflake_not_null(&o, "value"));
break;
case co_integer:
opt.value = o.at("value").get<int64_t>();
break;
case co_string:
opt.value = o.at("value").get<std::string>();
break;
case co_number:
opt.value = o.at("value").get<double>();
break;
case co_sub_command:
case co_sub_command_group:
/* Silences warning on clang, handled elsewhere */
break;
}
}
opt.focused = bool_not_null(&o, "focused");
ac.options.emplace_back(opt);
}
fill_options(d["data"]["options"], ac.options);
ac.command = i;
client->creator->on_autocomplete.call(ac);
}
Expand Down Expand Up @@ -158,4 +163,4 @@ void interaction_create::handle(discord_client* client, json &j, const std::stri
}
}

};
};