代码之家  ›  专栏  ›  技术社区  ›  Rstarks

与Pyton Minecraft服务器状态的Discord Bot

  •  0
  • Rstarks  · 技术社区  · 2 年前

    我试图制作一个不和谐的机器人,让minecraft服务器在线,但在启动机器人后,价值观没有改变。

    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")
    
    

    client.run(“bot_token”)

    试过了,但我有同样的问题:

    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")
    

    错误

    [2024-05-05 00:34:51][INFO]discord.client:使用静态令牌登录 [2024-05-05 00:34:52][信息]不一致。网关:碎片ID无连接到网关(会话ID:c29fff29e2317467a3bfbca68c76aa83)。 RS的Main已登录 [2024-05-05 00:35:43][警告]discord.gateway:碎片ID无心跳阻塞超过10秒。 循环线程回溯(最后一次调用): 文件“C:\Users\playe\PycharmProjects\pythonProject1\main.py”,第27行,位于 client.run(“MTIzNjQxMT2ODk4NjgzMDg3OQ.Gvzhao.__aMFjviZKlS8AG09KDBI2bkslSeXMYH2FMA7s”) 文件“C:\Users\playe\PycharmProjects\pythonProject1.venv\Lib\site-packages\discord\client.py”,第860行,正在运行 asyncio.run(runner()) 文件“C:\Users\playe\AppData\Local\Programs\Python312\Lib\asyncio\runners.py”,第194行,运行中 回流流道。流道(主) 文件“C:\Users\playe\AppData\Local\Programs\Python312\Lib\asyncio\runners.py”,第118行,运行中 回归自我_loop.run_until_complete(任务) run_until_complete中的文件“C:\Users\playe\AppData\Local\Programs\Python312\Lib\asyncio\base_events.py”,第674行 self.run_forever() run_forever中的文件“C:\Users\playe\AppData\Local\Programs\Python312\Lib\asyncio\windows_events.py”,第322行 super().run_forever() run_forever中的文件“C:\Users\playe\AppData\Local\Programs\Python312\Lib\asyncio\base_events.py”,第641行 自己运行() 文件“C:\Users\playe\AppData\Local\Programs\Python312\Lib\asyncio\base_events.py”,第1987行,在_run_once中 手柄跑 文件“C:\Users\playe\AppData\Local\Programs\Python312\Lib\asyncio\events.py”,第88行,在_run中 自己context.run(self.callback,*self.args) 文件“C:\Users\playe\PycharmProjects\pythonProject1.venv\Lib\site-packages\discord\client.py”,第441行,在_run_event中 等待coro(*args,**kwargs) 文件“C:\Users\playe\PycharmProjects\pythonProject1\main.py”,第22行,在on_red中 时间。睡眠(60)

    我变了

    
    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
    

    并添加

    
        while True:
            time.sleep(60)
            server_status = player_count()
            await client.change_presence(activity=discord.activity.Game(name=server_status), status=discord.Status.idle)
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   Dimi140    2 年前

    time.sleep(60) 正在阻止你的discord机器人的主循环。

    将其更改为:

    await asyncio.sleep(60)
    

    别忘了:

    import asyncio