Skip to content

Fix scheduled posting when last_posted is uninitialized#25

Merged
joemurrell merged 2 commits intomainfrom
copilot/fix-scheduled-posting-bug
Feb 16, 2026
Merged

Fix scheduled posting when last_posted is uninitialized#25
joemurrell merged 2 commits intomainfrom
copilot/fix-scheduled-posting-bug

Conversation

Copy link
Contributor

Copilot AI commented Feb 16, 2026

Scheduled posts never trigger when last_posted is 0 (Redis key unset/expired). The bot calculates next_post_time = 0 + 86400 (Jan 2, 1970), then skips because current_time > next_post_time + 300.

Changes

post_brevity_term() - Detect and handle uninitialized state

# Before
last_posted = await get_last_posted(guild_id)  # Returns 0.0 if unset
next_post_time = last_posted + (freq_hours * 3600)  # = 86400 (Jan 1970)

if current_time < next_post_time - 300 or current_time > next_post_time + 300:
    continue  # Always skips when current_time >> next_post_time

# After
if last_posted < 1577836800:  # Jan 1, 2020
    next_post_time = time.time()  # Post immediately
else:
    next_post_time = last_posted + (freq_hours * 3600)

if current_time < next_post_time - 300:
    continue  # Only skip if too early

enable_posting() - Initialize timestamp on first enable

last_posted = await get_last_posted(guild_id)
if last_posted < 1577836800:
    await set_last_posted(guild_id, time.time())

/setup command - Reset timer during configuration

await enable_posting(interaction.guild.id)
await set_last_posted(interaction.guild.id, time.time())

Upper bound removal allows overdue posts without causing burst (timer resets after each post).

Original prompt

Problem

Scheduled posting is not working on Discord servers where posting is enabled. The bot runs every 5 minutes but never posts messages on the configured schedule (e.g., every 24 hours).

Root Cause

In the post_brevity_term() background task (lines 1318-1363 in brevitybot.py), when last_posted is 0 (uninitialized or cleared from Redis), the scheduling logic breaks:

last_posted = await get_last_posted(guild_id)  # Returns 0.0 if not set
next_post_time = last_posted + (freq_hours * 3600)  # e.g., 0 + 86400 = 86400 (Jan 2, 1970)

current_time = time.time()  # e.g., 1739750400 (Feb 2026)
if current_time < next_post_time - 300 or current_time > next_post_time + 300:
    continue  # Always skips because current_time >> next_post_time

When last_posted is 0:

  • next_post_time becomes a timestamp in January 1970
  • current_time is the current timestamp (February 2026)
  • The condition current_time > next_post_time + 300 is always True
  • The bot skips posting entirely and gets stuck permanently

This happens when:

  • A server enables posting for the first time
  • Redis data is cleared or lost
  • The bot restarts and Redis keys are expired
  • Recent updates that may have affected Redis key storage

Solution

Fix the post_brevity_term() function to handle uninitialized last_posted timestamps:

  1. Check if last_posted is 0 or very old (e.g., before 2020) - this indicates uninitialized state
  2. Post immediately when uninitialized, treating it as the first scheduled post
  3. Initialize last_posted when posting is first enabled via enable_posting() function
  4. Add logging to help diagnose scheduling issues

Code Changes Needed

In post_brevity_term() function (around line 1327):

freq_hours = await get_post_frequency(guild_id)
last_posted = await get_last_posted(guild_id)

# Handle uninitialized or invalid last_posted timestamp
# If last_posted is 0 or before Jan 1, 2020, treat as uninitialized
if last_posted < 1577836800:  # Jan 1, 2020 timestamp
    logger.info("Uninitialized last_posted for guild %s, posting immediately", guild_id)
    # Post immediately by setting next_post_time to now
    next_post_time = time.time()
else:
    next_post_time = last_posted + (freq_hours * 3600)

# Check if it's time to post (allowing a ±5-minute window)
current_time = time.time()
if current_time < next_post_time - 300:
    continue
# Note: Removed the upper bound check since we now handle initialization

In enable_posting() function (around line 207):

async def enable_posting(guild_id):
    await r.srem(DISABLED_GUILDS_KEY, str(guild_id))
    
    # Initialize last_posted if not set, so the first post happens after the configured interval
    last_posted = await get_last_posted(guild_id)
    if last_posted < 1577836800:  # Uninitialized
        await set_last_posted(guild_id, time.time())
        logger.info("Initialized last_posted for guild %s", guild_id)
    
    logger.info("Posting enabled for guild %s", guild_id)

In /setup command (around line 558):

@tree.command(name="setup", description="Set the current channel for daily brevity posts.")
async def setup(interaction: discord.Interaction):
    await save_config(interaction.guild.id, interaction.channel.id)
    await enable_posting(interaction.guild.id)
    # Initialize last_posted so first post happens after the configured interval
    await set_last_posted(interaction.guild.id, time.time())
    await interaction.response.send_message(f"Setup complete for <#{interaction.channel.id}>.", ephemeral=True)

Testing

After this fix:

  1. Servers with uninitialized last_posted will post immediately on the next 5-minute check
  2. New servers running /setup will have their first post scheduled for freq_hours from setup time
  3. Servers with valid timestamps continue working as before
  4. Logs will clearly show when uninitialized timestamps are detected

Files to Modify

  • brevitybot.py - Update post_brevity_term(), enable_posting(), and /setup command

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.

Co-authored-by: joemurrell <6580685+joemurrell@users.noreply.github.com>
@joemurrell joemurrell marked this pull request as ready for review February 16, 2026 02:05
Copilot AI changed the title [WIP] Fix scheduled posting not working in Discord bot Fix scheduled posting when last_posted is uninitialized Feb 16, 2026
Copilot AI requested a review from joemurrell February 16, 2026 02:06
@joemurrell joemurrell merged commit 2358af1 into main Feb 16, 2026
3 checks passed
Copy link
Owner

@joemurrell joemurrell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants