针对Discord的Minecraft服务器命令:“OSError:服务器未响应任何信息!”

2024-06-16 11:06:23 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试使用discord.py和mcstatus创建一个discord命令,返回延迟和播放器编号。每次运行命令时,我都会收到错误“OSError:Server没有响应任何信息!”我尝试使用MinecraftServer.lookup("mc.hypixel.net")MinecraftServer.lookup("mc.hypixel.net:25565")MinecraftServer("mc.hypixel.net", 25565)和其他服务器,但它们都响应相同的错误

这是我的密码:

import discord
import mcstatus
from mcstatus import MinecraftServer
from discord.ext import commands

client = commands.Bot(command_prefix='\\')
client.remove_command('help')

@client.event
async def on_ready():
    activity = discord.Activity(type=discord.ActivityType.watching, name="for \\\'s")
    await client.change_presence(status=discord.Status.online, activity=activity)
    print('Logged in as {0.user}'.format(client))

@client.event
async def on_command_error(ctx, error):
    print(error)

@client.command()
async def mcserver(ctx):
    server = MinecraftServer.lookup("mc.hypixel.net")
    status = server.status()
    latency = server.ping()
    print("The server replied in {0} ms".format(latency))
    await ctx.channel.send("The server has {0} players and replied in {1}
ms".format(status.players.online, status.latency))

Tags: importclientasyncnetserverdefstatusactivity
1条回答
网友
1楼 · 发布于 2024-06-16 11:06:23

问题在于server.ping()函数。Tbh,我不知道为什么它不起作用,但它对我也不起作用。但令人高兴的是,延迟包含在server.status()调用中


import discord
import mcstatus
from mcstatus import MinecraftServer
from discord.ext import commands

client = commands.Bot(command_prefix='\\')
client.remove_command('help')

@client.event
async def on_ready():
    activity = discord.Activity(type=discord.ActivityType.watching, name="for \\\'s")
    await client.change_presence(status=discord.Status.online, activity=activity)
    print('Logged in as {0.user}'.format(client))

@client.event
async def on_command_error(ctx, error):
    print(error)

@client.command()
async def mcserver(ctx):
    server = MinecraftServer.lookup("mc.hypixel.net")
    status = server.status()
    print("The server replied in {0} ms".format(status.latency))
    await ctx.channel.send("The server has {0} players and replied in {1}
ms".format(status.players.online, status.latency))

我在我的机器上测试了这段代码,它工作得完美无缺。 也许在the github project of mcstats上发表一个问题来解释这个问题

相关问题 更多 >