diff --git a/main.cpp b/main.cpp index daad8e7..5dee349 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,34 @@ #include "sqlite_modern_cpp/sqlite_modern_cpp.h" #include +#include +#include +#include +#include #include +class Cache { + using Object = std::variant; + std::unordered_map cache; + +public: + void store(dpp::snowflake id, const Object& object) { + cache[id] = object; + } + const Object& fetch(dpp::snowflake id) const { + auto entry = cache.find(id); + if (entry == cache.end()) { + return std::monostate(); + } + return entry->second; + } +}; + int main() { + Cache cache; + // Initialize Sqlite3 sqlite::database db("log.sqlite3"); @@ -82,8 +105,8 @@ int main() { " timestamp TEXT NOT NULL," " is_initial INTEGER DEFAULT 1 NOT NULL," " name TEXT NOT NULL," - " icon TEXT," - " owner_user_id TEXT NOT NULL" + " owner_user_id TEXT NOT NULL," + " in_guild INTEGER DEFAULT 1 NOT NULL" ");"; } @@ -93,10 +116,43 @@ int main() { // Set up callbacks // Useful: https://discord.com/developers/docs/topics/gateway-events cluster.on_guild_create([&](const dpp::guild_create_t& event) { - + const auto guild = event.created; + db << "INSERT OR IGNORE INTO levels (id, timestamp, name, owner_user_id)" + " VALUES (?, ?, ?, ? );" + << std::to_string(guild->id) << std::to_string(time(nullptr)) << guild->name << std::to_string(guild->owner_id); + cache.store(guild->id, *guild); + }); + cluster.on_guild_update([&](const dpp::guild_update_t& event) { + const auto guild = event.updated; + db << "INSERT OR IGNORE INTO levels (id, timestamp, is_initial, name, owner_user_id)" + " VALUES (?, ?, 0, ?, ? );" + << std::to_string(guild->id) << std::to_string(time(nullptr)) << guild->name << std::to_string(guild->owner_id); + cache.store(guild->id, *guild); + }); + cluster.on_guild_delete([&](const dpp::guild_delete_t& event) { + const auto& guild = std::get(cache.fetch(event.deleted->id)); + db << "INSERT OR IGNORE INTO levels (id, timestamp, is_initial, name, owner_user_id)" + " VALUES (?, ?, 0, ?, ? );" + << std::to_string(guild.id) << std::to_string(time(nullptr)) << guild.name << std::to_string(guild.owner_id); }); + db << "CREATE TABLE IF NOT EXISTS message_contents (" + " message_id TEXT NOT NULL," + " is_initial INTEGER DEFAULT 1 NOT NULL," + " timestamp TEXT NOT NULL," + " content TEXT NOT NULL" + ");"; + auto on_message_content = [&](const dpp::message& msg, bool is_initial) { + db << "INSERT OR IGNORE INTO message_contents (message_id, is_initial, timestamp, content)" + " VALUES (?, ?, ?, ? );" + << std::to_string(msg.id) << is_initial << time(nullptr) << msg.content; + }; cluster.on_message_create([&](const dpp::message_create_t& event) { - + const auto msg = event.msg; + db << "INSERT OR IGNORE INTO messages (id, type, channel_id, author_id, replied_to_id, creation_timestamp)" + " VALUES (?, ?, ?, ?, ?, ? );" + << std::to_string(msg.id) << std::to_string(msg.type) << std::to_string(msg.channel_id) << std::to_string(msg.author.id) << (msg.message_reference.message_id?std::optional(std::to_string(msg.message_reference.message_id)):nullptr) << time(nullptr); + if (!msg.content.empty()) on_message_content(msg, true); + cache.store(msg.author.id, msg.author); }); }