Skip to content

Commit

Permalink
Update mcstatus
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Mar 13, 2024
1 parent 0577928 commit 220bd35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
10 changes: 5 additions & 5 deletions plugins/minecraft_ping.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import socket

from mcstatus import MinecraftServer
# TODO(linuxdaemon): Implement bedrock support
from mcstatus import JavaServer as MinecraftServer
from mcstatus.pinger import PingResponse

from cloudbot import hook
Expand Down Expand Up @@ -59,10 +60,9 @@ def mcping(text):
except (IOError, ValueError) as e:
return "Error pinging server: {}".format(e)

if isinstance(s.description, dict):
description = format_colors(" ".join(s.description["text"].split()))
else:
description = format_colors(" ".join(s.description.split()))
motd = s.motd.to_minecraft()

description = format_colors(" ".join(motd.split()))

output_format = colors.parse(
"{}$(clear) - $(bold){}$(clear) - $(bold){:.1f}ms$(clear) - $(bold){}/{}$(clear) players"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ googlemaps == 4.5.3
imgurpython == 1.1.7
isodate == 0.6.0
lxml == 4.9.2
mcstatus == 6.5.0
mcstatus == 11.1.1
multidict == 6.0.5
nltk == 3.6.6
psutil == 5.8.0
Expand Down
36 changes: 15 additions & 21 deletions tests/plugin_tests/test_minecraft_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,33 @@

import mcstatus
import pytest
from mcstatus.status_response import JavaStatusResponse

from plugins import minecraft_ping


@pytest.fixture()
def mock_mcserver():
with patch.object(mcstatus.MinecraftServer, "lookup") as mock:
with patch.object(mcstatus.JavaServer, "lookup") as mock:
yield mock


def test_mcping(mock_mcserver):
mock_mcserver.return_value = server = MagicMock()
server.status.return_value = status = MagicMock()
status.version.name = "1.12.2"
status.latency = 12.345
status.players.online = 2
status.players.max = 10
status.description = {"text": "A description"}
res = minecraft_ping.mcping("host.invalid")
assert (
res
== "A description\x0f - \x021.12.2\x0f - \x0212.3ms\x0f - \x022/10\x0f players"
server.status.return_value = JavaStatusResponse.build(
{
"version": {
"name": "1.12.2",
"protocol": 340,
},
"description": "A description",
"players": {
"online": 2,
"max": 10,
},
},
12.345,
)


def test_mcping_text(mock_mcserver):
mock_mcserver.return_value = server = MagicMock()
server.status.return_value = status = MagicMock()
status.version.name = "1.12.2"
status.latency = 12.345
status.players.online = 2
status.players.max = 10
status.description = "A description"
res = minecraft_ping.mcping("host.invalid")
assert (
res
Expand Down

0 comments on commit 220bd35

Please sign in to comment.