From 26b541ceefbe865e5ae609ce4bf4dfd0ef3f7974 Mon Sep 17 00:00:00 2001 From: Will Brown Date: Tue, 15 Apr 2025 16:06:27 -0400 Subject: [PATCH] Add storytime feature --- config.c | 5 ++++ config.h | 1 + config.json | 3 ++- main.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index 612cd20..489a030 100644 --- a/config.c +++ b/config.c @@ -33,6 +33,11 @@ zblock_config_err zblock_config_load(struct discord *client) { if (!zblock_config.tuesday_channel) return ZBLOCK_CONFIG_NO_TUESDAY_CHANNEL; } + // failure here will just make the bot complain whenever somebody requests a storytime compilation + struct ccord_szbuf_readonly storytime_channel = discord_config_get_field(client, (char *[2]){"zblock", "storytime_channel"}, 2); + zblock_config.storytime_channel = 0; // initialize it with something + if (storytime_channel.size > 0) zblock_config.storytime_channel = strtoull(storytime_channel.start, NULL, 10); + return ZBLOCK_CONFIG_OK; } diff --git a/config.h b/config.h index 85e3a55..22ef6dd 100644 --- a/config.h +++ b/config.h @@ -7,6 +7,7 @@ extern struct zblock_config { char *conninfo; u64snowflake tuesday_channel; + u64snowflake storytime_channel; bool tuesday_enable; } zblock_config; diff --git a/config.json b/config.json index 304b86a..8703017 100644 --- a/config.json +++ b/config.json @@ -23,6 +23,7 @@ "tuesday": { "enable": false, "channel": "YOUR-CHANNEL-ID" - } + }, + "storytime_channel": "YOUR-CHANNEL-ID" } } diff --git a/main.c b/main.c index b3ce7c7..7b5e568 100644 --- a/main.c +++ b/main.c @@ -444,6 +444,65 @@ static void list_update(struct discord *client, const struct discord_interaction Arena_delete(arena); } +static bool acceptchars(int c, const char *accept) { + while (*accept) if (c == *accept++) return true; + return false; +} + +struct storytime_args { + struct discord *client; + u64snowflake channel_id; + u64snowflake user_id; +}; + +struct storytime_msgs_list { + struct discord_messages data; + struct storytime_msgs_list *next; +}; + +static void bot_command_story(struct discord *client, const struct discord_interaction *event) { + if (!zblock_config.storytime_channel) { + struct discord_interaction_response res = { + .type = DISCORD_INTERACTION_CHANNEL_MESSAGE_WITH_SOURCE, + .data = &(struct discord_interaction_callback_data) { + .content = "There is no storytime channel set in the bot config." + } + }; + + discord_create_interaction_response(client, event->id, event->token, &res, NULL); + return; + } + + // time to dump our messages + char story_content[DISCORD_MAX_MESSAGE_LEN]; + char *story_content_last = story_content; + + struct discord_get_channel_messages params = { .limit = 100 }; + struct discord_messages msgs = {0}; + struct discord_ret_messages ret = { .sync = &msgs }; + discord_get_channel_messages(client, zblock_config.storytime_channel, ¶ms, &ret); + + for (int i = msgs.size - 1; i >= 0; --i) { + char *msg_content = msgs.array[i].content; + if (!acceptchars(msg_content[0], "!),.?")) { // check for punctuation + story_content_last = stpncpy(story_content_last, " ", sizeof(story_content) - (story_content_last - story_content)); + } + story_content_last = stpncpy(story_content_last, msg_content, sizeof(story_content) - (story_content_last - story_content)); + } + story_content[sizeof(story_content) - 1] = 0; // in case the message was truncated + + discord_messages_cleanup(&msgs); + + struct discord_interaction_response res = { + .type = DISCORD_INTERACTION_CHANNEL_MESSAGE_WITH_SOURCE, + .data = &(struct discord_interaction_callback_data) { + .content = story_content_last != story_content ? story_content : "Sorry, I don't have a story for you." + } + }; + discord_create_interaction_response(client, event->id, event->token, &res, NULL); + +} + static void bot_command_help(struct discord *client, const struct discord_interaction *event) { char msg[DISCORD_MAX_MESSAGE_LEN]; @@ -507,6 +566,14 @@ static struct bot_command commands[] = { }, .func = &bot_command_list }, + { + .cmd = { + .name = "story", + .description = "Tell me a story", + .default_permission = true + }, + .func = &bot_command_story + }, { .cmd = { .name = "help",