Add storytime feature

This commit is contained in:
2025-04-15 16:06:27 -04:00
parent 77157dcabe
commit 26b541ceef
4 changed files with 75 additions and 1 deletions

View File

@ -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;
}

View File

@ -7,6 +7,7 @@
extern struct zblock_config {
char *conninfo;
u64snowflake tuesday_channel;
u64snowflake storytime_channel;
bool tuesday_enable;
} zblock_config;

View File

@ -23,6 +23,7 @@
"tuesday": {
"enable": false,
"channel": "YOUR-CHANNEL-ID"
}
},
"storytime_channel": "YOUR-CHANNEL-ID"
}
}

67
main.c
View File

@ -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, &params, &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",