From 3d69dba0e6f1b08a13546cdee615d70b0590720a Mon Sep 17 00:00:00 2001 From: Will Brown Date: Tue, 26 Nov 2024 01:46:22 -0500 Subject: [PATCH] Remove all feeds associated with a guild when the bot leaves --- feed_info.c | 23 +++++++++++++++++++++++ feed_info.h | 3 +++ main.c | 8 ++++++++ 3 files changed, 34 insertions(+) diff --git a/feed_info.c b/feed_info.c index 274c818..a6ed0ba 100644 --- a/feed_info.c +++ b/feed_info.c @@ -195,6 +195,29 @@ zblock_feed_info_err zblock_feed_info_delete(PGconn *conn, const char *url, u64s return result; } +// deletes all feeds associated with a guild from the database +zblock_feed_info_err zblock_feed_info_delete_all_guild(PGconn *conn, u64snowflake guild_id) { + if (!conn) return ZBLOCK_FEED_INFO_INVALID_ARGS; + + uint64_t guild_id_be = htobe64(guild_id); + const char *const params[] = {(char *) &guild_id_be}; + const int param_lengths[] = {sizeof(guild_id_be)}; + const int param_formats[] = {1}; + PGresult *res = PQexecParams(conn, + "DELETE FROM feeds WHERE guild_id = $1::bigint", + 1, NULL, params, param_lengths, param_formats, 1 + ); + + zblock_feed_info_err result = ZBLOCK_FEED_INFO_OK; + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + log_error(PQresultErrorMessage(res)); + result = ZBLOCK_FEED_INFO_DBERROR; + } + + PQclear(res); + return result; +} + // updates the last_pubDate field of a given feed in the database zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_minimal *feed) { if (!conn || !feed) return ZBLOCK_FEED_INFO_INVALID_ARGS; diff --git a/feed_info.h b/feed_info.h index 859e7e5..6712b65 100644 --- a/feed_info.h +++ b/feed_info.h @@ -60,6 +60,9 @@ zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *fee // deletes feed from the database zblock_feed_info_err zblock_feed_info_delete(PGconn *conn, const char *url, u64snowflake channel_id); +// deletes all feeds associated with a guild from the database +zblock_feed_info_err zblock_feed_info_delete_all_guild(PGconn *conn, u64snowflake guild_id); + // updates the last_pubDate field of a given feed in the database zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_minimal *feed); diff --git a/main.c b/main.c index de5fa41..647f94f 100644 --- a/main.c +++ b/main.c @@ -513,6 +513,13 @@ static void on_interaction(struct discord *client, const struct discord_interact } +static void on_guild_delete(struct discord *client, const struct discord_guild *event) { + (void) client; + if (zblock_feed_info_delete_all_guild(database_conn, event->id)) { + log_error("Unable to delete all feeds from guild %" PRIu64 ". You probably want to clean this up.", event->id); + } +} + // delay before the first feed retrieval (in ms) #define FEED_TIMER_DELAY 15000 @@ -538,6 +545,7 @@ int main(void) { discord_set_on_ready(client, &on_ready); discord_set_on_interaction_create(client, &on_interaction); + discord_set_on_guild_delete(client, &on_guild_delete); discord_timer_interval(client, timer_retrieve_feeds, NULL, NULL, FEED_TIMER_DELAY, FEED_TIMER_INTERVAL, -1); discord_run(client);