Skip to content

Commit

Permalink
feat: support subcommand autocomplete (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
erics118 authored Oct 21, 2023
1 parent af1af42 commit cad6668
Showing 1 changed file with 40 additions and 35 deletions.
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
}
}

};
};

0 comments on commit cad6668

Please sign in to comment.