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

[Docs]: added object editing example #847

Merged
merged 21 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f8a3b08
Fixed typo's and added Callback Functions examples
Henonicks Aug 31, 2023
8e7ed3f
Added more explanation to Callback Functions example, removed unneces…
Henonicks Sep 1, 2023
b211d2a
Merge branch 'dev' into dev
Henonicks Sep 1, 2023
426d8bf
Merge branch 'dev' into dev
Henonicks Sep 1, 2023
e712670
Changed all // comments to /* comments */
Henonicks Sep 1, 2023
fdf4148
Fixed some real minor stuff so that the code follows the guide
Henonicks Sep 1, 2023
b316f8d
Fixed more stuff that didn't push
Henonicks Sep 1, 2023
c2439ad
Put the missing quote
Henonicks Sep 1, 2023
e3132d3
Put a missing comma in my example.
Henonicks Sep 1, 2023
4c85c17
In the action is now in action
Henonicks Sep 1, 2023
ef9eaca
callback.get<without a space>, extra line after else-if is gon
Henonicks Sep 1, 2023
93247c8
Merge branch 'brainboxdotcc:dev' into dev
Henonicks Sep 2, 2023
258e9e1
Merge branch 'brainboxdotcc:dev' into dev
Henonicks Sep 4, 2023
4de1dac
Added object editing example
Henonicks Sep 7, 2023
60047ae
Formatted the code that creates slashcommands, replaced std::string w…
Henonicks Sep 7, 2023
7aa248b
Fixing the most embarrasing mistake ever
Henonicks Sep 7, 2023
6497d1b
Renamed the page, changed reference representation
Henonicks Sep 7, 2023
55058ec
Fixed the code block issue that made it think that images are part of…
Henonicks Sep 7, 2023
f8f2608
Plural instead of Channel and Message, const where it can be const
Henonicks Sep 7, 2023
9ef6675
Added a note about bots' inability to edit messages sent by others
Henonicks Sep 7, 2023
defcf43
why didn't i google what plural means
Henonicks Sep 7, 2023
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
1 change: 1 addition & 0 deletions docpages/example_programs/the_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ These example programs are great to get started with simple things in the D++ li
* \subpage attach-file "Attaching a file"
* \subpage webhooks "Webhooks"
* \subpage callback-functions "Using Callback Functions"
* \subpage editing-channels-and-messages
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
\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.

~~~~~~~~~~{.cpp}
#include <dpp/dpp.h>

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 */

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() == "msg-send") {
event.reply("That's a message");
} else if (event.command.get_command_name() == "msg-edit") {
const auto content = std::get<std::string>(event.get_parameter("content"));

/* get message to edit it after */
const dpp::snowflake msg_id = std::get<std::string>(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()) {
event.reply("error");
return;
}
auto message = callback.get<dpp::message>();

/* 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<std::string>(event.get_parameter("name"));

/* get the channel to edit it after */
const auto channel_id = std::get<dpp::snowflake>(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<dpp::channel>();

/* 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 <struct register_global_commands>()) {
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.start(dpp::st_wait);
return 0;
}
~~~~~~~~~~

Before editing:

\image html stuff_edit1.png

After editing:

\image html stuff_edit2.png
Binary file added docpages/images/stuff_edit1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docpages/images/stuff_edit2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.