Skip to content

Commit

Permalink
tons of fixes, including premium guild assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Aug 23, 2024
1 parent 478f717 commit 7738b3e
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 626 deletions.
45 changes: 5 additions & 40 deletions include/sporks/bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,12 @@ class Bot {
/* True if the bot has member intents */
bool memberintents;

/* Threads */
std::thread* thr_presence;

/* Set to true if all threads are to end */
bool terminate;

uint32_t my_cluster_id;

uint32_t maxclusters;

uint32_t shard_init_count;

/* Thread handlers */
void UpdatePresenceThread(); /* Updates the bot presence every 120 seconds */

void SetSignals();
void UpdatePresenceTimerTick(); /* Updates the bot presence every 30 seconds */

public:
/* D++ cluster */
Expand All @@ -87,9 +77,9 @@ class Bot {
Bot(bool development, bool testing, bool intents, dpp::cluster* dppcluster, uint32_t cluster_id);
virtual ~Bot();

bool IsDevMode();
bool IsTestMode();
bool HasMemberIntents();
bool IsDevMode() const;
bool IsTestMode() const;
bool HasMemberIntents() const;

ModuleLoader* Loader;

Expand All @@ -101,40 +91,15 @@ class Bot {

uint32_t GetClusterID();
void SetClusterID(uint32_t c);
uint32_t GetMaxClusters();
uint32_t GetMaxClusters() const;

void onReady(const dpp::ready_t &ready);
void onServer(const dpp::guild_create_t &gc);
void onMember(const dpp::guild_member_add_t &gma);
void onChannel(const dpp::channel_create_t &channel);
void onMessage(const dpp::message_create_t &message);
void onChannelDelete(const dpp::channel_delete_t &cd);
void onServerDelete(const dpp::guild_delete_t &gd);
void onTypingStart (const dpp::typing_start_t &event);
void onMessageUpdate (const dpp::message_update_t &event);
void onMessageDelete (const dpp::message_delete_t &event);
void onMessageDeleteBulk (const dpp::message_delete_bulk_t &event);
void onGuildUpdate (const dpp::guild_update_t &event);
void onMessageReactionAdd (const dpp::message_reaction_add_t &event);
void onMessageReactionRemove (const dpp::message_reaction_remove_t &event);
void onMessageReactionRemoveAll (const dpp::message_reaction_remove_all_t &event);
void onUserUpdate (const dpp::user_update_t &event);
void onResumed (const dpp::resumed_t &event);
void onChannelUpdate (const dpp::channel_update_t &event);
void onChannelPinsUpdate (const dpp::channel_pins_update_t &event);
void onGuildBanAdd (const dpp::guild_ban_add_t &event);
void onGuildBanRemove (const dpp::guild_ban_remove_t &event);
void onGuildEmojisUpdate (const dpp::guild_emojis_update_t &event);
void onGuildIntegrationsUpdate (const dpp::guild_integrations_update_t &event);
void onGuildMemberRemove (const dpp::guild_member_remove_t &event);
void onGuildMemberUpdate (const dpp::guild_member_update_t &event);
void onGuildMembersChunk (const dpp::guild_members_chunk_t &event);
void onGuildRoleCreate (const dpp::guild_role_create_t &event);
void onGuildRoleUpdate (const dpp::guild_role_update_t &event);
void onGuildRoleDelete (const dpp::guild_role_delete_t &event);
void onPresenceUpdate (const dpp::presence_update_t &event);
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);
Expand Down
57 changes: 3 additions & 54 deletions include/sporks/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,13 @@ enum Implementation
I_BEGIN,
I_OnMessage,
I_OnReady,
I_OnChannelCreate,
I_OnChannelDelete,
I_OnGuildMemberAdd,
I_OnGuildCreate,
I_OnGuildDelete,
I_OnPresenceUpdate,
I_OnRestEnd,
I_OnAllShardsReady,
I_OnTypingStart,
I_OnMessageUpdate,
I_OnMessageDelete,
I_OnMessageDeleteBulk,
I_OnGuildUpdate,
I_OnMessageReactionAdd,
I_OnMessageReactionRemove,
I_OnMessageReactionRemoveAll,
I_OnUserUpdate,
I_OnResumed,
I_OnChannelUpdate,
I_OnChannelPinsUpdate,
I_OnGuildBanAdd,
I_OnGuildBanRemove,
I_OnGuildEmojisUpdate,
I_OnGuildIntegrationsUpdate,
I_OnGuildMemberRemove,
I_OnGuildMemberUpdate,
I_OnGuildMembersChunk,
I_OnGuildRoleCreate,
I_OnGuildRoleUpdate,
I_OnGuildRoleDelete,
I_OnPresenceUpdateWS,
I_OnVoiceStateUpdate,
I_OnVoiceServerUpdate,
I_OnWebhooksUpdate,
I_OnEntitlementCreate,
I_OnEntitlementUpdate,
Expand Down Expand Up @@ -129,7 +103,7 @@ class ModuleLoader {
std::map<std::string, ModuleNative> Modules;

/* Retrieve a named symbol from a shared object file */
bool GetSymbol(ModuleNative &native, const char *sym_name);
static bool GetSymbol(ModuleNative &native, const char *sym_name);

/* The module list as external systems see it, a map of string filenames to
* module objects
Expand All @@ -144,7 +118,7 @@ class ModuleLoader {
/* An array of vectors indicating which modules are watching which events */
std::vector<Module*> EventHandlers[I_END];

ModuleLoader(Bot* creator);
explicit ModuleLoader(Bot* creator);
virtual ~ModuleLoader();

/* Attach a module to an event. Only events a module explicitly attaches to will be
Expand Down Expand Up @@ -175,7 +149,7 @@ class ModuleLoader {
void LoadAll();

/* Get a list of all loaded modules */
const ModMap& GetModuleList() const;
[[nodiscard]] const ModMap& GetModuleList() const;

const std::string& GetLastError();
};
Expand All @@ -200,40 +174,15 @@ class Module {
virtual std::string GetDescription();

/* D++ events */
virtual bool OnChannelCreate(const dpp::channel_create_t &channel);
virtual bool OnReady(const dpp::ready_t &ready);
virtual bool OnChannelDelete(const dpp::channel_delete_t &channel);
virtual bool OnGuildCreate(const dpp::guild_create_t &guild);
virtual bool OnGuildDelete(const dpp::guild_delete_t &guild);
virtual bool OnGuildMemberAdd(const dpp::guild_member_add_t &gma);
virtual bool OnMessage(const dpp::message_create_t &message, const std::string& clean_message, bool mentioned, const std::vector<std::string> &stringmentions);
virtual bool OnPresenceUpdate();
virtual bool OnAllShardsReady();
virtual bool OnTypingStart(const dpp::typing_start_t &obj);
virtual bool OnMessageUpdate(const dpp::message_update_t &obj);
virtual bool OnMessageDelete(const dpp::message_delete_t &obj);
virtual bool OnMessageDeleteBulk(const dpp::message_delete_bulk_t &obj);
virtual bool OnGuildUpdate(const dpp::guild_update_t &obj);
virtual bool OnMessageReactionAdd(const dpp::message_reaction_add_t &obj);
virtual bool OnMessageReactionRemove(const dpp::message_reaction_remove_t &obj);
virtual bool OnMessageReactionRemoveAll(const dpp::message_reaction_remove_all_t &obj);
virtual bool OnUserUpdate(const dpp::user_update_t &obj);
virtual bool OnResumed(const dpp::resumed_t &obj);
virtual bool OnChannelUpdate(const dpp::channel_update_t &obj);
virtual bool OnChannelPinsUpdate(const dpp::channel_pins_update_t &obj);
virtual bool OnGuildBanAdd(const dpp::guild_ban_add_t &obj);
virtual bool OnGuildBanRemove(const dpp::guild_ban_remove_t &obj);
virtual bool OnGuildEmojisUpdate(const dpp::guild_emojis_update_t &obj);
virtual bool OnGuildIntegrationsUpdate(const dpp::guild_integrations_update_t &obj);
virtual bool OnGuildMemberRemove(const dpp::guild_member_remove_t &obj);
virtual bool OnGuildMemberUpdate(const dpp::guild_member_update_t &obj);
virtual bool OnGuildMembersChunk(const dpp::guild_members_chunk_t &obj);
virtual bool OnGuildRoleCreate(const dpp::guild_role_create_t &obj);
virtual bool OnGuildRoleUpdate(const dpp::guild_role_update_t &obj);
virtual bool OnGuildRoleDelete(const dpp::guild_role_delete_t &obj);
virtual bool OnPresenceUpdateWS(const dpp::presence_update_t &obj);
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);
Expand Down
4 changes: 2 additions & 2 deletions modules/bandwidth/bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class BandwidthModule : public Module
if (half % 2 == 0) {
uint64_t bandwidth_websocket = 0;
auto& shards = bot->core->get_shards();
for (auto i = shards.begin(); i != shards.end(); ++i) {
dpp::discord_client* shard = i->second;
for (auto i : shards) {
dpp::discord_client* shard = i.second;
bandwidth_websocket += shard->get_bytes_in() + shard->get_bytes_out();
}
uint64_t bandwidth_last_60_seconds = bandwidth_websocket - last_bandwidth_websocket;
Expand Down
25 changes: 5 additions & 20 deletions modules/diagnostics/diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,17 @@ std::string exec(const char* cmd) {

class DiagnosticsModule : public Module
{
PCRE* diagnosticmessage;
double microseconds_ping;
PCRE* diagnostic_message;
public:
DiagnosticsModule(Bot* instigator, ModuleLoader* ml) : Module(instigator, ml)
{
ml->Attach({ I_OnMessage, I_OnRestEnd }, this);
diagnosticmessage = new PCRE("^sudo(\\s+(.+?))$", true);
ml->Attach({ I_OnMessage }, this);
diagnostic_message = new PCRE("^sudo(\\s+(.+?))$", true);
}

virtual ~DiagnosticsModule()
{
delete diagnosticmessage;
delete diagnostic_message;
}

virtual std::string GetVersion()
Expand All @@ -95,17 +94,11 @@ class DiagnosticsModule : public Module
return "Diagnostic Commands (sudo), '@Sporks sudo'";
}

virtual bool OnRestEnd(std::chrono::steady_clock::time_point start_time, uint16_t code)
{
microseconds_ping = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start_time).count();
return true;
}

virtual bool OnMessage(const dpp::message_create_t &message, const std::string& clean_message, bool mentioned, const std::vector<std::string> &stringmentions)
{
std::vector<std::string> param;

if (mentioned && diagnosticmessage->Match(clean_message, param) && param.size() >= 3) {
if (mentioned && diagnostic_message->Match(clean_message, param) && param.size() >= 3) {

dpp::message msg = message.msg;
std::stringstream tokens(trim(param[2]));
Expand Down Expand Up @@ -276,14 +269,6 @@ class DiagnosticsModule : public Module
::sleep(5);
/* Note: exit here will restart, because we run the bot via run.sh which restarts the bot on quit. */
exit(0);
} else if (lowercase(subcommand) == "ping") {
std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
bot->core->message_create(dpp::message(msg.channel_id, "Pinging..."), [msg, this, start_time](const dpp::confirmation_callback_t & state) {
double microseconds_ping = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start_time).count();
dpp::snowflake mid = (std::get<dpp::message>(state.value)).id;
this->bot->core->message_delete(mid, msg.channel_id);
this->EmbedSimple(fmt::format("**Pong!** REST Response time: {:.3f} ms", microseconds_ping / 1000, 4), msg.channel_id, msg.guild_id);
});
} else if (lowercase(subcommand) == "lookup") {
int64_t gnum = 0;
tokens >> gnum;
Expand Down
10 changes: 5 additions & 5 deletions modules/presence/presence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

class PresenceModule : public Module
{
uint64_t halfminutes{};
uint64_t half_minutes{};

public:
PresenceModule(Bot* instigator, ModuleLoader* ml) : Module(instigator, ml), halfminutes(0)
PresenceModule(Bot* instigator, ModuleLoader* ml) : Module(instigator, ml), half_minutes(0)
{
ml->Attach({ I_OnPresenceUpdate, I_OnGuildCreate, I_OnGuildDelete, I_OnGuildUpdate }, this);
}
Expand Down Expand Up @@ -114,9 +114,9 @@ class PresenceModule : public Module
0, bot->sent_messages, bot->received_messages, ram, games
}
);
if (++halfminutes > 20) {
/* Reset counters every 10 mins. Chewey stats uses these counters and expects this */
halfminutes = bot->sent_messages = bot->received_messages = 0;
if (++half_minutes > 20) {
/* Reset counters every 10 minutes. Chewey stats uses these counters and expects this */
half_minutes = bot->sent_messages = bot->received_messages = 0;
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/trivia/cmd_context_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct in_flight {
/**
* @brief Active menu states per user
*/
std::unordered_map<dpp::snowflake, in_flight> active_menus;
std::map<dpp::snowflake, in_flight> active_menus;

/**
* @brief Find active menu state
Expand Down
20 changes: 10 additions & 10 deletions modules/trivia/cmd_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ void command_info_t::call(const in_cmd &cmd, std::stringstream &tokens, guild_se
std::stringstream s;
dpp::utility::uptime ut = creator->GetBot()->core->uptime();

int64_t servers = creator->GetGuildTotal();
int64_t users = creator->GetMemberTotal();
uint64_t servers = creator->GetGuildTotal();
uint64_t users = creator->GetMemberTotal();

char startstr[256];
tm _tm;
Expand All @@ -66,7 +66,7 @@ void command_info_t::call(const in_cmd &cmd, std::stringstream &tokens, guild_se

uint64_t shard = (cmd.guild_id >> 22) % from_string<uint32_t>(Bot::GetConfig("shardcount"), std::dec);

const statusfield statusfields[] = {
const statusfield status_fields[] = {
statusfield(_("ACTIVEGAMES", settings), Comma(creator->GetActiveGames())),
statusfield(_("TOTALSERVERS", settings), Comma(servers)),
statusfield(_("CONNSINCE", settings), startstr),
Expand All @@ -85,13 +85,13 @@ void command_info_t::call(const in_cmd &cmd, std::stringstream &tokens, guild_se
statusfield("", "")
};

s << "{\"title\":\"" << creator->GetBot()->user.username << " " << _("INFO", settings);
s << "\",\"thumbnail\":{\"url\":\"https:\\/\\/triviabot.co.uk\\/images\\/triviabot_tl_icon.png\"},";
s << "\"color\":" << settings.embedcolour << ",\"url\":\"https:\\/\\/triviabot.co.uk\\/\\/\",";
s << "\"footer\":{\"link\":\"https:\\/\\/triviabot.co.uk\\/\",\"text\":\"" << _("POWERED_BY", settings) << "\",\"icon_url\":\"https:\\/\\/triviabot.co.uk\\/images\\/triviabot_tl_icon.png\"},\"fields\":[";
for (int i = 0; statusfields[i].name != ""; ++i) {
s << "{\"name\":\"" << statusfields[i].name << "\",\"value\":\"" << statusfields[i].value << "\", \"inline\": " << (i != 14 ? "true" : "false") << "}";
if (statusfields[i + 1].name != "") {
s << R"({"title":")" << creator->GetBot()->user.username << " " << _("INFO", settings);
s << R"(","thumbnail":{"url":"https:\/\/triviabot.co.uk\/images\/triviabot_tl_icon.png"},)";
s << "\"color\":" << settings.embedcolour << R"(,"url":"https:\/\/triviabot.co.uk\/\/",)";
s << R"("footer":{"link":"https:\/\/triviabot.co.uk\/","text":")" << _("POWERED_BY", settings) << R"(","icon_url":"https:\/\/triviabot.co.uk\/images\/triviabot_tl_icon.png"},"fields":[)";
for (int i = 0; status_fields[i].name != ""; ++i) {
s << "{\"name\":\"" << status_fields[i].name << R"(","value":")" << status_fields[i].value << R"(", "inline": )" << (i != 14 ? "true" : "false") << "}";
if (status_fields[i + 1].name != "") {
s << ",";
}
}
Expand Down
3 changes: 3 additions & 0 deletions modules/trivia/entitlement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bool TriviaModule::OnEntitlementCreate(const dpp::entitlement_create_t& entitlem
"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 }
);
db::query("UPDATE bot_guild_settings SET premium = 1 WHERE snowflake_id = ?", { entitlement.created.guild_id });
return true;
}

Expand All @@ -44,6 +45,7 @@ bool TriviaModule::OnEntitlementUpdate(const dpp::entitlement_update_t& entitlem
entitlement.updating_entitlement.subscription_id
}
);
db::query("UPDATE bot_guild_settings SET premium = ? WHERE snowflake_id = ?", { entitlement.updating_entitlement.is_deleted() || entitlement.updating_entitlement.ends_at < time(nullptr) ? 0 : 1, entitlement.updating_entitlement.guild_id });
return true;
}

Expand All @@ -53,6 +55,7 @@ bool TriviaModule::OnEntitlementDelete(const dpp::entitlement_delete_t& entitlem
"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 }
);
db::query("UPDATE bot_guild_settings SET premium = 0 WHERE snowflake_id = ?", { entitlement.deleted.guild_id });
return true;
}

5 changes: 0 additions & 5 deletions modules/trivia/guildqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,3 @@
*
************************************************************************************/

#include "trivia.h"

void TriviaModule::ProcessGuildQueue()
{
}
2 changes: 1 addition & 1 deletion modules/trivia/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "piglatin.h"
#include "time.h"

std::unordered_map<uint64_t, bool> banlist;
std::map<uint64_t, bool> banlist;

in_msg::in_msg(const std::string &m, uint64_t author, bool mention, const std::string &_username, dpp::user u, dpp::guild_member gm) : msg(m), author_id(author), mentions_bot(mention), username(_username), user(u), member(gm)
{
Expand Down
4 changes: 2 additions & 2 deletions modules/trivia/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ class state_t
bool hintless;
std::map<std::string, bool> insane;
std::map<uint64_t, time_t> activity;
std::unordered_map<dpp::snowflake, uint64_t> scores;
std::unordered_map<dpp::snowflake, uint32_t> insane_round_stats;
std::map<dpp::snowflake, uint64_t> scores;
std::map<dpp::snowflake, uint32_t> insane_round_stats;
std::vector<question_t> question_cache;

state_t(const state_t &) = default;
Expand Down
Loading

0 comments on commit 7738b3e

Please sign in to comment.