Search code examples
pythondiscord.pyminecraft

Discord Bot With Pyhton Minecraft Server Status


I tried to make a Discord bot for checking a Minecraft server's stauts but after starting the bot the values are not changing.

Here is my code:

import discord
from discord.ext import commands
from discord import Interaction

import minestat
import time


def player_count():
    i = 0

    while (i < 1):
        ms = minestat.MineStat('serverip', serverport)
        print('Minecraft server status of %s on port %d:' % (ms.address, ms.port))
        if ms.online:
            server_status = ('Sunucu Aktif! version %s  %s out of %s players.' % (ms.version, ms.current_players, ms.max_players))
            return server_status  # Return the server status instead of assigning to global variable

client = commands.Bot(command_prefix=".", intents=discord.Intents.all())


@client.event
async def on_ready():
    server_status = player_count()  # Call the player_count function to get the status
    await client.change_presence(activity=discord.activity.Game(name=server_status), status=discord.Status.idle)
    print(f"{client.user.name} is logged in")

I tried putting client.run("bot_token") at the bottom:

import discord
from discord.ext import commands
from discord import Interaction
import minestat
import time


def player_count():
    ms = minestat.MineStat('serverip', serverport)
    server_status = ('Sunucu Aktif! version %s  %s out of %s players.' % (ms.version, ms.current_players, ms.max_players))
    return server_status  # Return the server status instead of assigning to global variable

client = commands.Bot(command_prefix=".", intents=discord.Intents.all())


@client.event
async def on_ready():
    server_status = player_count()  # Call the player_count function to get the status
    await client.change_presence(activity=discord.activity.Game(name=server_status), status=discord.Status.idle)
    print(f"{client.user.name} is logged in")
    while True:
        time.sleep(60)
        server_status = player_count()
        await client.change_presence(activity=discord.activity.Game(name=server_status), status=discord.Status.idle)


client.run("bot_token")

error:

[2024-05-05 00:34:51] [INFO    ] discord.client: logging in using static token
[2024-05-05 00:34:52] [INFO    ] discord.gateway: Shard ID None has connected to Gateway (Session ID: c29fff29e2317467a3bfbca68c76aa83).
RS's Main is logged in
[2024-05-05 00:35:43] [WARNING ] discord.gateway: Shard ID None heartbeat blocked for more than 10 seconds.
Loop thread traceback (most recent call last):
  File "C:\Users\playe\PycharmProjects\pythonProject1\main.py", line 27, in 
    client.run("MTIzNjQxMTI2ODk4NjgzMDg3OQ.Gvzhao.__aMFjviZKlS8AG09KDBI2bkslSeXMYH2FMA7s")
  File "C:\Users\playe\PycharmProjects\pythonProject1\.venv\Lib\site-packages\discord\client.py", line 860, in run
    asyncio.run(runner())
  File "C:\Users\playe\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
  File "C:\Users\playe\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
  File "C:\Users\playe\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 674, in run_until_complete
    self.run_forever()
  File "C:\Users\playe\AppData\Local\Programs\Python\Python312\Lib\asyncio\windows_events.py", line 322, in run_forever
    super().run_forever()
  File "C:\Users\playe\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 641, in run_forever
    self._run_once()
  File "C:\Users\playe\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 1987, in _run_once
    handle._run()
  File "C:\Users\playe\AppData\Local\Programs\Python\Python312\Lib\asyncio\events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\playe\PycharmProjects\pythonProject1\.venv\Lib\site-packages\discord\client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\playe\PycharmProjects\pythonProject1\main.py", line 22, in on_ready
    time.sleep(60)

I changed:

i = 0
def player_count(): 
   while i < 1:
       ms = minestat.MineStat('cakalistan.aternos.me', 29049)
       if ms.online:
          print('Server is online running version %s with %s out of %s players.' % (ms.version,         ms.current_players, ms.max_players))
       server_status = ('Sunucu Aktif! version %s  %s out of %s players.' % (ms.version, ms.current_players, ms.max_players))
    return server_status  # Return the server status instead of assigning to global variable

and added:

    while True:
        time.sleep(60)
        server_status = player_count()
        await client.change_presence(activity=discord.activity.Game(name=server_status), status=discord.Status.idle)

but it is not working.


Solution

  • time.sleep(60) is blocking the main loop of your discord bot.

    Change it to:

    await asyncio.sleep(60)
    

    And dont forget:

    import asyncio