Discord bot always shows server as down even when it's running

I’m having trouble with my Discord bot that monitors server status. The problem is that it keeps reporting the server as offline even though I know it’s actually online.

Here’s what I’ve built:

import os
import discord
import socket
import asyncio

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

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

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

@bot.event
async def on_ready():
    print(f'Bot ready: {bot.user}')
    bot.loop.create_task(monitor_host())

bot.run(API_KEY)

The issue: It always says the server is down but I can connect to it fine using other tools. I tried different IPs and ports but same result. Could this be because I’m running it on Replit? Maybe there’s some network restriction or I’m missing something in my code?

Had the same issue when hosting my bot on cloud platforms. Replit blocks connections to private IPs like 192.168.1.100 for security - your server works fine locally but Replit can’t reach it through your router. Two fixes that worked for me: run the bot locally on the same network, or set up port forwarding on your router to expose the server via your public IP. Just grab your public IP, configure port forwarding for the specific port, and you’re set. Fair warning though - exposing your server to the internet comes with security risks.

That 3-second timeout is probably too aggressive. I’ve hit the same false negatives monitoring game servers - the handshake just takes longer than expected, especially on cloud hosts like Replit. Bump it to 10-15 seconds first. These connections can be slow to establish even when the server’s running fine. Also, some game servers have basic DDoS protection that blocks or delays connections from unfamiliar IP ranges. That’d explain why your local tools work but the bot from Replit’s infrastructure doesn’t.

Yeah, that’s definitely a Replit thing - it can’t access your local network addresses. Try testing with a public server first, like google.com port 80, just to see if your monitoring code actually works. If it does, then you know it’s the private IP issue Grace mentioned.