diff --git a/docpages/example_code/autocomplete.cpp b/docpages/example_code/autocomplete.cpp index da29127e30..380cb45785 100644 --- a/docpages/example_code/autocomplete.cpp +++ b/docpages/example_code/autocomplete.cpp @@ -50,10 +50,10 @@ int main() */ std::string uservalue = std::get(opt.value); bot.interaction_response_create(event.command.id, event.command.token, dpp::interaction_response(dpp::ir_autocomplete_reply) - .add_autocomplete_choice(dpp::command_option_choice("squids", "lots of squids")) - .add_autocomplete_choice(dpp::command_option_choice("cats", "a few cats")) - .add_autocomplete_choice(dpp::command_option_choice("dogs", "bucket of dogs")) - .add_autocomplete_choice(dpp::command_option_choice("elephants", "bottle of elephants")) + .add_autocomplete_choice(dpp::command_option_choice("squids", std::string("lots of squids"))) + .add_autocomplete_choice(dpp::command_option_choice("cats", std::string("a few cats"))) + .add_autocomplete_choice(dpp::command_option_choice("dogs", std::string("bucket of dogs"))) + .add_autocomplete_choice(dpp::command_option_choice("elephants", std::string("bottle of elephants"))) ); bot.log(dpp::ll_debug, "Autocomplete " + opt.name + " with value '" + uservalue + "' in field " + event.name); break; diff --git a/docpages/example_code/editing_messages.cpp b/docpages/example_code/editing_messages1.cpp similarity index 57% rename from docpages/example_code/editing_messages.cpp rename to docpages/example_code/editing_messages1.cpp index b1c5bd9e50..04e7640ba7 100644 --- a/docpages/example_code/editing_messages.cpp +++ b/docpages/example_code/editing_messages1.cpp @@ -1,8 +1,8 @@ #include int main() { - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); /* the second argument is a bitmask of intents - i_message_content is needed to get messages */ + dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content); bot.on_log(dpp::utility::cout_logger()); @@ -13,9 +13,8 @@ int main() { } else if (event.command.get_command_name() == "msg-edit") { const auto content = std::get(event.get_parameter("content")); - /* get message to edit it after */ + /* get the message to edit it after. here string will automatically be converted to snowflake */ const dpp::snowflake msg_id = std::get(event.get_parameter("msg-id")); - /* here string will automatically be converted to snowflake */ bot.message_get(msg_id, event.command.channel_id, [&bot, content, event](const dpp::confirmation_callback_t& callback) { if (callback.is_error()) { @@ -27,51 +26,24 @@ int main() { /* change the message content and edit the message itself */ message.set_content(content); bot.message_edit(message); - event.reply("Message content is now `" + content + "`."); }); - } else if (event.command.get_command_name() == "channel-edit") { - const auto name = std::get(event.get_parameter("name")); - - /* get the channel to edit it after */ - const auto channel_id = std::get(event.get_parameter("channel")); - - bot.channel_get(channel_id, [&bot, name, event](const dpp::confirmation_callback_t& callback) { - if (callback.is_error()) { - event.reply("error"); - return; - } - auto channel = callback.get(); - - /* change the channel name and edit the channel itself */ - channel.set_name(name); - bot.channel_edit(channel); - - event.reply("Channel name is now `" + name + "`."); - }); } }); bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once ()) { dpp::slashcommand msg_edit("msg-edit", "Edit a message sent by the bot", bot.me.id); msg_edit.add_option(dpp::command_option(dpp::co_string, "msg-id", "ID of the message to edit", true)); /* true for required option */ msg_edit.add_option(dpp::command_option(dpp::co_string, "content", "New content for the message", true)); /* same here */ - dpp::slashcommand channel_edit("channel-edit", "Edit the name of channel specified", bot.me.id); - - channel_edit.add_option(dpp::command_option(dpp::co_channel, "channel", "Channel to edit", true)); - channel_edit.add_option(dpp::command_option(dpp::co_string, "name", "New name for the channel", true)); - dpp::slashcommand msg_send("msg-send", "Send my message", bot.me.id); - bot.global_bulk_command_create({ msg_edit, channel_edit, msg_send }); + bot.global_bulk_command_create({ msg_edit, msg_send }); } }); bot.start(dpp::st_wait); - return 0; } diff --git a/docpages/example_code/editing_messages2.cpp b/docpages/example_code/editing_messages2.cpp new file mode 100644 index 0000000000..a3595d8e81 --- /dev/null +++ b/docpages/example_code/editing_messages2.cpp @@ -0,0 +1,45 @@ +#include + +int main() { + /* the second argument is a bitmask of intents - i_message_content is needed to get messages */ + dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content); + + bot.on_log(dpp::utility::cout_logger()); + + /* The event is fired when someone issues your commands */ + bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { + if (event.command.get_command_name() == "channel-edit") { + const auto name = std::get(event.get_parameter("name")); + + /* get the channel to edit it after */ + const auto channel_id = std::get(event.get_parameter("channel")); + bot.channel_get(channel_id, [&bot, name, event](const dpp::confirmation_callback_t& callback) { + if (callback.is_error()) { + event.reply("error"); + return; + } + auto channel = callback.get(); + + /* change the channel name and edit the channel itself */ + channel.set_name(name); + bot.channel_edit(channel); + event.reply("Channel name is now `" + name + "`."); + }); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + + if (dpp::run_once ()) { + dpp::slashcommand channel_edit("channel-edit", "Edit the name of channel specified", bot.me.id); + + channel_edit.add_option(dpp::command_option(dpp::co_channel, "channel", "Channel to edit", true)); + channel_edit.add_option(dpp::command_option(dpp::co_string, "name", "New name for the channel", true)); + + bot.global_command_create(channel_edit); + } + }); + + bot.start(dpp::st_wait); + return 0; +} diff --git a/docpages/example_code/editing_messages3.cpp b/docpages/example_code/editing_messages3.cpp new file mode 100644 index 0000000000..5d598dfbe8 --- /dev/null +++ b/docpages/example_code/editing_messages3.cpp @@ -0,0 +1,59 @@ +#include + +int main() { + /* the second argument is a bitmask of intents - i_message_content is needed to get messages */ + dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content); + + bot.on_log(dpp::utility::cout_logger()); + + /* The event is fired when someone issues your commands */ + bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { + if (event.command.get_command_name() == "embed-send") { + dpp::embed embed = dpp::embed() + .set_color(dpp::colors::sti_blue) + .set_title("like and subscribe") + .set_url("https://dpp.dev/") + .set_author("Some author", "https://dpp.dev/", "https://dpp.dev/DPP-Logo.png") + .set_description("Creator is "); + event.reply(embed); + } else if (event.command.get_command_name() == "embed-edit") { + const auto description = std::get(event.get_parameter("desc")); + + /* get the message to edit its embed after. here string will automatically be converted to snowflake */ + const dpp::snowflake msg_id = std::get(event.get_parameter("msg-id")); + + bot.message_get(msg_id, event.command.channel_id, [&bot, description, event](const dpp::confirmation_callback_t& callback) { + if (callback.is_error()) { + event.reply("error"); + return; + } + auto message = callback.get(); + auto& embeds = message.embeds; + + /* change the embed description and edit the message itself. + * since we're using a reference, what changes in embeds changes in message.embeds + */ + embeds[0].set_description(description); + + bot.message_edit(message); + event.reply("Embed description is now `" + description + "`."); + }); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once ()) { + dpp::slashcommand embed_send("embed-send", "Send my embed", bot.me.id); + + dpp::slashcommand embed_edit("embed-edit", "Edit an embed sent by the bot", bot.me.id); + + embed_edit.add_option(dpp::command_option(dpp::co_string, "msg-id", "ID of the embed to edit", true)); /* true for required option */ + embed_edit.add_option(dpp::command_option(dpp::co_string, "desc", "New description for the embed", true)); /* same here */ + + bot.global_bulk_command_create({ embed_send, embed_edit }); + } + }); + + bot.start(dpp::st_wait); + return 0; +} diff --git a/docpages/example_programs/the_basics/editing-channels-and-messages.md b/docpages/example_programs/the_basics/editing-channels-and-messages.md index a94ab7ccd8..af4b733d1e 100644 --- a/docpages/example_programs/the_basics/editing-channels-and-messages.md +++ b/docpages/example_programs/the_basics/editing-channels-and-messages.md @@ -1,15 +1,46 @@ -\page editing-channels-and-messages Editing Channels and Messages - -Sometimes we need to update an object, such as message or channel. At first, it might seem confusing, but it's actually really simple! You just need to use an object with identical properties you don't need to update. NOTE: your bot can't edit messages sent by others. - -\note This example uses callback functions. To see more information about them, visit \ref callback-functions. - -\include{cpp} editing_messages.cpp - -Before editing: - -\image html stuff_edit1.png - -After editing: - -\image html stuff_edit2.png \ No newline at end of file +\page editing-channels-and-messages Editing Channels and Messages + +Sometimes we need to update an object, such as a message (whether it's plain text or an embed) or a channel. At first, it might seem confusing, but it's actually really simple! You need an object with all the properties being identical to the existing one. Say you're editing a message. You need to have an object with its ID the same as the one in Discord. Then you replace what you need, such as its content. + +\note This example uses callback functions and embeds. To see more information about them, visit \ref callback-functions and + +## Editing messages +Here we send a message and edit it after. To do so, we first reply to the command `msg-send` with some text, "This is a message" in our case. As described above, on the next step the message object is taken and the text is replaced with whatever the user desires. + +\include{cpp} editing_messages1.cpp + +\note Your bot can't edit messages sent by others!\ref embed-message. + +Before editing the message: + +\image html stuff_edit1.png + +After editing the message: + +\image html stuff_edit2.png + +## Editing channels +Now we'll want to edit an existing channel - its name in this case. This works similarly to how messages are edited. + +\include{cpp} editing_messages2.cpp + +Before editing the channel: + +\image html stuff_edit3.png + +After editing the channel: + +\image html stuff_edit4.png + +## Editing embeds +Now let's send an embed and edit it. If a message has one `content` field, it can have a few `embed` fields, up to 10 to be precise. So we first get the embed we want and edit and change its description. + +\include{cpp} editing_messages3.cpp + +Before editing the embed: + +\image html stuff_edit5.png + +Finally, after editing the embed: + +\image html stuff_edit6.png diff --git a/docpages/images/stuff_edit1.png b/docpages/images/stuff_edit1.png index bf64878610..745891a037 100644 Binary files a/docpages/images/stuff_edit1.png and b/docpages/images/stuff_edit1.png differ diff --git a/docpages/images/stuff_edit2.png b/docpages/images/stuff_edit2.png index cf77dd5fd8..4571433f58 100644 Binary files a/docpages/images/stuff_edit2.png and b/docpages/images/stuff_edit2.png differ diff --git a/docpages/images/stuff_edit3.png b/docpages/images/stuff_edit3.png new file mode 100644 index 0000000000..7ce5dd68eb Binary files /dev/null and b/docpages/images/stuff_edit3.png differ diff --git a/docpages/images/stuff_edit4.png b/docpages/images/stuff_edit4.png new file mode 100644 index 0000000000..a4166a18ad Binary files /dev/null and b/docpages/images/stuff_edit4.png differ diff --git a/docpages/images/stuff_edit5.png b/docpages/images/stuff_edit5.png new file mode 100644 index 0000000000..48579eed9d Binary files /dev/null and b/docpages/images/stuff_edit5.png differ diff --git a/docpages/images/stuff_edit6.png b/docpages/images/stuff_edit6.png new file mode 100644 index 0000000000..a61be95c25 Binary files /dev/null and b/docpages/images/stuff_edit6.png differ diff --git a/src/dpp/sslclient.cpp b/src/dpp/sslclient.cpp index f9aa2df1ac..ee7f4d7af0 100644 --- a/src/dpp/sslclient.cpp +++ b/src/dpp/sslclient.cpp @@ -208,7 +208,7 @@ int connect_with_timeout(dpp::socket sockfd, const struct sockaddr *addr, sockle pollfd pfd = {}; pfd.fd = sockfd; pfd.events = POLLOUT; - int r = poll(&pfd, 1, 10); + int r = ::poll(&pfd, 1, 10); if (r > 0 && pfd.revents & POLLOUT) { rc = 0; } else if (r != 0 || pfd.revents & POLLERR) { @@ -269,7 +269,7 @@ ssl_client::ssl_client(const std::string &_hostname, const std::string &_port, b pollfd pfd = {}; pfd.fd = iter->second.sfd; pfd.events = POLLOUT; - int r = poll(&pfd, 1, 1); + int r = ::poll(&pfd, 1, 1); if (time(nullptr) > (iter->second.created + 60) || r < 0 || pfd.revents & POLLERR) { make_new = true; /* This connection is dead, free its resources and make a new one */ @@ -480,7 +480,7 @@ void ssl_client::read_loop() const int64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); int poll_time = 1000 - (now % 1000); poll_time = poll_time > 400 ? 1000 : poll_time + poll_time / 3 + 1; - r = poll(pfd, sockets, now / 1000 == (int64_t)last_tick ? poll_time : 0); + r = ::poll(pfd, sockets, now / 1000 == (int64_t)last_tick ? poll_time : 0); if (r == 0) { continue;