Prevent Discord bot from messaging when channel is inactive

I’m working on a Discord bot in Python that sends random messages every 6 hours. But I want to make it smarter. How can I change the code so the bot doesn’t post if no one else has written anything in the channel since its last message?

Here’s a simplified version of what I have so far:

import discord
import asyncio
import random

client = discord.Client()

async def send_messages():
    while True:
        channel = client.get_channel(123456789)  # Replace with actual channel ID
        message = random.choice(['Hello!', 'How's everyone?', 'What's new?'])
        await channel.send(message)
        await asyncio.sleep(21600)  # 6 hours

client.loop.create_task(send_messages())
client.run('YOUR_BOT_TOKEN')

I’m not sure how to check for channel activity before sending. Any ideas?

hey swiftcoder15, here’s a idea: before sending, check the channel’s message history. you can use channel.history() to get recent msgs. if the last message is from ur bot, don’t post a new one. might look somethin like:

last_msg = await channel.history(limit=1).flatten()
if not last_msg or last_msg[0].author != client.user:
    await channel.send(message)

hope that helps!

I’ve faced a similar challenge with my Discord bot. Here’s what worked for me:

Instead of using a fixed sleep time, I implemented a system where the bot tracks the timestamp of its last message and the most recent user message in the channel. Before sending a new message, it compares these timestamps.

You could modify your send_messages function like this:

async def send_messages():
    last_bot_message = datetime.now()
    while True:
        channel = client.get_channel(123456789)
        last_user_message = await get_last_user_message(channel)
        
        if last_user_message > last_bot_message:
            message = random.choice(['Hello!', 'How's everyone?', 'What's new?'])
            await channel.send(message)
            last_bot_message = datetime.now()
        
        await asyncio.sleep(21600)

This approach ensures the bot only posts if there’s been user activity since its last message. You’ll need to implement the get_last_user_message function to fetch the timestamp of the most recent non-bot message in the channel. This method has worked well for keeping my bot’s chatter relevant and non-intrusive.

I’ve implemented a similar feature in my Discord bot. Here’s a more efficient approach:

Store the ID of the bot’s last message as a variable. Before sending a new message, fetch the latest message in the channel and compare its ID to the stored one. If they’re different, it means there’s been activity, so send a new message and update the stored ID.

last_message_id = None

async def send_messages():
    global last_message_id
    while True:
        channel = client.get_channel(123456789)
        latest_message = await channel.fetch_message(channel.last_message_id)
        
        if latest_message.id != last_message_id:
            message = random.choice(['Hello!', 'How's everyone?', 'What's new?'])
            sent_message = await channel.send(message)
            last_message_id = sent_message.id
        
        await asyncio.sleep(21600)

This method is more performant as it only fetches one message instead of the channel history each time.