Commit 3bcd2619 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

Initial commit

parents
import os
import urllib.request
import discord
import re
import pprint
import requests
import platform
from discord.ext import tasks, commands
from discord.ext.commands import Bot
# line=b'* 0 0 0 37 1E9C4 0 MG-on\n'
# line=b'0 10E1 "POLAND vs Mystery" B180D21 12C 691 91 "" "0/1 -- •15:30" 900 2 42 6178368D 108.20.79.156 10E1 "Kate Kirby" B180B21 96 3B9 9 "test msg" "..." 0 16 32 61780D39 108.20.79.156 10E1 "Mario" B180B21 96 3B9 9 "test msg" "..." 0 16 32 61780D39 * 0 0 0 37 1E9C4 0 MG-on\n'
# line=b'0 10E1 "POLAND vs Mystery" B180D21 12C 691 91 "" "0/2 -- 30:30•" 900 2 42 6178368D 108.20.79.156 10E1 "Kate Kirby" B180B21 96 3B9 9 "test msg" "..." 0 16 32 6178382F * 1 0 0 C 1E9D4 0 MG-on\n'
# line=line.decode('utf-8')
reGame = re.compile(''
'\s*([0-9.]+)'
'\s+([0-9a-fA-F]+)'
'\s+"(.*?)"'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+"(.*?)"'
'\s+"(.*?)"'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s*',
re.MULTILINE)
reSummary = re.compile('^'
'\s*(.*)\*'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+MG\-on.*$',
re.MULTILINE)
oldGames=[]
newGames=[]
def parse_server_poll(data):
print(data)
m = reSummary.match(data)
(listing, summary) = (m.groups()[0], m.groups()[1:])
m = reGame.findall(listing)
r = {'games':
[{
'ip': a[0],
'port': int(a[1], 16),
'name': a[2],
# 'game_info': [{
# 'trial': x>>0 & 1<<2-1,
# 'player_cfg': x>>2 & 1<<3-1,
# 'nb_set': x>>5 & 1<<2-1,
# 'skill_mode': x>>7 & 1<<2-1,
# 'court': x>>9 & 1<<9-1,
# 'game_per_set': x>>18 & 1<<3-1,
# 'unused': x>>21 & 1<<1-1,
# 'control_mode': x>>22 & 1<<2-1,
# 'preview': x>>24 & 1<<3-1,
# 'tiredness': x>>27 & 1<<1-1,
# } for x in [int(a[3], 16)]][0],
'game_info': [{
'tiredness': (x>>27) & ((1<<1)-1),
'preview': (x>>24) & ((1<<3)-1),
'control_mode': (x>>22) & ((1<<2)-1),
'unused': (x>>21) & ((1<<1)-1),
'game_per_set': (x>>18) & ((1<<3)-1),
'court': (x>>9) & ((1<<9)-1),
'skill_mode': (x>>7) & ((1<<2)-1),
'nb_set': (x>>5) & ((1<<2)-1),
'player_cfg': (x>>2) & ((1<<3)-1),
'trial': (x>>0) & ((1<<2)-1),
} for x in [int(a[3], 16)]][0],
'max_ping': int(a[4], 16),
'elo': int(a[5], 16),
'nb_game': int(a[6], 16),
'tagline': a[7],
'score': a[8],
'other_elo': int(a[9], 16),
'give_up_rate': int(a[10], 16),
'reputation': int(a[11], 16),
'creation_time': int(a[12], 16),
} for a in m],
'summary': summary}
return(r)
# pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(r)
# print(parse_server_poll(line))
#stats = [0,0,0]
TOKEN = open("token.txt", "r").readline()
intents = discord.Intents.default()
intents.members = False
intents.typing = False
intents.presences = False
intents.messages = True
ch = None
# client = discord.Client(intents = intents)
# client = commands.Bot(command_prefix='!')
bot = Bot(command_prefix='!', intents=intents)
# The code in this even is executed when the bot is ready
@bot.event
async def on_ready():
global ch
print(f"Logged in as {bot.user.name}")
print(f"Discord.py API version: {discord.__version__}")
print(f"Python version: {platform.python_version()}")
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
print("-------------------")
channels = [c for g in bot.guilds for c in g.text_channels] #getting all the text channels bot can see
ch = discord.utils.get(channels, name="find-a-game")
# await ch.send("hello world!")
status_task.start()
# Setup the game status task of the bot
@tasks.loop(seconds=15.0)
async def status_task():
global oldGames, newGames
data = requests.get("https://www.managames.com/tennis/online/TE4_ServerList.php?Poll=1")
# print(data.text)
# print('-----')
info = parse_server_poll(data.text.replace('\n',' '))
# info2 = parse_server_poll(data.content.decode('utf-8'))
# pprint.pprint(info2)
newGames = [x for x in info['games'] if x not in oldGames]
if len(newGames)>0:
out=[]
for game in newGames:
if game['ip']=='0':
s = f"{game['name']} started their match."
else:
s = f"{game['name']} (elo: {game['elo']}, rep: {game['reputation']}) wants to play a {game['game_info']['nb_set']}-set match. Comment: {game['tagline']}"
out += [s]
pprint.pprint(out)
await ch.send("\n".join(out))
oldGames=newGames
# else:
# await ch.send("No current games found.")
# else:
# out = [info['summary']]
# await bot.change_presence(activity=discord.Game(random.choice(statuses)))
# @client.event
# async def on_ready():
# print('Logged in as {0.user}'.format(client))
# # channel = discord.utils.get(client.guilds[0].text_channels, name="General")
# channels = [c for g in client.guilds for c in g.text_channels] #getting all the text channels bot can see
# ch = discord.utils.get(channels, name="general")
# await ch.send("hello world!")
# @client.event
# async def on_message(message):
# if message.author == client.user:
# return
# if message.content.startswith('!games'):
# # data = urllib.request.urlopen("https://www.managames.com/tennis/online/TE4_ServerList.php?Poll=1").read()
# # data = data.decode('utf-8')
# data = requests.get("https://www.managames.com/tennis/online/TE4_ServerList.php?Poll=1")
# print(data.text)
# print('-----')
# info = parse_server_poll(data.text.replace('\n',' '))
# pprint.pprint(info)
# # info2 = parse_server_poll(data.content.decode('utf-8'))
# # pprint.pprint(info2)
# if len(info['games'])>0:
# out = [pprint.pformat(x) for x in info['games']]
# print(out)
# await message.channel.send("\n".join(out))
# else:
# await message.channel.send("No current games found.")
# # else:
# # out = [info['summary']]
bot.run(TOKEN)
# client.run(TOKEN)
# 108.20.79.156 10E1 "Kate Kirby" B180B21 96 3B9 9 "test msg" "..." 0 16 32 61780D39 * 0 0 0 37 1E9C4 0 MG-on
# * 0 0 0 37 1E9C4 0 MG-on
# For each server, you'll see this :
# Ip Port "Name" GameInfo MaxPing Elo NbGame "TagLine" "Score" OtherElo GiveUpRate Reputation CreationTimeInMs
# Ip is 0 if the game is already started.
# All numbers are hexadecimal.
# GameInfo is a bitfield like this :
# - Trial : 2 bits
# - PlayerCfg : 3 ; 0 => singles ; 2 => Vs doubles ; 3 => Coop doubles (IIRC)
# - NbSet : 2
# - SkillMode : 2
# - Court : 9 (will eventually be changed to 2 string, 1 for stadium & 1 for the surface)
# - GamePerSet : 3
# - <unused> : 1
# - ControlMode : 2
# - Preview : 3
# - Tiredness : 1
# {
# 'trial': x>>0 & 1<<2-1,
# 'player_cfg': x>>2 & 1<<3-1,
# 'nb_set': x>>5 & 1<<2-1,
# 'skill_mode': x>>7 & 1<<2-1,
# 'court': x>>9 & 1<<9-1,
# 'game_per_set': x>>18 & 1<<3-1,
# 'unused': x>>21 & 1<<1-1,
# 'control_mode': x>>22 & 1<<2-1,
# 'preview': x>>24 & 1<<3-1,
# 'tiredness': x>>27 & 1<<1-1,
# }
# {
# 'tiredness': x & 1,
# 'preview': x>>1 & 1<<3-1,
# 'control_mode': x>>4 & 1<<2-1,
# 'unused': x>>5 & 1<<1-1,
# 'game_per_set': x>>6 & 1<<3-1,
# 'court': x>>9 & 1<<9-1,
# 'skill_mode': x>>18 & 1<<2-1,
# 'nb_set': x>>20 & 1<<2-1,
# 'player_cfg': x>>22 & 1<<3-1,
# 'trial': x>>25 & 1<<2-1,
# }
# bot = commands.Bot(command_prefix='!')
# initial_extensions = ['cogs.mycog'] #cog.(filename)
# if __name__ == '__main__':
# for extension in initial_extensions:
# bot.load_extension(extension)
# @client.event
# async def on_ready():
# print('Bot is ready')
# bot.run(TOKEN)
\ No newline at end of file
import urllib.request
import discord
from discord.ext import tasks, commands
#stats = [0,0,0]
#class MyCog(commands.Cog):
#
# def __init__(self):
# self.stats = [0,0,0]
# self.updater.start()
#
# def cog_unload(self):
# self.updater.cancel()
#
# @tasks.loop(seconds=60.0)
# async def updater(self):
# contents = urllib.request.urlopen("https://www.managames.com/tennis/online/TE4_NbOnlinePlayers.php").read()
# [a,b,c]=contents.split(b"\n")[0].split(b" ")
# [a,b,c]=[int(a),int(b),int(c)]
# self.stats = [a,b,c]
# stats = [a,b,c]
TOKEN = open("token.txt", "r").readline()
intents = discord.Intents.default()
intents.members = False
intents.typing = False
intents.presences = False
intents.messages = True
client = discord.Client(intents = intents)
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!games'):
contents = urllib.request.urlopen("https://www.managames.com/tennis/online/TE4_NbOnlinePlayers.php").read()
[a,b,c]=contents.split(b"\n")[0].split(b" ")
stats=[int(a),int(b),int(c)]
print(stats)
await message.channel.send('{} players online; {} games running; {} games waiting.'.format(*stats))
def setup(client):
client.add_cog(MyCog(client))
client.run(TOKEN)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment