Skip to content

Commit

Permalink
Add twstatus
Browse files Browse the repository at this point in the history
  • Loading branch information
12pm committed Oct 9, 2018
1 parent ac9e48d commit 7901ec1
Show file tree
Hide file tree
Showing 5 changed files with 754 additions and 8 deletions.
5 changes: 3 additions & 2 deletions bot.py
Expand Up @@ -39,7 +39,8 @@ def setup_logging():
'cogs.profilecard',
'cogs.testing_main',
'cogs.testing_archiving',
'cogs.testing_moderation'
'cogs.testing_moderation',
'cogs.twstatus'
]


Expand Down Expand Up @@ -91,4 +92,4 @@ def run(self):
bot = DDNet()

with setup_logging():
bot.run()
bot.run()
99 changes: 99 additions & 0 deletions cogs/twstatus.py
@@ -0,0 +1,99 @@
import json
import asyncio
from datetime import datetime

import discord
from discord.ext import commands
from twstatus import ServerHandler

CHAN_INFO = 392853737099624449
MSG_STATUS = 421364843161976833

def format_time(seconds):
return '%02d:%02d' % divmod(seconds, 60)

def format_score(score, pure=False):
if pure:
return str(score)

if score == -9999:
return '00:00'

return format_time(abs(score))

class TwStatus:
def __init__(self, bot):
self.bot = bot
with open('tw-status/info', 'r', encoding='utf-8') as inp:
self.info = json.load(inp)
self.country_codes = {
'GER': 'de',
'RUS': 'ru',
'CHL': 'cl',
'BRA': 'br',
'ZAF': 'za',
'USA': 'us',
'CAN': 'ca',
'CHN': 'cn'
}

async def on_ready(self):
await self.ddnet_status()


async def get_ddnet_servers(self, servers):
out = []
for name, addresses in servers.items():
count = 0
player_count = 0
for addr in addresses:
ip, port = addr.split(':')
server = ServerHandler(ip, int(port), False, 2)
request = await server.get_info()
if request:
count += 1
player_count += request.client_count

out.append((name, (count, len(addresses)), player_count))

return out


async def ddnet_status(self):
info_chan = None
while info_chan is None:
try:
info_chan = self.bot.get_channel(CHAN_INFO)
embed_msg = await info_chan.get_message(MSG_STATUS)
except:
pass

while not self.bot.is_closed():
servers = {s['name']: s['servers']['DDNet'] for s in self.info['servers']}
status = await self.get_ddnet_servers(servers)
total_servers = sum([s[1][0] for s in status])
max_servers = sum([s[1][1] for s in status])
total_players = sum([s[2] for s in status])
desc = f'**Total servers:** {total_servers}/{max_servers} — **Total players:** {total_players}'
embed = discord.Embed(description=desc, timestamp=datetime.utcnow())
ddnet_emoji = 'https://cdn.discordapp.com/emojis/391727274824826880.png?v=1'
embed.set_author(name=f'DDNet Servers', icon_url=ddnet_emoji)
embed.set_footer(text='Last updated')

for stats in status:
name, servers, players = stats
flag = f':flag_{self.country_codes[name]}:'
name = f'{flag} {name}'
count, max_count = servers
content = f'**Servers:** {count}/{max_count}\n**Players:** {players}'
embed.add_field(name=name, value=content)

while len(embed.fields) % 3 != 0:
embed.add_field(name='\255', value='\255')

await embed_msg.edit(embed=embed)
await asyncio.sleep(360)


def setup(bot):
bot.add_cog(TwStatus(bot))
13 changes: 7 additions & 6 deletions requirements.txt
@@ -1,6 +1,7 @@
git+https://github.com/Rapptz/discord.py@rewrite
requests
colorthief
msgpack
fuzzywuzzy
pymysql
git+https://github.com/Rapptz/discord.py@rewrite
requests
colorthief
msgpack
fuzzywuzzy
pymysql
twstatus.py
257 changes: 257 additions & 0 deletions tw-status/countryflags.json
@@ -0,0 +1,257 @@
{
"-1":"default",
"901":"XEN",
"902":"XNI",
"903":"XSC",
"904":"XWA",
"905":"XEU",
"906":"XCA",
"737":"SS",
"4":"AF",
"248":"AX",
"8":"AL",
"12":"DZ",
"16":"AS",
"20":"AD",
"24":"AO",
"660":"AI",
"28":"AG",
"32":"AR",
"51":"AM",
"533":"AW",
"36":"AU",
"40":"AT",
"31":"AZ",
"44":"BS",
"48":"BH",
"50":"BD",
"52":"BB",
"112":"BY",
"56":"BE",
"84":"BZ",
"204":"BJ",
"60":"BM",
"64":"BT",
"68":"BO",
"535":"BQ",
"70":"BA",
"72":"BW",
"74":"BV",
"76":"BR",
"86":"IO",
"96":"BN",
"100":"BG",
"854":"BF",
"108":"BI",
"116":"KH",
"120":"CM",
"124":"CA",
"132":"CV",
"136":"KY",
"140":"CF",
"148":"TD",
"152":"CL",
"156":"CN",
"162":"CX",
"166":"CC",
"170":"CO",
"174":"KM",
"178":"CG",
"180":"CD",
"184":"CK",
"188":"CR",
"384":"CI",
"191":"HR",
"192":"CU",
"531":"CW",
"196":"CY",
"203":"CZ",
"208":"DK",
"262":"DJ",
"212":"DM",
"214":"DO",
"218":"EC",
"818":"EG",
"222":"SV",
"226":"GQ",
"232":"ER",
"233":"EE",
"231":"ET",
"238":"FK",
"234":"FO",
"242":"FJ",
"246":"FI",
"250":"FR",
"254":"GF",
"258":"PF",
"260":"TF",
"266":"GA",
"270":"GM",
"268":"GE",
"276":"DE",
"288":"GH",
"292":"GI",
"300":"GR",
"304":"GL",
"308":"GD",
"312":"GP",
"316":"GU",
"320":"GT",
"831":"GG",
"324":"GN",
"624":"GW",
"328":"GY",
"332":"HT",
"334":"HM",
"336":"VA",
"340":"HN",
"344":"HK",
"348":"HU",
"352":"IS",
"356":"IN",
"360":"ID",
"364":"IR",
"368":"IQ",
"372":"IE",
"833":"IM",
"376":"IL",
"380":"IT",
"388":"JM",
"392":"JP",
"832":"JE",
"400":"JO",
"398":"KZ",
"404":"KE",
"296":"KI",
"408":"KP",
"410":"KR",
"414":"KW",
"417":"KG",
"418":"LA",
"428":"LV",
"422":"LB",
"426":"LS",
"430":"LR",
"434":"LY",
"438":"LI",
"440":"LT",
"442":"LU",
"446":"MO",
"807":"MK",
"450":"MG",
"454":"MW",
"458":"MY",
"462":"MV",
"466":"ML",
"470":"MT",
"584":"MH",
"474":"MQ",
"478":"MR",
"480":"MU",
"175":"YT",
"484":"MX",
"583":"FM",
"498":"MD",
"492":"MC",
"496":"MN",
"499":"ME",
"500":"MS",
"504":"MA",
"508":"MZ",
"104":"MM",
"516":"NA",
"520":"NR",
"524":"NP",
"528":"NL",
"540":"NC",
"554":"NZ",
"558":"NI",
"562":"NE",
"566":"NG",
"570":"NU",
"574":"NF",
"580":"MP",
"578":"NO",
"512":"OM",
"586":"PK",
"585":"PW",
"275":"PS",
"591":"PA",
"598":"PG",
"600":"PY",
"604":"PE",
"608":"PH",
"612":"PN",
"616":"PL",
"620":"PT",
"630":"PR",
"634":"QA",
"638":"RE",
"642":"RO",
"643":"RU",
"646":"RW",
"652":"BL",
"654":"SH",
"659":"KN",
"662":"LC",
"663":"MF",
"666":"PM",
"670":"VC",
"882":"WS",
"674":"SM",
"678":"ST",
"682":"SA",
"686":"SN",
"688":"RS",
"690":"SC",
"694":"SL",
"702":"SG",
"534":"SX",
"703":"SK",
"705":"SI",
"90":"SB",
"706":"SO",
"710":"ZA",
"239":"GS",
"724":"ES",
"144":"LK",
"736":"SD",
"740":"SR",
"744":"SJ",
"748":"SZ",
"752":"SE",
"756":"CH",
"760":"SY",
"158":"TW",
"762":"TJ",
"834":"TZ",
"764":"TH",
"626":"TL",
"768":"TG",
"772":"TK",
"776":"TO",
"780":"TT",
"788":"TN",
"792":"TR",
"795":"TM",
"796":"TC",
"798":"TV",
"800":"UG",
"804":"UA",
"784":"AE",
"826":"GB",
"840":"US",
"581":"UM",
"858":"UY",
"860":"UZ",
"548":"VU",
"862":"VE",
"704":"VN",
"92":"VG",
"850":"VI",
"876":"WF",
"732":"EH",
"887":"YE",
"894":"ZM",
"716":"ZW"
}

0 comments on commit 7901ec1

Please sign in to comment.