From 7901ec1bc5cf8cc3fbb23fd5283f346e3578731c Mon Sep 17 00:00:00 2001 From: 12pm <30786226+12pm@users.noreply.github.com> Date: Tue, 9 Oct 2018 13:17:30 +0200 Subject: [PATCH] Add twstatus --- bot.py | 5 +- cogs/twstatus.py | 99 +++++++++ requirements.txt | 13 +- tw-status/countryflags.json | 257 ++++++++++++++++++++++++ tw-status/info | 388 ++++++++++++++++++++++++++++++++++++ 5 files changed, 754 insertions(+), 8 deletions(-) create mode 100644 cogs/twstatus.py create mode 100644 tw-status/countryflags.json create mode 100644 tw-status/info diff --git a/bot.py b/bot.py index 0cc140a..cec0817 100644 --- a/bot.py +++ b/bot.py @@ -39,7 +39,8 @@ def setup_logging(): 'cogs.profilecard', 'cogs.testing_main', 'cogs.testing_archiving', - 'cogs.testing_moderation' + 'cogs.testing_moderation', + 'cogs.twstatus' ] @@ -91,4 +92,4 @@ def run(self): bot = DDNet() with setup_logging(): - bot.run() + bot.run() \ No newline at end of file diff --git a/cogs/twstatus.py b/cogs/twstatus.py new file mode 100644 index 0000000..c76a91f --- /dev/null +++ b/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)) diff --git a/requirements.txt b/requirements.txt index ddd0718..29dc2ff 100644 --- a/requirements.txt +++ b/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 \ No newline at end of file diff --git a/tw-status/countryflags.json b/tw-status/countryflags.json new file mode 100644 index 0000000..8f74ba2 --- /dev/null +++ b/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" + } \ No newline at end of file diff --git a/tw-status/info b/tw-status/info new file mode 100644 index 0000000..fdc4e2b --- /dev/null +++ b/tw-status/info @@ -0,0 +1,388 @@ +{ + "news": "|Teeworlds Auto-Font by bit|\nToday bit presented a new tool for mappers to easily create font mapres.\nIt is now available on DDNet.tw/auto-font\n\n|Buggy Tiles in DDNet Server 11.4 and 11.4.1|\nOn August 21 DDNet 11.4 was released with a server bug that made some tiles\nhave no effect. As of 22:50 on August 23 the official servers were rebooted\nwith the new version 11.4.1 (skipping 11.4) that had the same bug. The effect\nwas that players since then could finish maps much easier and as we saw on\nDiscord this was widely abused immediately.\n\nThis morning (August 24) at 09:40 we fixed the bug and released DDNet 11.4.2\nwith a new version and immediately force-restarted all servers. Since this bug\nmade most maps significantly easier to finish all ranks and saves made between\n2018-08-23 22:50 CEST and 2018-08-24 09:40 CEST had to be deleted.\n\nIf you are running your own server and updated it to DDNet 11.4 or 11.4.1\nalready, make sure to update the server immediately and remove affected ranks\nand saves.\n\nSorry for the inconvenience.\n", + "version": "11.4.4", + "servers": [ + { + "flagId": 276, + "name": "GER", + "servers": { + "gCTF": [ + "95.172.92.151:8108" + ], + "DDNet": [ + "95.172.92.151:8303", + "95.172.92.151:8304", + "95.172.92.151:8305", + "95.172.92.151:8306", + "95.172.92.151:8308", + "95.172.92.151:8309", + "95.172.92.151:8311", + "95.172.92.151:8312", + "95.172.92.151:8313", + "95.172.92.151:8314", + "95.172.92.151:8316", + "95.172.92.151:8317", + "95.172.92.151:8318", + "95.172.92.151:8319", + "95.172.92.151:8320", + "95.172.92.151:8321", + "95.172.92.151:8322", + "95.172.92.151:8323", + "95.172.92.151:8324", + "95.172.92.151:8326", + "95.172.92.151:8327", + "95.172.92.151:8328", + "95.172.92.151:8330", + "95.172.92.151:8331", + "95.172.92.151:8332", + "95.172.92.151:8333", + "95.172.92.151:8334", + "95.172.92.151:8335", + "95.172.92.151:8336", + "95.172.92.151:8337", + "95.172.92.151:8338", + "95.172.92.151:8339", + "95.172.92.151:8302" + ], + "Vanilla": [ + "95.172.92.151:8141", + "95.172.92.151:8142" + ], + "Infection": [ + "95.172.92.151:8200", + "95.172.92.151:8201" + ], + "FNG": [ + "95.172.92.151:8220" + ], + "iCTF": [ + "95.172.92.151:8101", + "95.172.92.151:8102", + "95.172.92.151:8105", + "95.172.92.151:8106" + ], + "Test": [ + "95.172.92.151:8001", + "95.172.92.151:8002", + "95.172.92.151:8003", + "95.172.92.151:8004", + "31.186.251.128:8325", + "31.186.251.128:8328", + "31.186.251.128:8329", + "31.186.251.128:8331" + ], + "zCatch": [ + "95.172.92.151:8400", + "95.172.92.151:8401", + "95.172.92.151:8402", + "95.172.92.151:8403" + ], + "Block": [ + "95.172.92.151:8202" + ] + } + }, + { + "flagId": 643, + "name": "RUS", + "servers": { + "DDNet": [ + "194.67.208.6:8303", + "194.67.208.6:8304", + "194.67.208.6:8305", + "194.67.208.6:8306", + "194.67.208.6:8308", + "194.67.208.6:8309", + "194.67.208.6:8311", + "194.67.208.6:8312", + "194.67.208.6:8313", + "194.67.208.6:8314", + "194.67.208.6:8316", + "194.67.208.6:8317", + "194.67.208.6:8318", + "194.67.208.6:8319", + "194.67.208.6:8320", + "194.67.208.6:8321", + "194.67.208.6:8322", + "194.67.208.6:8323", + "194.67.208.6:8324", + "194.67.208.6:8326", + "194.67.208.6:8327" + ], + "Monster": [ + "194.67.208.6:8250" + ], + "Vanilla": [ + "194.67.208.6:8141" + ], + "FNG": [ + "194.67.208.6:8220" + ], + "Test": [ + "194.67.208.6:8307" + ], + "Block": [ + "194.67.208.6:8203", + "194.67.208.6:8204" + ] + } + }, + { + "flagId": 152, + "name": "CHL", + "servers": { + "Bomb": [ + "131.221.32.165:8299" + ], + "Vanilla": [ + "131.221.32.165:8241", + "131.221.32.165:8242" + ], + "FNG": [ + "131.221.32.165:8220" + ], + "Test": [ + "131.221.32.165:8307", + "131.221.32.165:8310" + ], + "iCTF": [ + "131.221.32.165:8101", + "131.221.32.165:8102" + ], + "Foot": [ + "131.221.32.165:8000", + "131.221.32.165:8001" + ], + "DDNet": [ + "131.221.32.165:8303", + "131.221.32.165:8304", + "131.221.32.165:8305", + "131.221.32.165:8306", + "131.221.32.165:8308", + "131.221.32.165:8309", + "131.221.32.165:8311", + "131.221.32.165:8312", + "131.221.32.165:8313", + "131.221.32.165:8314", + "131.221.32.165:8316", + "131.221.32.165:8317", + "131.221.32.165:8321", + "131.221.32.165:8322", + "131.221.32.165:8323", + "131.221.32.165:8324", + "131.221.32.165:8326" + ], + "zCatch": [ + "131.221.32.165:8210", + "131.221.32.165:8211", + "131.221.32.165:8212" + ], + "Block": [ + "131.221.32.165:8203", + "131.221.32.165:8204" + ] + } + }, + { + "flagId": 76, + "name": "BRA", + "servers": { + "Bomb": [ + "191.96.4.249:8299" + ], + "Vanilla": [ + "191.96.4.249:8241", + "191.96.4.249:8242" + ], + "FNG": [ + "191.96.4.249:8220" + ], + "iCTF": [ + "191.96.4.249:8101", + "191.96.4.249:8102" + ], + "Foot": [ + "191.96.4.249:8000", + "191.96.4.249:8001" + ], + "DDNet": [ + "191.96.4.249:8303", + "191.96.4.249:8304", + "191.96.4.249:8305", + "191.96.4.249:8306", + "191.96.4.249:8308", + "191.96.4.249:8309", + "191.96.4.249:8311", + "191.96.4.249:8312", + "191.96.4.249:8317", + "191.96.4.249:8321" + ], + "zCatch": [ + "191.96.4.249:8210", + "191.96.4.249:8211", + "191.96.4.249:8212" + ], + "Block": [ + "191.96.4.249:8203", + "191.96.4.249:8204" + ] + } + }, + { + "flagId": 710, + "name": "ZAF", + "servers": { + "DDNet": [ + "129.232.182.213:8303", + "129.232.182.213:8304", + "129.232.182.213:8305", + "129.232.182.213:8306", + "129.232.182.213:8308", + "129.232.182.213:8309", + "129.232.182.213:8310", + "129.232.182.213:8311", + "129.232.182.213:8317", + "129.232.182.213:8321", + "129.232.182.213:8322", + "129.232.182.213:8323", + "129.232.182.213:8324", + "129.232.182.213:8325" + ], + "Vanilla": [ + "129.232.182.213:8141", + "129.232.182.213:8142" + ], + "Infection": [ + "129.232.182.213:8200", + "129.232.182.213:8201" + ], + "FNG": [ + "129.232.182.213:8220" + ], + "Foot": [ + "129.232.182.213:8000" + ], + "iCTF": [ + "129.232.182.213:8101", + "129.232.182.213:8102" + ], + "Test": [ + "129.232.182.213:8307", + "129.232.182.213:8312" + ], + "zCatch": [ + "129.232.182.213:8210", + "129.232.182.213:8211" + ], + "Block": [ + "129.232.182.213:8203" + ] + } + }, + { + "flagId": 840, + "name": "USA", + "servers": { + "DDNet": [ + "74.91.114.132:8303", + "74.91.114.132:8304", + "74.91.114.132:8305", + "74.91.114.132:8306", + "74.91.114.132:8317", + "74.91.114.132:8321" + ], + "Monster": [ + "74.91.114.132:8250" + ], + "Vanilla": [ + "74.91.114.132:8241", + "74.91.114.132:8242" + ], + "Infection": [ + "74.91.114.132:8200", + "74.91.114.132:8201" + ], + "FNG": [ + "74.91.114.132:8220" + ], + "Foot": [ + "74.91.114.132:8000" + ], + "iCTF": [ + "74.91.114.132:8101", + "74.91.114.132:8102", + "74.91.114.132:8103", + "74.91.114.132:8104" + ], + "Test": [ + "74.91.114.132:8307", + "74.91.114.132:8310" + ], + "Bomb": [ + "74.91.114.132:8299" + ], + "zCatch": [ + "74.91.114.132:8210", + "74.91.114.132:8211", + "74.91.114.132:8212" + ], + "Block": [ + "74.91.114.132:8203", + "74.91.114.132:8204", + "74.91.114.132:8205" + ] + } + }, + { + "flagId": 124, + "name": "CAN", + "servers": { + "DDNet": [ + "158.69.65.251:8303", + "158.69.65.251:8304", + "158.69.65.251:8305", + "158.69.65.251:8306", + "158.69.65.251:8317", + "158.69.65.251:8321" + ], + "Vanilla": [ + "158.69.65.251:8241" + ], + "Infection": [ + "158.69.65.251:8200", + "158.69.65.251:8201" + ], + "FNG": [ + "158.69.65.251:8220" + ], + "iCTF": [ + "158.69.65.251:8101" + ], + "zCatch": [ + "158.69.65.251:8210" + ], + "Block": [ + "158.69.65.251:8203" + ] + } + }, + { + "flagId": 156, + "name": "CHN", + "servers": { + "DDNet": [ + "119.29.57.22:7303", + "119.29.57.22:7304", + "119.29.57.22:7305", + "119.29.57.22:7306", + "119.29.57.22:7317", + "119.29.57.22:7321" + ], + "zCatch": [ + "119.29.57.22:7400", + "119.29.57.22:7401" + ] + } + } + ] +} \ No newline at end of file