Python Discord Bot Not Updating Minecraft Server Info

I’m creating a Discord bot to check the status of a Minecraft server and display it in the bot’s activity. However, after launching the bot, the server information appears to be stuck and does not refresh with the updated player count or status changes.

Here’s the code I have so far:

import discord
from discord.ext import commands, tasks
import minestat

def get_server_info():
    server = minestat.MineStat('my-server.com', 25565)
    if server.online:
        status_text = f'Online! v{server.version} - {server.current_players}/{server.max_players} players'
        return status_text
    else:
        return 'Server Offline'

bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())

@bot.event
async def on_ready():
    print(f'{bot.user} has started')
    current_status = get_server_info()
    await bot.change_presence(activity=discord.Game(name=current_status))

bot.run('YOUR_TOKEN_HERE')

I also attempted to implement a loop to update the status every minute, but this resulted in a heartbeat error:

@bot.event
async def on_ready():
    print(f'{bot.user} has started')
    while True:
        current_status = get_server_info()
        await bot.change_presence(activity=discord.Game(name=current_status))
        time.sleep(60)  # This leads to issues

The error indicates that the heartbeat is being blocked, which causes the bot to become unresponsive. What can I do to ensure the status updates automatically without disrupting the bot’s connection?

That heartbeat blocking issue happens because your while loop blocks the entire event loop - Discord can’t maintain its connection properly.

I hit the same problem when I started with Discord bots. Use the @tasks.loop decorator instead of a manual while loop:

@tasks.loop(minutes=1)
async def update_server_status():
    current_status = get_server_info()
    await bot.change_presence(activity=discord.Game(name=current_status))

@bot.event
async def on_ready():
    print(f'{bot.user} has started')
    update_server_status.start()

Watch out for minestat operations - they can take forever if the server’s slow or unreachable. Wrap your get_server_info function in try-except and add a timeout so the task doesn’t hang. Learned this the hard way when my bot stopped updating because it got stuck waiting for an unresponsive server.

Never use time.sleep() in async code! That’s what killed your bot connection. Harry’s right about @tasks.loop, but don’t forget to call update_server_status.start() after the bot’s ready or it won’t run.

Yeah, this blocking issue happens all the time with Discord bots. Your second approach crashes because time.sleep() freezes the entire bot thread - Discord’s internal stuff can’t run anymore.

I’ve hit this same problem monitoring game servers. Besides @tasks.loop, make your get_server_info function async and add error handling. The minestat library loves to hang forever on network issues, which kills your update task.

Discord rate limits presence updates too - every minute’s fine, but don’t go faster or you’ll get temp restrictions. I stick to 2-3 minutes just to be safe.

One more tip: if your Minecraft server drops offline a lot, handle consecutive failures better instead of spamming “Server Offline” updates.