Skip to content

Commit 93cc574

Browse files
committed
[Stats] Add server command.
1 parent 27d232c commit 93cc574

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

stats/stats.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from redbot.core import commands, checks
22
from .ase import MTAServerlist
33

4-
import discord, aiohttp, asyncio, time
4+
import discord, aiohttp, asyncio, time, re
55

66
class Stats(commands.Cog):
77
"""My custom cog"""
@@ -46,3 +46,31 @@ async def top(self, ctx):
4646
if not v["version"].endswith("n"):
4747
embed.add_field(name="**"+v["name"]+"**", value=str(v["players"])+"/"+str(v["maxplayers"]), inline=False)
4848
await ctx.channel.send(embed=embed)
49+
50+
@stats.command()
51+
@commands.cooldown(1, 30, commands.BucketType.user)
52+
@commands.guild_only()
53+
async def server(self, ctx, host):
54+
"""Shows the provided server stats."""
55+
async with ctx.typing():
56+
host_ip = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", host)
57+
host_port = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:(\d+)", host)
58+
if not host_ip:
59+
embed = discord.Embed(colour=discord.Colour(0xf5a623), description="**Invalid ip address.**")
60+
return await ctx.channel.send(embed=embed)
61+
else:
62+
host_ip = host_ip[0]
63+
if not host_port:
64+
host_port = "22003"
65+
else:
66+
host_port = host_port[0]
67+
async with aiohttp.ClientSession() as session:
68+
async with session.get(self.url) as response:
69+
ase = MTAServerlist()
70+
await ase.parse(await response.read())
71+
for v in ase.servers:
72+
if str(v["ip"]) == str(host_ip) and str(v["port"]) == str(host_port):
73+
embed = discord.Embed(colour=discord.Colour(0xf5a623), description="**"+v["name"]+"**\n"+str(v["players"])+"/"+str(v["maxplayers"]))
74+
return await ctx.channel.send(embed=embed)
75+
embed = discord.Embed(colour=discord.Colour(0xf5a623), description="**No result.**")
76+
return await ctx.channel.send(embed=embed)

0 commit comments

Comments
 (0)