| 1 | #include <dpp/dpp.h> |
| 2 | |
| 3 | int main() { |
| 4 | dpp::cluster bot{"token"}; |
| 5 | |
| 6 | bot.on_log(dpp::utility::cout_logger()); |
| 7 | |
| 8 | bot.on_slashcommand([](const dpp::slashcommand_t& event) -> dpp::task<void> { |
| 9 | if (event.command.get_command_name() == "avatar") { |
| 10 | // Make a nested coroutine to fetch the guild member requested, that returns it as an optional |
| 11 | constexpr auto resolve_member = [](const dpp::slashcommand_t& event) -> dpp::task<std::optional<dpp::guild_member>> { |
| 12 | const dpp::command_value &user_param = event.get_parameter("user"); |
| 13 | dpp::snowflake user_id; |
| 14 | if (std::holds_alternative<std::monostate>(user_param)) { |
| 15 | user_id = event.command.usr.id; // Parameter is empty so user is sender |
| 16 | } |
| 17 | else if (std::holds_alternative<dpp::snowflake>(user_param)) { |
| 18 | user_id = std::get<dpp::snowflake>(user_param); // Parameter has a user |
| 19 | } |
| 20 | |
| 21 | // If we have the guild member in the command's resolved data, return it |
| 22 | const auto &member_map = event.command.resolved.members; |
| 23 | if (auto member = member_map.find(user_id); member != member_map.end()) { |
| 24 | co_return member->second; |
| 25 | } |
| 26 | // Try looking in guild cache |
| 27 | dpp::guild *guild = dpp::find_guild(event.command.guild_id); |
| 28 | if (guild) { |
| 29 | // Look in guild's member cache |
| 30 | if (auto member = guild->members.find(user_id); member != guild->members.end()) { |
| 31 | co_return member->second; |
| 32 | } |
| 33 | } |
| 34 | // Finally if everything else failed, request API |
| 35 | dpp::confirmation_callback_t confirmation = co_await event.owner->co_guild_get_member(event.command.guild_id, user_id); |
| 36 | if (confirmation.is_error()) { |
| 37 | co_return std::nullopt; // Member not found, return empty |
| 38 | } else { |
| 39 | co_return confirmation.get<dpp::guild_member>(); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | // Send a "<bot> is thinking..." message, to wait on later so we can edit |
| 44 | dpp::async thinking = event.co_thinking(false); |
| 45 | // Call our coroutine defined above to retrieve the member requested |
| 46 | std::optional<dpp::guild_member> member = co_await resolve_member(event); |
| 47 | if (!member.has_value()) { |
| 48 | // Wait for the thinking response to arrive to make sure we can edit |
| 49 | co_await thinking; |
| 50 | event.edit_original_response(dpp::message{"User not found in this server!"}); |
| 51 | co_return; |
| 52 | } |
| 53 | |
| 54 | std::string avatar_url = member->get_avatar_url(512); |
| 55 | if (avatar_url.empty()) { // Member does not have a custom avatar for this server, get their user avatar |
| 56 | dpp::confirmation_callback_t confirmation = co_await event.owner->co_user_get_cached(member->user_id); |
| 57 | if (confirmation.is_error()) { |
| 58 | // Wait for the thinking response to arrive to make sure we can edit |
| 59 | co_await thinking; |
| 60 | event.edit_original_response(dpp::message{"User not found!"}); |
| 61 | co_return; |
| 62 | } |
| 63 | avatar_url = confirmation.get<dpp::user_identified>().get_avatar_url(512); |
| 64 | } |
| 65 | |
| 66 | // Wait for the thinking response to arrive to make sure we can edit |
| 67 | co_await thinking; |
| 68 | event.edit_original_response(dpp::message{avatar_url}); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | |
| 73 | bot.on_ready([&bot](const dpp::ready_t& event) { |
| 74 | if (dpp::run_once<struct register_bot_commands>()) { |
| 75 | dpp::slashcommand command("avatar", "Get your or another user's avatar image", bot.me.id); |
| 76 | command.add_option(dpp::command_option(dpp::co_user, "user", "User to fetch the avatar from")); |
| 77 | |
| 78 | bot.global_command_create(command); |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | bot.start(dpp::st_wait); |
| 83 | |
| 84 | return 0; |
| 85 | } |