Skip to content

Commit 386f33d

Browse files
committed
handle role events
1 parent daef6bc commit 386f33d

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

Discord.C++/Discord.cpp

+28-2
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,39 @@ void DiscordCPP::Discord::handle_raw_event(const std::string &event_name, const
363363
guild->_update_emojis(emojis);
364364
} else if (event_name == "GUILD_ROLE_CREATE") {
365365
Guild *guild = get_guild(data.at("guild_id").get<std::string>());
366+
Role role = Role(data.at("role"), guild->get_id(), get_token());
366367
guild->_add_role(Role(data.at("role"), guild->get_id(), get_token()));
368+
369+
try {
370+
on_role_create(role);
371+
} catch (const std::exception &e) {
372+
log.error("ignoring exception in on_role_create: " + std::string(e.what()));
373+
}
367374
} else if (event_name == "GUILD_ROLE_UPDATE") {
368375
Guild *guild = get_guild(data.at("guild_id").get<std::string>());
369-
guild->_update_role(Role(data.at("role"), guild->get_id(), get_token()));
376+
Role role = Role(data.at("role"), guild->get_id(), get_token());
377+
guild->_update_role(role);
378+
379+
try {
380+
on_role_update(role);
381+
} catch (const std::exception &e) {
382+
log.error("ignoring exception in on_role_update: " + std::string(e.what()));
383+
}
370384
} else if (event_name == "GUILD_ROLE_DELETE") {
385+
std::string role_id = data.at("role_id").get<std::string>();
371386
Guild *guild = get_guild(data.at("guild_id").get<std::string>());
372-
guild->_remove_role(data.at("role_id").get<std::string>());
387+
388+
for (Role role : guild->get_roles()) {
389+
if (role.get_id() == role_id) {
390+
guild->_remove_role(role_id);
391+
try {
392+
on_role_delete(role);
393+
} catch (const std::exception &e) {
394+
log.error("ignoring exception in on_role_delete: " + std::string(e.what()));
395+
}
396+
break;
397+
}
398+
}
373399
} else if (event_name == "MESSAGE_CREATE") {
374400
if (has_value(data, ("guild_id"))) {
375401
std::string guild_id = data.at("guild_id").get<std::string>();

Discord.C++/Discord.h

+18-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "MainGateway.h"
2525
#include "Message.h"
2626
#include "Reaction.h"
27+
#include "Role.h"
2728
#include "TextChannel.h"
2829
#include "Threadpool.h"
2930
#include "User.h"
@@ -136,9 +137,24 @@ class Discord : public DiscordObject {
136137
*/
137138
virtual void on_typing_start(User, TextChannel, unsigned int) {}
138139

140+
/** called when a role was created
141+
@param[in] role the created role
142+
*/
143+
virtual void on_role_create(Role) {}
144+
145+
/** called when a role was updated
146+
@param[in] role the updated role
147+
*/
148+
virtual void on_role_update(Role) {}
149+
150+
/** called when a role was deleted
151+
@param[in] role the deleted role
152+
*/
153+
virtual void on_role_delete(Role) {}
154+
139155
/** called when an interaction was created
140-
@param[in] interaction the Interaction that was received
141-
*/
156+
@param[in] interaction the Interaction that was received
157+
*/
142158
virtual void on_interaction(Interaction) {}
143159

144160
public:

Discord.C++/Intents.h

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class Intents {
3232
* This affects the following events:
3333
* - on_user_join
3434
* - on_user_remove
35+
* - on_role_create
36+
* - on_role_update
37+
* - on_role_delete
3538
*/
3639
static const unsigned int MEMBERS = 1 << 1;
3740

test_bot/main.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,27 @@ class Client : public Discord {
409409
" in channel: " + message.get_channel_id());
410410
}
411411

412+
void on_role_create(Role role) override {
413+
log.info("Role " + role.get_name() +
414+
" (id: " + role.get_id() +
415+
") was created in guild " + role.get_guild_id() +
416+
" with permissions " + std::string(role.get_permissions()));
417+
}
418+
419+
void on_role_update(Role role) override {
420+
log.info("Role " + role.get_name() +
421+
" (id: " + role.get_id() +
422+
") was upated in guild " + role.get_guild_id() +
423+
" with permissions " + std::string(role.get_permissions()));
424+
}
425+
426+
void on_role_delete(Role role) override {
427+
log.info("Role " + role.get_name() +
428+
" (id: " + role.get_id() +
429+
") was deleted in guild " + role.get_guild_id() +
430+
" with permissions " + std::string(role.get_permissions()));
431+
}
432+
412433
void on_interaction(Interaction interaction) override {
413434
if (interaction.get_type() != Interaction::Type::APPLICATION_COMMAND) {
414435
return;

0 commit comments

Comments
 (0)