| 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 | /* The event is fired when someone issues your commands */ |
| 9 | bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { |
| 10 | /* Check which command they ran */ |
| 11 | if (event.command.get_command_name() == "addrole") { |
| 12 | /* Fetch a parameter value from the command options */ |
| 13 | dpp::snowflake user_id = std::get<dpp::snowflake>(event.get_parameter("user")); |
| 14 | dpp::snowflake role_id = std::get<dpp::snowflake>(event.get_parameter("role")); |
| 15 | |
| 16 | /* Get member object from resolved list */ |
| 17 | dpp::guild_member resolved_member = event.command.get_resolved_member(user_id); |
| 18 | |
| 19 | resolved_member.add_role(role_id); |
| 20 | bot.guild_edit_member(resolved_member); |
| 21 | |
| 22 | event.reply("Added role"); |
| 23 | } |
| 24 | }); |
| 25 | |
| 26 | /* Attach on_ready event */ |
| 27 | bot.on_ready([&bot](const dpp::ready_t& event) { |
| 28 | if (dpp::run_once<struct register_bot_commands>()) { |
| 29 | dpp::slashcommand add_role("addrole", "Give user a role", bot.me.id); |
| 30 | |
| 31 | /* Add user and role type command options to the slash command */ |
| 32 | add_role.add_option(dpp::command_option(dpp::co_user, "user", "User to give role to", true)); |
| 33 | add_role.add_option(dpp::command_option(dpp::co_role, "role", "Role to give", true)); |
| 34 | |
| 35 | bot.global_command_create(add_role); |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | /* Start bot */ |
| 40 | bot.start(dpp::st_wait); |
| 41 | |
| 42 | return 0; |
| 43 | } |