Skip to content

Commit

Permalink
Merge branch 'master' of github.com:brainboxdotcc/triviabot
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Jun 10, 2024
2 parents 0fc75af + 22cdb88 commit 7800943
Show file tree
Hide file tree
Showing 21 changed files with 259 additions and 45 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.

2 changes: 1 addition & 1 deletion TriviaBot-Image-Submission-Guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* "Glitched" or "broken" images that crash Discord are **prohibited**.
* Pay attention to **copyright**. if you are not sure about the ownership of an image you are submitting, source images that are creative commons licensed from Wikipedia or similar.
* All question/answer images must be hosted on [imgur](https://imgur.com), for example web addresses of images must look like this: https://i.imgur.com/hhj3fFl.jpeg. Any images not matching this pattern will be rejected.
* All question/answer images must be hosted on [Discord](https://discord.com), for example web addresses of images must look like this: https://cdn.discordapp.com/attachments/546392598437298186/1138712871639973918/card.jpg. Any images not matching this pattern will be rejected.
* Images must be of a **reasonable resolution** (at least 800 pixels wide is recommended)
* Images may be **portrait, landscape or square**
* Supported image formats are: **GIF, JPEG, PNG**.
Expand Down
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
2 changes: 1 addition & 1 deletion modules/trivia/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ void TriviaModule::handle_command(const in_cmd &cmd, const dpp::interaction_crea
if (g) {
for (auto modrole = settings.moderator_roles.begin(); modrole != settings.moderator_roles.end(); ++modrole) {
/* Check for when user cache is off, and guild member passed in via the message */
for (auto role = cmd.member.roles.begin(); role != cmd.member.roles.end(); ++role) {
for (auto role = cmd.member.get_roles().begin(); role != cmd.member.get_roles().end(); ++role) {
if (*role == *modrole) {
moderator = true;
break;
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;
}

21 changes: 15 additions & 6 deletions 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 Expand Up @@ -283,15 +292,15 @@ bool TriviaModule::OnChannelDelete(const dpp::channel_delete_t &cd)
bool TriviaModule::OnGuildDelete(const dpp::guild_delete_t &gd)
{
/* Unavailable guilds means an outage. We don't remove them if it's just an outage */
if (!gd.deleted->is_unavailable()) {
if (!gd.deleted.is_unavailable()) {
{
std::unique_lock locker(settingcache_mutex);
settings_cache.erase(gd.deleted->id);
settings_cache.erase(gd.deleted.id);
}
db::backgroundquery("UPDATE trivia_guild_cache SET kicked = 1 WHERE snowflake_id = ?", {gd.deleted->id});
bot->core->log(dpp::ll_info, fmt::format("Kicked from guild id {}", gd.deleted->id));
db::backgroundquery("UPDATE trivia_guild_cache SET kicked = 1 WHERE snowflake_id = ?", {gd.deleted.id});
bot->core->log(dpp::ll_info, fmt::format("Kicked from guild id {}", gd.deleted.id));
} else {
bot->core->log(dpp::ll_info, fmt::format("Outage on guild id {}", gd.deleted->id));
bot->core->log(dpp::ll_info, fmt::format("Outage on guild id {}", gd.deleted.id));
}
return true;
}
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
11 changes: 6 additions & 5 deletions modules/trivia/webrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,19 @@ void cache_user(const dpp::user *_user, const dpp::guild *_guild, const dpp::gui
{user_id, _user->username, _user->discriminator, _user->avatar.to_string(), _user->username, _user->discriminator, _user->avatar.to_string()});

db::backgroundquery("INSERT INTO trivia_guild_cache (snowflake_id, name, icon, owner_id) VALUES('?', '?', '?', '?') ON DUPLICATE KEY UPDATE name = '?', icon = '?', owner_id = '?', kicked = 0",
{guild_id, _guild->name, _guild->icon.to_string(), _guild->owner_id, _guild->name, _guild->icon.to_string(), _guild->owner_id});
{guild_id, _guild->name, _guild->icon.as_iconhash().to_string(), _guild->owner_id, _guild->name, _guild->icon.as_iconhash().to_string(), _guild->owner_id});

std::string member_roles;
std::string comma_roles;
for (auto r = gi->roles.begin();r != gi->roles.end(); ++r) {
for (auto r = gi->get_roles().begin();r != gi->get_roles().end(); ++r) {
member_roles.append(std::to_string(*r)).append(" ");
}
member_roles = trim(member_roles);
db::backgroundquery("INSERT INTO trivia_guild_membership (guild_id, user_id, roles) VALUES('?', '?', '?') ON DUPLICATE KEY UPDATE roles = '?'",
{guild_id, user_id, member_roles, member_roles});

for (auto n = _guild->roles.begin(); n != _guild->roles.end(); ++n) {
// TODO: Gather these in the dashboard via a REST API request when the user wants them. No need to be constantly caching and writing them here, removes our need to check roles at all.
/*for (auto n = _guild->roles.begin(); n != _guild->roles.end(); ++n) {
dpp::role* r = dpp::find_role(*n);
if (r) {
comma_roles.append(std::to_string(r->id)).append(",");
Expand All @@ -448,9 +449,9 @@ void cache_user(const dpp::user *_user, const dpp::guild *_guild, const dpp::gui
});
}
}
comma_roles = trim(comma_roles.substr(0, comma_roles.length() - 1));
comma_roles = trim(comma_roles.substr(0, comma_roles.length() - 1));*/
/* Delete any that have been deleted from discord */
db::backgroundquery("DELETE FROM trivia_role_cache WHERE guild_id = ? AND id NOT IN (" + comma_roles + ")", {guild_id});
//db::backgroundquery("DELETE FROM trivia_role_cache WHERE guild_id = ? AND id NOT IN (" + comma_roles + ")", {guild_id});
}

/* Fetch a question by ID from the database */
Expand Down
Loading

0 comments on commit 7800943

Please sign in to comment.