-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: refactored code examples to comply with code standard (#940)
- Loading branch information
1 parent
d6fd83d
commit 947bceb
Showing
48 changed files
with
516 additions
and
577 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
#include <dpp/dpp.h> | ||
|
||
int main() { | ||
dpp::cluster bot("token"); | ||
dpp::cluster bot("token"); | ||
|
||
bot.on_log(dpp::utility::cout_logger()); | ||
/* The event is fired when someone issues your commands */ | ||
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) { | ||
/* Check which command they ran */ | ||
if (event.command.get_command_name() == "file") { | ||
dpp::message msg(event.command.channel_id, "Hey there, I've got a new file!"); | ||
|
||
dpp::message msg(event.command.channel_id, "Hey there, I've got a new file!"); | ||
|
||
/* attach the file to the message */ | ||
msg.add_file("foobar.txt", dpp::utility::read_file("path_to_your_file.txt")); | ||
/* attach the file to the message */ | ||
msg.add_file("foobar.txt", dpp::utility::read_file("path_to_your_file.txt")); | ||
|
||
/* Reply to the user with the message, with our file attached. */ | ||
event.reply(msg); | ||
} | ||
}); | ||
|
||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once<struct register_bot_commands>()) { | ||
|
||
/* Create and register a command when the bot is ready */ | ||
bot.global_command_create(dpp::slashcommand("file", "Send a message with a file attached!", bot.me.id)); | ||
} | ||
}); | ||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once<struct register_bot_commands>()) { | ||
/* Create and register a command when the bot is ready */ | ||
bot.global_command_create(dpp::slashcommand("file", "Send a message with a file attached!", bot.me.id)); | ||
} | ||
}); | ||
|
||
bot.start(dpp::st_wait); | ||
bot.start(dpp::st_wait); | ||
|
||
return 0; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,40 @@ | ||
#include <dpp/dpp.h> | ||
|
||
int main() { | ||
dpp::cluster bot("token"); | ||
dpp::cluster bot("token"); | ||
|
||
bot.on_log(dpp::utility::cout_logger()); | ||
bot.on_log(dpp::utility::cout_logger()); | ||
|
||
/* The event is fired when someone issues your commands */ | ||
/* The event is fired when someone issues your commands */ | ||
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { | ||
/* Check which command they ran */ | ||
if (event.command.get_command_name() == "file") { | ||
|
||
/* Request the image from the URL specified and capture the event in a lambda. */ | ||
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [event](const dpp::http_request_completion_t & httpRequestCompletion) { | ||
/* Request the image from the URL specified and capture the event in a lambda. */ | ||
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [event](const dpp::http_request_completion_t & httpRequestCompletion) { | ||
|
||
/* Create a message */ | ||
dpp::message msg(event.command.channel_id, "This is my new attachment:"); | ||
/* Create a message */ | ||
dpp::message msg(event.command.channel_id, "This is my new attachment:"); | ||
|
||
/* Attach the image to the message, only on success (Code 200). */ | ||
if (httpRequestCompletion.status == 200) { | ||
msg.add_file("logo.png", httpRequestCompletion.body); | ||
} | ||
/* Attach the image to the message, only on success (Code 200). */ | ||
if (httpRequestCompletion.status == 200) { | ||
msg.add_file("logo.png", httpRequestCompletion.body); | ||
} | ||
|
||
/* Send the message, with our attachment. */ | ||
event.reply(msg); | ||
}); | ||
/* Send the message, with our attachment. */ | ||
event.reply(msg); | ||
}); | ||
} | ||
}); | ||
|
||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once<struct register_bot_commands>()) { | ||
|
||
/* Create and register a command when the bot is ready */ | ||
bot.global_command_create(dpp::slashcommand("file", "Send a message with an image attached from the internet!", bot.me.id)); | ||
} | ||
}); | ||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once<struct register_bot_commands>()) { | ||
/* Create and register a command when the bot is ready */ | ||
bot.global_command_create(dpp::slashcommand("file", "Send a message with an image attached from the internet!", bot.me.id)); | ||
} | ||
}); | ||
|
||
bot.start(dpp::st_wait); | ||
bot.start(dpp::st_wait); | ||
|
||
return 0; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,39 @@ | ||
#include <dpp/dpp.h> | ||
|
||
int main() { | ||
dpp::cluster bot("token"); | ||
dpp::cluster bot("token"); | ||
|
||
bot.on_log(dpp::utility::cout_logger()); | ||
bot.on_log(dpp::utility::cout_logger()); | ||
|
||
/* The event is fired when someone issues your commands */ | ||
/* The event is fired when someone issues your commands */ | ||
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { | ||
/* Check which command they ran */ | ||
if (event.command.get_command_name() == "file") { | ||
/* Create a message. */ | ||
dpp::message msg(event.command.channel_id, ""); | ||
|
||
/* Create a message. */ | ||
dpp::message msg(event.command.channel_id, ""); | ||
/* Attach the image to the message we just created. */ | ||
msg.add_file("image.jpg", dpp::utility::read_file("path_to_your_image.jpg")); | ||
|
||
/* Attach the image to the message we just created. */ | ||
msg.add_file("image.jpg", dpp::utility::read_file("path_to_your_image.jpg")); | ||
/* Create an embed. */ | ||
dpp::embed embed; | ||
embed.set_image("attachment://image.jpg"); /* Set the image of the embed to the attached image. */ | ||
|
||
/* Create an embed. */ | ||
dpp::embed embed; | ||
embed.set_image("attachment://image.jpg"); /* Set the image of the embed to the attached image. */ | ||
/* Add the embed to the message. */ | ||
msg.add_embed(embed); | ||
|
||
/* Add the embed to the message. */ | ||
msg.add_embed(embed); | ||
|
||
event.reply(msg); | ||
event.reply(msg); | ||
} | ||
}); | ||
|
||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once<struct register_bot_commands>()) { | ||
|
||
/* Create and register a command when the bot is ready */ | ||
bot.global_command_create(dpp::slashcommand("file", "Send a local image along with an embed with the image!", bot.me.id)); | ||
} | ||
}); | ||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once<struct register_bot_commands>()) { | ||
/* Create and register a command when the bot is ready */ | ||
bot.global_command_create(dpp::slashcommand("file", "Send a local image along with an embed with the image!", bot.me.id)); | ||
} | ||
}); | ||
|
||
bot.start(dpp::st_wait); | ||
bot.start(dpp::st_wait); | ||
|
||
return 0; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,89 @@ | ||
#include <dpp/dpp.h> | ||
|
||
int main() { | ||
dpp::cluster bot("Token Was Here", 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) -> void { | ||
if (event.command.get_command_name() == "msgs-get") { | ||
int64_t limit = std::get<int64_t>(event.get_parameter("quantity")); | ||
|
||
/* get messages using ID of the channel the command was issued in */ | ||
bot.messages_get(event.command.channel_id, 0, 0, 0, limit, [event](const dpp::confirmation_callback_t& callback) -> void { | ||
if (callback.is_error()) { /* catching an error to log it */ | ||
std::cout << callback.get_error().message << std::endl; | ||
return; | ||
} | ||
|
||
auto messages = callback.get<dpp::message_map>(); | ||
/* std::get<dpp::message_map>(callback.value) would give the same result */ | ||
|
||
std::string contents; | ||
for (const auto& x : messages) { /* here we iterate through the dpp::message_map we got from callback... */ | ||
contents += x.second.content + '\n'; /* ...where x.first is ID of the current message and x.second is the message itself. */ | ||
} | ||
|
||
event.reply(contents); /* we will see all those messages we got, united as one! */ | ||
}); | ||
} else if (event.command.get_command_name() == "channel-create") { | ||
/* create a text channel */ | ||
dpp::channel channel = dpp::channel() | ||
.set_name("test") | ||
.set_guild_id(event.command.guild_id); | ||
|
||
bot.channel_create(channel, [&bot, event](const dpp::confirmation_callback_t& callback) -> void { | ||
if (callback.is_error()) { /* catching an error to log it */ | ||
bot.log(dpp::loglevel::ll_error, callback.get_error().message); | ||
return; | ||
} | ||
|
||
auto channel = callback.get<dpp::channel>(); | ||
/* std::get<dpp::channel>(callback.value) would give the same result */ | ||
|
||
/* reply with the created channel information */ | ||
dpp::message message = dpp::message("The channel's name is `" + channel.name + "`, ID is `" + std::to_string(channel.id) + " and type is `" + std::to_string(channel.get_type()) + "`."); | ||
/* note that channel types are represented as numbers */ | ||
event.reply(message); | ||
}); | ||
} else if (event.command.get_command_name() == "msg-error") { | ||
bot.message_get(0, 0, [event](const dpp::confirmation_callback_t& callback) -> void { | ||
/* the error will occur since there is no message with ID '0' that is in a channel with ID '0' (I'm not explaining why) */ | ||
if (callback.is_error()) { | ||
event.reply(callback.get_error().message); | ||
return; | ||
} | ||
|
||
/* we won't be able to get here because of the return; statement */ | ||
auto message = callback.get<dpp::message>(); | ||
event.reply(message); | ||
}); | ||
} | ||
}); | ||
|
||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once <struct register_global_commands>()) { | ||
dpp::slashcommand msgs_get("msgs-get", "Get messages", bot.me.id); | ||
|
||
constexpr int64_t min_val{1}; | ||
constexpr int64_t max_val{100}; | ||
|
||
msgs_get.add_option( | ||
dpp::command_option(dpp::co_integer, "quantity", "Quantity of messages to get. Max - 100.") | ||
.set_min_value(min_val) | ||
.set_max_value(max_val) | ||
); | ||
|
||
dpp::slashcommand channel_create("channel-create", "Create a channel", bot.me.id); | ||
dpp::slashcommand msg_error("msg-error", "Get an error instead of message :)", bot.me.id); | ||
|
||
bot.global_bulk_command_create({ msgs_get, channel_create, msg_error }); | ||
} | ||
}); | ||
|
||
bot.start(dpp::st_wait); | ||
return 0; | ||
dpp::cluster bot("Token Was Here", 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() == "msgs-get") { | ||
int64_t limit = std::get<int64_t>(event.get_parameter("quantity")); | ||
|
||
/* get messages using ID of the channel the command was issued in */ | ||
bot.messages_get(event.command.channel_id, 0, 0, 0, limit, [event](const dpp::confirmation_callback_t& callback) { | ||
if (callback.is_error()) { /* catching an error to log it */ | ||
std::cout << callback.get_error().message << std::endl; | ||
return; | ||
} | ||
|
||
auto messages = callback.get<dpp::message_map>(); | ||
/* std::get<dpp::message_map>(callback.value) would give the same result */ | ||
|
||
std::string contents; | ||
for (const auto& x : messages) { /* here we iterate through the dpp::message_map we got from callback... */ | ||
contents += x.second.content + '\n'; /* ...where x.first is ID of the current message and x.second is the message itself. */ | ||
} | ||
|
||
event.reply(contents); /* we will see all those messages we got, united as one! */ | ||
}); | ||
} else if (event.command.get_command_name() == "channel-create") { | ||
/* create a text channel */ | ||
dpp::channel channel = dpp::channel() | ||
.set_name("test") | ||
.set_guild_id(event.command.guild_id); | ||
|
||
bot.channel_create(channel, [&bot, event](const dpp::confirmation_callback_t& callback) -> void { | ||
if (callback.is_error()) { /* catching an error to log it */ | ||
bot.log(dpp::loglevel::ll_error, callback.get_error().message); | ||
return; | ||
} | ||
|
||
auto channel = callback.get<dpp::channel>(); | ||
/* std::get<dpp::channel>(callback.value) would give the same result */ | ||
|
||
/* reply with the created channel information */ | ||
dpp::message message = dpp::message("The channel's name is `" + channel.name + "`, ID is `" + std::to_string(channel.id) + " and type is `" + std::to_string(channel.get_type()) + "`."); | ||
/* note that channel types are represented as numbers */ | ||
event.reply(message); | ||
}); | ||
} else if (event.command.get_command_name() == "msg-error") { | ||
bot.message_get(0, 0, [event](const dpp::confirmation_callback_t& callback) -> void { | ||
/* the error will occur since there is no message with ID '0' that is in a channel with ID '0' (I'm not explaining why) */ | ||
if (callback.is_error()) { | ||
event.reply(callback.get_error().message); | ||
return; | ||
} | ||
|
||
/* we won't be able to get here because of the return; statement */ | ||
auto message = callback.get<dpp::message>(); | ||
event.reply(message); | ||
}); | ||
} | ||
}); | ||
|
||
bot.on_ready([&bot](const dpp::ready_t& event) { | ||
if (dpp::run_once <struct register_global_commands>()) { | ||
dpp::slashcommand msgs_get("msgs-get", "Get messages", bot.me.id); | ||
|
||
constexpr int64_t min_val{1}; | ||
constexpr int64_t max_val{100}; | ||
|
||
msgs_get.add_option( | ||
dpp::command_option(dpp::co_integer, "quantity", "Quantity of messages to get. Max - 100.") | ||
.set_min_value(min_val) | ||
.set_max_value(max_val) | ||
); | ||
|
||
dpp::slashcommand channel_create("channel-create", "Create a channel", bot.me.id); | ||
dpp::slashcommand msg_error("msg-error", "Get an error instead of message :)", bot.me.id); | ||
|
||
bot.global_bulk_command_create({ msgs_get, channel_create, msg_error }); | ||
} | ||
}); | ||
|
||
bot.start(dpp::st_wait); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.