Discord bot always shows server as down even when its running

I’m working on a Discord bot that should monitor if a game server is up or down. The problem is it keeps saying the server is offline when I know for sure its actually online and working.

I’ve tested with different IP addresses and ports but they all return offline status. The bot runs without errors but the status detection isn’t working right. I can connect to the server through other tools so I know its not actually down.

One time I managed to get it showing online status but then when the server actually went down it didn’t send any notification. I have no idea what I changed to make that work.

import os
import discord
import socket
import asyncio

BOT_TOKEN = os.environ['BOT_TOKEN']
NOTIFY_CHANNEL = 1234567890123456789
HOST_ADDRESS = '192.168.1.100'
HOST_PORT = 25565

bot = discord.Client(intents=discord.Intents.all())

async def monitor_host_status():
    last_state = None
    
    while True:
        try:
            await bot.wait_until_ready()
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(10)
            sock.connect((HOST_ADDRESS, HOST_PORT))
            sock.close()
            current_state = "up"
        except Exception:
            current_state = "down"
        
        if current_state != last_state:
            last_state = current_state
            notify_channel = bot.get_channel(NOTIFY_CHANNEL)
            await notify_channel.send(f"Host status changed: {current_state}")
        
        await asyncio.sleep(120)

@bot.event
async def on_ready():
    print(f'Bot connected as {bot.user}')
    bot.loop.create_task(monitor_host_status())

bot.run(BOT_TOKEN)

I’m running this on Replit so maybe that’s causing issues with the socket connections? Has anyone else had problems with server monitoring bots giving wrong status updates?

Check if your bot has network permissions on Replit. I had the same issue - monitoring worked inconsistently because Replit blocks certain outbound connections. Since it worked once, your code’s probably fine but the platform’s interfering. Also, you’ve got wait_until_ready() inside the loop which might mess with timing - move it outside to the beginning of the function. For debugging, add print statements to see what exceptions you’re actually getting instead of catching everything blindly. It’s probably connection refused errors from Replit’s networking restrictions.

totally agree, repl.it can be a hassle for bot hosting. switching to a VPS really makes a diff, plus you get more control. give it a shot!

Your socket connection handling is the problem. You’re creating new sockets each time without managing the connection state properly. Wrap your socket operations in a try-finally block for cleanup, and use socket.create_connection() instead of creating sockets manually. That 10 second timeout’s probably too short - I bumped mine to 30 seconds and added error logging to catch specific exceptions. Fixed the same issue with my monitoring bot. Since it worked once, your logic’s fine - just need better connection handling.