From e1b470b27c65f2e9d8531b8533493297411eb08a Mon Sep 17 00:00:00 2001 From: Will Brown Date: Mon, 30 Dec 2024 14:40:45 -0500 Subject: [PATCH] [main.c] Immediately remembered why I didn't register the timers on ready --- main.c | 63 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/main.c b/main.c index 42cc427..ece11db 100644 --- a/main.c +++ b/main.c @@ -516,18 +516,6 @@ static struct bot_command commands[] = { } }; -// delay before the first feed retrieval (in ms) -#define FEED_TIMER_DELAY 15000 - -// interval for the feed retrieval timer (in ms) -#define FEED_TIMER_INTERVAL 600000 - -// seconds in a day -#define ONE_DAY_SEC 86400 - -// milliseconds in a week -#define ONE_WEEK_MS 604800000 - static void on_ready(struct discord *client, const struct discord_ready *event) { log_info("Logged in as %s!", event->user->username); @@ -535,25 +523,6 @@ static void on_ready(struct discord *client, const struct discord_ready *event) for (struct bot_command *i = commands; i < commands + sizeof(commands) / sizeof(*commands); ++i) { discord_create_global_application_command(client, event->application->id, &i->cmd, NULL); } - - // feed timer - discord_timer_interval(client, timer_retrieve_feeds, NULL, NULL, FEED_TIMER_DELAY, FEED_TIMER_INTERVAL, -1); - - // find the next tueday and start the timer for the tuesday event - if (zblock_config.tuesday_enable) { - time_t current_time = time(NULL); - struct tm midnight_tm; - gmtime_r(¤t_time, &midnight_tm); - // set it to midnight - midnight_tm.tm_sec = 0; - midnight_tm.tm_min = 0; - midnight_tm.tm_hour = 0; - time_t midnight_time = timegm(&midnight_tm); - // find next tuesday and add it to the time - time_t next_tuesday = midnight_time + ONE_DAY_SEC * (((1 - midnight_tm.tm_wday + 7) % 7) + 1); - // set a timer that starts midnight next tuesday and triggers every week - discord_timer_interval(client, timer_tuesday_event, NULL, NULL, (next_tuesday - current_time) * 1000, ONE_WEEK_MS, -1); - } } static void on_interaction(struct discord *client, const struct discord_interaction *event) { @@ -591,6 +560,18 @@ static void on_guild_delete(struct discord *client, const struct discord_guild * } } +// delay before the first feed retrieval (in ms) +#define FEED_TIMER_DELAY 15000 + +// interval for the feed retrieval timer (in ms) +#define FEED_TIMER_INTERVAL 600000 + +// seconds in a day +#define ONE_DAY_SEC 86400 + +// milliseconds in a week +#define ONE_WEEK_MS 604800000 + int main(void) { int exit_code = 0; @@ -617,6 +598,26 @@ 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); + + // register timers + discord_timer_interval(client, timer_retrieve_feeds, NULL, NULL, FEED_TIMER_DELAY, FEED_TIMER_INTERVAL, -1); + + // find the next tueday and start the timer for the tuesday event + if (zblock_config.tuesday_enable) { + time_t current_time = time(NULL); + struct tm midnight_tm; + gmtime_r(¤t_time, &midnight_tm); + // set it to midnight + midnight_tm.tm_sec = 0; + midnight_tm.tm_min = 0; + midnight_tm.tm_hour = 0; + time_t midnight_time = timegm(&midnight_tm); + // find next tuesday and add it to the time + time_t next_tuesday = midnight_time + ONE_DAY_SEC * (((1 - midnight_tm.tm_wday + 7) % 7) + 1); + // set a timer that starts midnight next tuesday and triggers every week + discord_timer_interval(client, timer_tuesday_event, NULL, NULL, (next_tuesday - current_time) * 1000, ONE_WEEK_MS, -1); + } + discord_run(client); PQfinish(database_conn);