Skip to content

Commit 7608af6

Browse files
committed
Update mcstatus lib and refactor minecraft_ping.py
1 parent 45450ec commit 7608af6

File tree

4 files changed

+115
-18
lines changed

4 files changed

+115
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Refactor tests to remove dependency on mock library
1313
- Change link_announcer.py to only warn on connection errors
1414
- Change user lookup logic in last.fm plugin
15+
- Refactor minecraft_ping plugin for updated mcstatus library
1516
### Fixed
1617
- Fix matching exception in horoscope test
1718
- Fix youtube.py ISO time parse

plugins/minecraft_ping.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
import socket
22

33
from mcstatus import MinecraftServer
4+
from mcstatus.pinger import PingResponse
45

56
from cloudbot import hook
7+
from cloudbot.util import colors
68

7-
mc_colors = [('\xa7f', '\x0300'), ('\xa70', '\x0301'), ('\xa71', '\x0302'), ('\xa72', '\x0303'),
8-
('\xa7c', '\x0304'), ('\xa74', '\x0305'), ('\xa75', '\x0306'), ('\xa76', '\x0307'),
9-
('\xa7e', '\x0308'), ('\xa7a', '\x0309'), ('\xa73', '\x0310'), ('\xa7b', '\x0311'),
10-
('\xa71', '\x0312'), ('\xa7d', '\x0313'), ('\xa78', '\x0314'), ('\xa77', '\x0315'),
11-
('\xa7l', '\x02'), ('\xa79', '\x0310'), ('\xa7o', ''), ('\xa7m', '\x13'),
12-
('\xa7r', '\x0f'), ('\xa7n', '\x15')]
9+
mc_colors = [
10+
("\xa7f", "\x0300"),
11+
("\xa70", "\x0301"),
12+
("\xa71", "\x0302"),
13+
("\xa72", "\x0303"),
14+
("\xa7c", "\x0304"),
15+
("\xa74", "\x0305"),
16+
("\xa75", "\x0306"),
17+
("\xa76", "\x0307"),
18+
("\xa7e", "\x0308"),
19+
("\xa7a", "\x0309"),
20+
("\xa73", "\x0310"),
21+
("\xa7b", "\x0311"),
22+
("\xa71", "\x0312"),
23+
("\xa7d", "\x0313"),
24+
("\xa78", "\x0314"),
25+
("\xa77", "\x0315"),
26+
("\xa7l", "\x02"),
27+
("\xa79", "\x0310"),
28+
("\xa7o", ""),
29+
("\xa7m", "\x13"),
30+
("\xa7r", "\x0f"),
31+
("\xa7n", "\x15"),
32+
]
1333

1434

1535
def format_colors(description):
@@ -24,10 +44,10 @@ def mcping(text):
2444
try:
2545
server = MinecraftServer.lookup(text)
2646
except (IOError, ValueError) as e:
27-
return e
47+
return str(e)
2848

2949
try:
30-
s = server.status()
50+
s = server.status() # type: PingResponse
3151
except socket.gaierror:
3252
return "Invalid hostname"
3353
except socket.timeout:
@@ -44,13 +64,10 @@ def mcping(text):
4464
else:
4565
description = format_colors(" ".join(s.description.split()))
4666

47-
# I really hate people for putting colors IN THE VERSION
48-
# WTF REALLY THIS IS A THING NOW?
67+
output_format = colors.parse(
68+
"{}$(clear) - $(bold){}$(clear) - $(bold){:.1f}ms$(clear) - $(bold){}/{}$(clear) players"
69+
)
4970

50-
if s.latency:
51-
return "{}\x0f - \x02{}\x0f - \x02{:.1f}ms\x02" \
52-
" - \x02{}/{}\x02 players".format(description, s.version.name_clean, s.latency,
53-
s.players.online, s.players.max).replace("\n", "\x0f - ")
54-
return "{}\x0f - \x02{}\x0f" \
55-
" - \x02{}/{}\x02 players".format(description, s.version.name_clean,
56-
s.players.online, s.players.max).replace("\n", "\x0f - ")
71+
return output_format.format(
72+
description, s.version.name, s.latency, s.players.online, s.players.max
73+
).replace("\n", colors.parse("$(clear) - "))

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cleverwrap==0.3.0.2
33
feedparser==5.2.1
44
forecastiopy==0.22
55
geoip2==3.0.0
6-
git+https://github.com/CloudBotIRC/mcstatus.git@master
6+
mcstatus==3.1.0
77
googlemaps==4.2.0
88
imgurpython==1.1.7
99
isodate==0.6.0
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import socket
2+
from unittest.mock import MagicMock, patch
3+
4+
import mcstatus
5+
import pytest
6+
7+
from plugins import minecraft_ping
8+
9+
10+
@pytest.fixture()
11+
def mock_mcserver():
12+
with patch.object(mcstatus.MinecraftServer, "lookup") as mock:
13+
yield mock
14+
15+
16+
def test_mcping(mock_mcserver):
17+
mock_mcserver.return_value = server = MagicMock()
18+
server.status.return_value = status = MagicMock()
19+
status.version.name = "1.12.2"
20+
status.latency = 12.345
21+
status.players.online = 2
22+
status.players.max = 10
23+
status.description = {"text": "A description"}
24+
res = minecraft_ping.mcping("host.invalid")
25+
assert (
26+
res
27+
== "A description\x0f - \x021.12.2\x0f - \x0212.3ms\x0f - \x022/10\x0f players"
28+
)
29+
30+
31+
def test_mcping_text(mock_mcserver):
32+
mock_mcserver.return_value = server = MagicMock()
33+
server.status.return_value = status = MagicMock()
34+
status.version.name = "1.12.2"
35+
status.latency = 12.345
36+
status.players.online = 2
37+
status.players.max = 10
38+
status.description = "A description"
39+
res = minecraft_ping.mcping("host.invalid")
40+
assert (
41+
res
42+
== "A description\x0f - \x021.12.2\x0f - \x0212.3ms\x0f - \x022/10\x0f players"
43+
)
44+
45+
46+
@pytest.mark.parametrize(
47+
"error,reply",
48+
[
49+
(IOError("Some IOError"), "Some IOError"),
50+
(ValueError("Some other ValueError"), "Some other ValueError"),
51+
],
52+
)
53+
def test_mcping_lookup_errors(error, reply, mock_mcserver):
54+
mock_mcserver.side_effect = error
55+
res = minecraft_ping.mcping("host.invalid")
56+
57+
assert res == reply
58+
59+
60+
@pytest.mark.parametrize(
61+
"error,reply",
62+
[
63+
(socket.gaierror(2, "Foo"), "Invalid hostname"),
64+
(socket.timeout(), "Request timed out"),
65+
(ConnectionRefusedError(), "Connection refused"),
66+
(ConnectionError(), "Connection error"),
67+
(IOError("Some IOError"), "Error pinging server: Some IOError"),
68+
(
69+
ValueError("Some other ValueError"),
70+
"Error pinging server: Some other ValueError",
71+
),
72+
],
73+
)
74+
def test_mcping_status_errors(error, reply, mock_mcserver):
75+
mock_mcserver.return_value = server = MagicMock()
76+
server.status.side_effect = error
77+
res = minecraft_ping.mcping("host.invalid")
78+
79+
assert res == reply

0 commit comments

Comments
 (0)