1
0
Fork 0
mirror of https://gitlab.com/niansa/dpplogger.git synced 2025-03-06 20:48:29 +01:00

Started implementing callbacks

This commit is contained in:
niansa 2023-01-07 03:01:08 +01:00
parent a47c1fe551
commit ad0f2f3b76

View file

@ -1,11 +1,34 @@
#include "sqlite_modern_cpp/sqlite_modern_cpp.h"
#include <iostream>
#include <unordered_map>
#include <variant>
#include <chrono>
#include <memory>
#include <dpp/dpp.h>
class Cache {
using Object = std::variant<std::monostate, dpp::guild, dpp::user, dpp::guild_member>;
std::unordered_map<dpp::snowflake, Object> 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<dpp::guild>(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::string>(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);
});
}