Skip to content

Commit

Permalink
fix: fix for issue #357
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Apr 16, 2022
1 parent a19f901 commit 1c74713
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
9 changes: 3 additions & 6 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ template<class T> class event_router_t {
* @brief A function to be called whenever the method is called, to check
* some condition that is required for this event to trigger correctly.
*/
std::function<void()> warning;
std::function<void(const T&)> warning;

protected:

Expand All @@ -364,7 +364,7 @@ template<class T> class event_router_t {
*
* @param warning_function A checking function to call
*/
void set_warning_callback(std::function<void()> warning_function) {
void set_warning_callback(std::function<void(const T&)> warning_function) {
warning = warning_function;
}

Expand All @@ -383,7 +383,7 @@ template<class T> class event_router_t {
*/
void call(const T& event) const {
if (warning) {
warning();
warning(event);
}
std::shared_lock l(lock);
std::for_each(dispatch_container.begin(), dispatch_container.end(), [&](auto &ev) {
Expand Down Expand Up @@ -441,9 +441,6 @@ template<class T> class event_router_t {
* detach the listener from the event later if necessary.
*/
event_handle attach(std::function<void(const T&)> func) {
if (warning) {
warning();
}
std::unique_lock l(lock);
event_handle h = __next_handle++;
dispatch_container.emplace(h, func);
Expand Down
32 changes: 18 additions & 14 deletions src/dpp/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,18 @@ namespace dpp {
*/
thread_local std::string audit_reason;

event_handle __next_handle = 1;

/**
* @brief Make a warning lambda for missing message intents
*
*
* @tparam T type of parameter for the event in the router
* @param cl Creating cluster
* @param required_intent Intent which is required
* @param message Message to display
* @return std::function<void()> Returned lambda
* @return std::function<void(const T&)> Returned lambda
*/
std::function<void()> make_intent_warning(cluster* cl, const intents required_intent, const std::string& message) {
return [cl, required_intent, message]() {
if (!(cl->intents & required_intent)) {
template<typename T> std::function<void(const T&)> make_intent_warning(cluster* cl, const intents required_intent, const std::string& message) {
return [cl, required_intent, message](const T& event) {
if (!(cl->intents & required_intent) && event.msg.guild_id) {
cl->log(ll_warning, message);
}
};
Expand All @@ -82,13 +81,18 @@ cluster::cluster(const std::string &_token, uint32_t _intents, uint32_t _shards,
raw_rest = new request_queue(this, request_threads);

/* Add checks for missing intents, these emit a one-off warning to the log if bound without the right intents */
std::function<void()> message_intents_warning = make_intent_warning(this, i_message_content, "You have attached an event to cluster::on_message_*() but have not specified the privileged intent dpp::i_message_content. Message content, embeds, attachments, and components on received guild messages will be empty.");
std::function<void()> member_intents_warning = make_intent_warning(this, i_guild_members, "You have attached an event to cluster::on_guild_member_*() but have not specified the privileged intent dpp::i_guild_members. These events will not fire, and the cache will only fill when a user interacts with the bot or channels.");
on_message_create.set_warning_callback(message_intents_warning);
on_message_update.set_warning_callback(message_intents_warning);
on_guild_member_add.set_warning_callback(member_intents_warning);
on_guild_member_remove.set_warning_callback(member_intents_warning);
on_guild_member_update.set_warning_callback(member_intents_warning);
on_message_create.set_warning_callback(
make_intent_warning<message_create_t>(
this,
i_message_content,
"You have attached an event to cluster::on_message_create() but have not specified the privileged intent dpp::i_message_content. Message content, embeds, attachments, and components on received guild messages will be empty.")
);
on_message_update.set_warning_callback(
make_intent_warning<message_update_t>(
this,
i_message_content,
"You have attached an event to cluster::on_message_update() but have not specified the privileged intent dpp::i_message_content. Message content, embeds, attachments, and components on received guild messages will be empty.")
);
}

cluster::~cluster()
Expand Down

0 comments on commit 1c74713

Please sign in to comment.