1
0
Fork 0
mirror of https://gitlab.com/niansa/dpplogger.git synced 2025-03-06 20:48:29 +01:00
dpplogger/main.cpp
2023-01-08 21:40:43 +01:00

472 lines
19 KiB
C++

#include "Random.hpp"
#include "sqlite_modern_cpp/sqlite_modern_cpp.h"
#include <iostream>
#include <unordered_map>
#include <optional>
#include <chrono>
#include <memory>
#include <stdexcept>
#include <boost/asio/io_service.hpp>
#include <dcboost/discord.hpp>
#include <dcboost/snowflake.hpp>
using namespace ChatFuse;
using namespace QLog;
class Cache {
Logger<Cache> logger;
std::unordered_map<Discord::Snowflake, Json::Value> cache;
public:
struct StoreRes {
const Json::Value& data;
enum {
unchanged = 0b00,
updated = 0b01,
created = 0b11,
};
uint8_t changed;
};
struct Miss : public std::runtime_error {
Miss() : std::runtime_error("Object cache miss") {}
};
Cache() {
logger.inst_ptr = this;
}
StoreRes store(Discord::Snowflake id, const Json::Value& object) {
logger.log(Loglevel::verbose, "Stored "+id.str()+" in cache");
auto cache_hit = cache.find(id);
if (cache_hit != cache.end()) {
bool changed = cache_hit->second != object;
if (changed) {
cache_hit->second = object;
}
return {cache_hit->second, changed};
} else {
return {cache.emplace(id, object).first->second, StoreRes::created};
}
}
StoreRes store(Discord::Snowflake id, Json::Value&& object) {
logger.log(Loglevel::verbose, "Moved "+id.str()+" into cache");
auto cache_hit = cache.find(id);
if (cache_hit != cache.end()) {
bool changed = cache_hit->second != object;
if (changed) {
cache_hit->second = std::move(object);
}
return {cache_hit->second, changed};
} else {
return {cache.emplace(id, std::move(object)).first->second, true};
}
}
StoreRes store(const Json::Value& object) {
auto id = object["id"].asString();
return store(id, object);
}
StoreRes store(Json::Value&& object) {
auto id = object["id"].asString();
return store(id, std::move(object));
}
const Json::Value& fetch(Discord::Snowflake id) const {
const auto& entry = cache.find(id);
if (entry == cache.end()) {
throw Miss();
}
return entry->second;
}
const Json::Value& fetch(const std::string& id) const {
return fetch(Discord::Snowflake(id));
}
bool has(Discord::Snowflake id) const {
return cache.find(id) != cache.end();
}
};
static inline std::optional<std::string> GetJSONAsOptionalString(const Json::Value& data) {
return data.isString()?data.asString():std::optional<std::string>();
}
static inline std::optional<int> GetJSONAsOptionalInt(const Json::Value& data) {
return data.isString()?data.asInt():std::optional<int>();
}
class MyClient final : public Discord::Client {
sqlite::database db;
Cache cache;
Json::Value *me;
public:
MyClient(boost::asio::io_service& io, const ChatFuse::Discord::Settings& settings = {})
: ChatFuse::Discord::Client(io, settings),
db("log.sqlite3")
{
// Improve database performance
db << "pragma journal_mode = WAL;";
db << "pragma synchronous = normal;";
db << "pragma temp_store = memory;";
// Create tables
{
db << "CREATE TABLE IF NOT EXISTS messages ("
" id TEXT PRIMARY KEY NOT NULL,"
" type INTEGER NOT NULL,"
" channel_id TEXT NOT NULL,"
" author_id TEXT NOT NULL,"
" replied_to_id TEXT,"
" is_deleted INTEGER DEFAULT 0 NOT NULL,"
" is_edited INTEGER DEFAULT 0 NOT NULL,"
" creation_timestamp TEXT NOT NULL,"
" update_timestamp TEXT DEFAULT '0' NOT NULL,"
" deletion_timestamp TEXT DEFAULT '0' NOT NULL,"
" UNIQUE(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,"
" embeds TEXT" // Discord JSON embed object array
");";
db << "CREATE TABLE IF NOT EXISTS users ("
" id TEXT NOT NULL,"
" is_initial INTEGER DEFAULT 1 NOT NULL,"
" timestamp TEXT NOT NULL,"
" full_username TEXT NOT NULL," // Username#Discriminator
" avatar TEXT,"
" bio TEXT,"
" has_nitro INTEGER NOT NULL,"
" is_bot INTEGER NOT NULL"
");";
db << "CREATE TABLE IF NOT EXISTS user_statuses ("
" user_id TEXT NOT NULL,"
" is_initial INTEGER DEFAULT 1 NOT NULL,"
" timestamp TEXT NOT NULL,"
" status TEXT," // JSON array or null if not updated: [int:online/dnd/afk/offline enum, ?:emoji (null/empty if none), string:text (empty if none)]
" presence TEXT" // Discord JSON presence object
");";
db << "CREATE TABLE IF NOT EXISTS members ("
" user_id TEXT NOT NULL,"
" guild_id TEXT NOT NULL,"
" is_initial INTEGER DEFAULT 1 NOT NULL,"
" timestamp TEXT NOT NULL,"
" nickname TEXT,"
" avatar TEXT,"
" in_guild INTEGER NOT NULL"
");";
db << "CREATE TABLE IF NOT EXISTS member_voice_connections ("
" channel_id TEXT NOT NULL,"
" user_id TEXT NOT NULL,"
" is_initial INTEGER DEFAULT 1 NOT NULL,"
" timestamp TEXT NOT NULL,"
" voice_channel_id TEXT,"
" self_muted INTEGER NOT NULL,"
" self_deafened INTEGER NOT NULL,"
" server_muted INTEGER NOT NULL,"
" server_deafened INTEGER NOT NULL"
");";
db << "CREATE TABLE IF NOT EXISTS channels ("
" id TEXT NOT NULL,"
" guild_id TEXT,"
" parent_id TEXT,"
" timestamp TEXT NOT NULL,"
" is_initial INTEGER DEFAULT 1 NOT NULL,"
" type INTEGER NOT NULL,"
" name TEXT,"
" topic TEXT,"
" is_deleted INTEGER DEFAULT 0 NOT NULL"
");";
db << "CREATE TABLE IF NOT EXISTS guilds ("
" id TEXT NOT NULL,"
" timestamp TEXT NOT NULL,"
" is_initial INTEGER DEFAULT 1 NOT NULL,"
" name TEXT NOT NULL,"
" owner_user_id TEXT NOT NULL,"
" in_guild INTEGER DEFAULT 1 NOT NULL"
");";
}
}
protected:
/*
* Note: On creation, update, and deletion, it is recommended to use
* the functions starting with 'insert' as they do not store any
* data in the cache. For creation, it is also recommended to
* use the functions starting with 'process' as they not only store
* data in the cache, but also automatically handle related members
* (e.g. channels in a guild). The functions starting with 'update'
* are preferred for both creation and update and store data in cache
* as well. When accessing the cache, it is recommended to use the
* functions starting with 'cache' rather than accessing it directly.
*/
/*
* Guilds
*/
boost::asio::awaitable<void> fetchAllGuilds(Json::Value&& unavailable_guild_array) {
RandomGenerator rng;
rng.seed();
// Fetch guilds and channels from API
for (auto& incomplete_data : unavailable_guild_array) {
Discord::Snowflake guild_id = incomplete_data["id"];
// Use cached guild if possible
if (cache.has(guild_id)) continue; // In this case, we skip processing the guild,
// since it's safe to assume it's already been cached
// Make sure data is actually incomplete
if (incomplete_data["name"].isString() && incomplete_data["channels"].isArray()) {
// It's not, so we'll just use it as-is
processGuild(std::move(incomplete_data));
continue;
}
// Fetch guild from API and process it
processGuild(co_await api.call(boost::beast::http::verb::get, "/guilds/"+guild_id.str()));
// Delay
co_await asyncSleep(rng.getUInt(6000, 15000));
}
}
Cache::StoreRes processGuild(Json::Value&& data) {
const auto& id = data["id"];
// Insert into database
insertGuildUpdate(data, !cache.has(id));
// Insert and store all channels
for (auto& channel_data : data["channels"]) {
// Make sure we haven't cached this channel yet
if (cache.has(channel_data["id"])) continue;
// Add guild_id to channel (it'll be missing)
channel_data["guild_id"] = id;
// Insert into database
insertChannelUpdate(channel_data, true);
// Store in cache
cache.store(std::move(channel_data));
}
// Process all members
for (auto& member_data : data["members"]) {
updateMember(std::move(member_data), id);
}
// Remove channels and members
data.removeMember("channels");
data.removeMember("members");
// Store in cache and return cached guild
return cache.store(std::move(data));
}
void insertGuildUpdate(const Json::Value& data, bool is_initial, bool is_deleted = false) {
db << "INSERT INTO guilds (id, timestamp, is_initial, name, owner_user_id, in_guild)"
" VALUES (?, ?, ?, ?, ?, ? );"
<< data["id"].asString() << std::to_string(time(nullptr)) << is_initial << data["name"].asString()
<< data["owner_id"].asString() << !is_deleted;
}
void insertGuildDelete(const Json::Value& data) {
auto cached_data = cache.fetch(data["id"]);
insertGuildUpdate(cached_data, false, true);
}
/*
* Members
*/
void updateMember(Json::Value&& data, Discord::Snowflake guild_id, Discord::Snowflake user_id = {}) {
if (user_id.empty()) {
// Get user
const auto& user_data = data["user"];
if (!user_data.isObject()) {
logger.log(Loglevel::error, "A processMember call has been aborted because the passed 'data' object doesn't contain a 'user' key.");
return;
}
user_id = user_data["id"].asString();
// Insert and cache user
auto changed = cache.store(user_data).changed;
if (changed) {
insertUserUpdate(user_data, (changed&Cache::StoreRes::created)?true:false);
}
}
// Cache member
auto changed = cacheStoreMember(std::move(data), user_id, guild_id).changed;
// Insert member
if (changed) {
insertMemberUpdate(data, user_id, guild_id, !cache.has(data["id"]));
}
}
Cache::StoreRes cacheStoreMember(const Json::Value& data, Discord::Snowflake user_id, Discord::Snowflake guild_id) {
return cache.store(guild_id.uint()|user_id.uint(), data);
}
const Json::Value& fetchFetchMember(Discord::Snowflake user_id, Discord::Snowflake guild_id) {
return cache.fetch(guild_id.uint()|user_id.uint());
}
void insertMemberUpdate(const Json::Value& data, Discord::Snowflake user_id, Discord::Snowflake guild_id, bool is_initial, bool is_removed = false) {
db << "INSERT INTO members (user_id, guild_id, is_initial, timestamp, nickname, avatar, in_guild)"
" VALUES (?, ?, ?, ?, ?, ?, ? );"
<< user_id.str() << guild_id.str() << is_initial << std::to_string(time(nullptr)) << GetJSONAsOptionalString(data["nick"]) << GetJSONAsOptionalString(data["avatar"]) << !is_removed;
}
void insertMemberDelete(Discord::Snowflake user_id, Discord::Snowflake guild_id) {
insertMemberUpdate(fetchFetchMember(user_id, guild_id), user_id, guild_id, false, true);
}
/*
* Users
*/
void insertUserUpdate(const Json::Value& data, bool is_initial) {
db << "INSERT INTO users (id, is_initial, timestamp, full_username, avatar, bio, has_nitro, is_bot)"
" VALUES (?, ?, ?, ?, ?, ?, ?, ? );"
<< data["id"].asString() << is_initial << std::to_string(time(nullptr))
<< data["username"].asString()+'#'+data["discriminator"].asString() << GetJSONAsOptionalString(data["avatar"])
<< GetJSONAsOptionalString(data["bio"]) << GetJSONAsOptionalInt(data["has_nitro"]).value_or(0)
<< GetJSONAsOptionalInt(data["bot"]).value_or(false);
}
/*
* Messages
*/
void processMessage(Json::Value&& data) {
const auto& guild_id = data["guild_id"];
const auto& user = data["author"];
// Process member
auto& member_data = data["member"];
if (member_data.isObject() && guild_id.isString()) {
updateMember(std::move(member_data), guild_id, user["id"]);
}
// Insert message into database
insertMessageContent(data, true);
}
void insertMessageContent(const Json::Value& data, bool is_initial) {
const auto& embeds = data["embeds"];
db << "INSERT INTO message_contents (message_id, is_initial, timestamp, content, embeds)"
" VALUES (?, ?, ?, ?, ? );"
<< data["id"].asString() << is_initial << std::to_string(time(nullptr)) << data["content"].asString()
<< (embeds.isArray()?Json::writeString(Json::StreamWriterBuilder(), embeds):std::optional<std::string>());
}
void insertMessageUpdate(const Json::Value& data, bool is_initial) {
bool has_content = !data["content"].asString().empty() || data["embeds"].isArray();
if (is_initial) {
const auto& author = data["author"];
int type = data["type"].asInt();
db << "INSERT INTO messages (id, type, channel_id, author_id, replied_to_id, creation_timestamp)"
" VALUES (?, ?, ?, ?, ?, ? );"
<< data["id"].asString() << type << data["channel_id"].asString() << author["id"].asString()
<< (type==19?data["referenced_message"]["id"].asString():std::optional<std::string>()) << std::to_string(time(nullptr));
cache.store(author);
if (has_content) insertMessageContent(data, true);
} else {
db << "UPDATE messages "
"SET is_edited = 1, update_timestamp = ? "
"WHERE id = ?;"
<< std::to_string(time(nullptr)) << data["id"].asString();
if (has_content) insertMessageContent(data, false);
}
}
void insertMessageDelete(const Json::Value& data) {
db << "UPDATE messages "
"SET is_deleted = 1, deletion_timestamp = ? "
"WHERE id = ?;"
<< std::to_string(time(nullptr)) << data["id"].asString();
}
/*
* Channels
*/
void insertChannelUpdate(const Json::Value& data, bool is_initial, bool is_deleted = false) {
db << "INSERT INTO channels (id, guild_id, parent_id, timestamp, is_initial, type, name, topic, is_deleted)"
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"
<< data["id"].asString() << GetJSONAsOptionalString(data["guild_id"])<< GetJSONAsOptionalString(data["parent_id"])
<< std::to_string(time(nullptr)) << is_initial << data["type"].asInt() << GetJSONAsOptionalString(data["name"])
<< GetJSONAsOptionalString(data["topic"]) << is_deleted;
}
void insertChannelDelete(const Json::Value& data) {
auto cached_data = cache.fetch(data["id"]);
insertChannelUpdate(cached_data, false, true);
}
/*
* Intent handler
*/
virtual boost::asio::awaitable<void> intentHandler(std::string intent, Json::Value data) override {
if (intent == "READY") [[unlikely]] {
const auto& user = cache.store(std::move(data["user"])).data;
logger.log(Loglevel::info, "Connected to Discord as: "+user["username"].asString()+'#'+user["discriminator"].asString());
settings.is_bot = GetJSONAsOptionalInt(user["bot"]).value_or(false);
co_await fetchAllGuilds(std::move(data["guilds"]));
}
else if (intent == "GUILD_CREATE") [[unlikely]] {
processGuild(std::move(data));
} else if (intent == "GUILD_UPDATE") [[unlikely]] {
insertGuildUpdate(data, false);
cache.store(std::move(data));
} else if (intent == "GUILD_DELETE") [[unlikely]] {
insertGuildDelete(data);
}
else if (intent == "MESSAGE_CREATE") [[likely]] {
insertMessageUpdate(data, true);
} else if (intent == "MESSAGE_UPDATE") [[likely]] {
insertMessageUpdate(data, false);
} else if (intent == "MESSAGE_DELETE") {
insertMessageDelete(data);
}
else if (intent == "CHANNEL_CREATE") [[unlikely]] {
insertChannelUpdate(data, true);
cache.store(std::move(data));
} else if (intent == "CHANNEL_UPDATE") [[unlikely]] {
insertChannelUpdate(data, false);
cache.store(std::move(data));
} else if (intent == "CHANNEL_DELETE") [[unlikely]] {
insertChannelDelete(data);
}
else if (intent == "GUILD_MEMBER_ADD" || intent == "GUILD_MEMBER_UDATE") [[unlikely]] {
auto guild_id = std::move(data["guild_id"]);
updateMember(std::move(data), guild_id.asString());
} else if (intent == "GUILD_MEMBER_REMOVE") [[unlikely]] {
const auto& user_data = cache.store(data["user"]).data;
insertMemberDelete(user_data["id"], data["guild_id"]);
}
co_return;
}
};
int main(int argc, char **argv) {
// Check args
if (argc == 1) {
std::cout << "Usage: " << argv[0] << " <token>" << std::endl;
return EXIT_FAILURE;
}
// Create io service
boost::asio::io_service io;
// Set up client
auto client = std::make_shared<MyClient>(io, ChatFuse::Discord::Settings{
.bot_token = argv[1],
.intents = ChatFuse::Discord::intents::all
});
client->detach();
// Erase bot token from argv (so it's no longer visible in /proc/{pid}/cmdline)
memset(argv[1], 0, strlen(argv[1]));
// Run!!!
io.run();
return EXIT_SUCCESS;
}