Skip to content

Commit

Permalink
add support for entitlements
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Jun 10, 2024
1 parent 2408be4 commit 22cdb88
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/runConfigurations/ssod.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/triviabot.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions include/sporks/bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ class Bot {
void onVoiceStateUpdate (const dpp::voice_state_update_t &event);
void onVoiceServerUpdate (const dpp::voice_server_update_t &event);
void onWebhooksUpdate (const dpp::webhooks_update_t &event);
void onEntitlementDelete(const dpp::entitlement_delete_t& ed);
void onEntitlementCreate(const dpp::entitlement_create_t& ed);
void onEntitlementUpdate(const dpp::entitlement_update_t& ed);

static std::string GetConfig(const std::string &name);

Expand Down
6 changes: 6 additions & 0 deletions include/sporks/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ enum Implementation
I_OnVoiceStateUpdate,
I_OnVoiceServerUpdate,
I_OnWebhooksUpdate,
I_OnEntitlementCreate,
I_OnEntitlementUpdate,
I_OnEntitlementDelete,
I_END
};

Expand Down Expand Up @@ -232,6 +235,9 @@ class Module {
virtual bool OnVoiceStateUpdate(const dpp::voice_state_update_t &obj);
virtual bool OnVoiceServerUpdate(const dpp::voice_server_update_t &obj);
virtual bool OnWebhooksUpdate(const dpp::webhooks_update_t &obj);
virtual bool OnEntitlementCreate(const dpp::entitlement_create_t &obj);
virtual bool OnEntitlementUpdate(const dpp::entitlement_update_t &obj);
virtual bool OnEntitlementDelete(const dpp::entitlement_delete_t &obj);

/* Emit a simple text only embed to a channel, many modules use this for error reporting */
void EmbedSimple(const std::string &message, int64_t channelID);
Expand Down
36 changes: 36 additions & 0 deletions modules/trivia/entitlement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "trivia.h"
#include <sporks/database.h>
#include <dpp/dpp.h>

bool TriviaModule::OnEntitlementCreate(const dpp::entitlement_create_t& entitlement)
{
db::query(
"INSERT INTO premium_credits (user_id, guild_id, subscription_id, active, since, plan_id, payment_failed, created_at, updated_at)"
"VALUES(?, ?, ?, 1, now(), 'ssod-monthly', 0, now(), now()) ON DUPLICATE KEY UPDATE subscription_id = ?, active = 1",
{ entitlement.created.user_id, entitlement.created.guild_id, entitlement.created.subscription_id, entitlement.created.subscription_id }
);
return true;
}

bool TriviaModule::OnEntitlementUpdate(const dpp::entitlement_update_t& entitlement)
{
db::query(
"UPDATE premium_credits SET active = ?, updated_at = now() WHERE user_id = ? AND subscription_id = ?",
{
entitlement.updating_entitlement.is_deleted() || entitlement.updating_entitlement.ends_at < time(nullptr) ? 0 : 1,
entitlement.updating_entitlement.user_id,
entitlement.updating_entitlement.id
}
);
return true;
}

bool TriviaModule::OnEntitlementDelete(const dpp::entitlement_delete_t& entitlement)
{
db::query(
"UPDATE premium_credits SET active = 0, cancel_date = now(), updated_at = now() WHERE user_id = ? AND subscription_id = ?",
{ entitlement.deleted.user_id, entitlement.deleted.subscription_id }
);
return true;
}

11 changes: 10 additions & 1 deletion modules/trivia/trivia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ TriviaModule::TriviaModule(Bot* instigator, ModuleLoader* ml) : Module(instigato
srand(time(NULL) * time(NULL));

/* Attach D++ events to module */
ml->Attach({ I_OnMessage, I_OnPresenceUpdate, I_OnChannelDelete, I_OnGuildDelete, I_OnAllShardsReady, I_OnGuildCreate }, this);
ml->Attach({ I_OnMessage,
I_OnPresenceUpdate,
I_OnChannelDelete,
I_OnGuildDelete,
I_OnAllShardsReady,
I_OnGuildCreate,
I_OnEntitlementCreate,
I_OnEntitlementDelete,
I_OnEntitlementUpdate
}, this);

/* Various regular expressions */
number_tidy_dollars = new PCRE("^([\\d\\,]+)\\s+dollars$");
Expand Down
4 changes: 4 additions & 0 deletions modules/trivia/trivia.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ class TriviaModule : public Module
virtual bool OnAllShardsReady();
virtual bool OnChannelDelete(const dpp::channel_delete_t &cd);
virtual bool OnGuildDelete(const dpp::guild_delete_t &gd);
virtual bool OnEntitlementCreate(const dpp::entitlement_create_t& entitlement);
virtual bool OnEntitlementUpdate(const dpp::entitlement_update_t& entitlement);
virtual bool OnEntitlementDelete(const dpp::entitlement_delete_t& entitlement);


/* Returns a local count */
uint64_t GetActiveLocalGames();
Expand Down
25 changes: 21 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ void Bot::onServerDelete(const dpp::guild_delete_t& gd) {
FOREACH_MOD(I_OnGuildDelete, OnGuildDelete(gd));
}

void Bot::onEntitlementDelete(const dpp::entitlement_delete_t& ed) {
FOREACH_MOD(I_OnEntitlementDelete, OnEntitlementDelete(ed));
}

void Bot::onEntitlementCreate(const dpp::entitlement_create_t& ed) {
FOREACH_MOD(I_OnEntitlementCreate, OnEntitlementCreate(ed));
}

void Bot::onEntitlementUpdate(const dpp::entitlement_update_t& ed) {
FOREACH_MOD(I_OnEntitlementUpdate, OnEntitlementUpdate(ed));
}

int main(int argc, char** argv) {

int dev = 0; /* Note: getopt expects ints, this is actually treated as bool */
Expand Down Expand Up @@ -323,11 +335,13 @@ int main(int argc, char** argv) {

/* Set cache policy for D++ library
* --------------------------------
* User caching: none
* Emoji caching: none
* Role caching: none (WAS: aggressive)
* User caching: none
* Emoji caching: none
* Role caching: none (WAS: aggressive)
* Channel caching: aggressive
* Guild caching: aggressive
*/
dpp::cache_policy_t cp = { dpp::cp_none, dpp::cp_none, dpp::cp_none };
dpp::cache_policy_t cp = { dpp::cp_none, dpp::cp_none, dpp::cp_none, dpp::cp_aggressive, dpp::cp_aggressive };
/* Construct cluster */
dpp::cluster bot(token, intents, dev ? 1 : from_string<uint32_t>(Bot::GetConfig("shardcount"), std::dec), clusterid, maxclusters, true, cp);

Expand Down Expand Up @@ -370,6 +384,9 @@ int main(int argc, char** argv) {
bot.on_guild_member_add(std::bind(&Bot::onMember, &client, std::placeholders::_1));
bot.on_guild_create(std::bind(&Bot::onServer, &client, std::placeholders::_1));
bot.on_guild_delete(std::bind(&Bot::onServerDelete, &client, std::placeholders::_1));
bot.on_entitlement_create(std::bind(&Bot::onEntitlementCreate, &client, std::placeholders::_1));
bot.on_entitlement_update(std::bind(&Bot::onEntitlementUpdate, &client, std::placeholders::_1));
bot.on_entitlement_delete(std::bind(&Bot::onEntitlementDelete, &client, std::placeholders::_1));

bot.set_websocket_protocol(dpp::ws_etf);

Expand Down
18 changes: 18 additions & 0 deletions src/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ const char* StringNames[I_END + 1] = {
"I_OnVoiceStateUpdate",
"I_OnVoiceServerUpdate",
"I_OnWebhooksUpdate",
"I_OnEntitlementCreate",
"I_OnEntitlementUpdate",
"I_OnEntitlementDelete",
"I_END"
};

Expand Down Expand Up @@ -352,6 +355,21 @@ bool Module::OnPresenceUpdate()
return true;
}

bool Module::OnEntitlementDelete(const dpp::entitlement_delete_t& ed)
{
return true;
}

bool Module::OnEntitlementCreate(const dpp::entitlement_create_t& ed)
{
return true;
}

bool Module::OnEntitlementUpdate(const dpp::entitlement_update_t& ed)
{
return true;
}

bool Module::OnTypingStart(const dpp::typing_start_t &obj)
{
return true;
Expand Down

0 comments on commit 22cdb88

Please sign in to comment.