1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/main.py

118 lines
5 KiB
Python
Raw Normal View History

2016-08-12 13:55:59 +00:00
import asyncio
import discord
2016-08-12 15:23:45 +00:00
import json
import overwatch
2016-08-12 20:53:12 +00:00
import league
2016-08-12 19:08:36 +00:00
import strings as s
2016-08-13 12:19:41 +00:00
import telegram
2016-08-12 15:48:35 +00:00
loop = asyncio.get_event_loop()
2016-08-12 15:23:45 +00:00
d_client = discord.Client()
2016-08-13 13:36:08 +00:00
discord_is_ready = False
# When Discord is ready, set discord_is_ready to True
@d_client.event
async def on_ready():
global discord_is_ready
discord_is_ready = True
2016-08-12 15:23:45 +00:00
# Get player database from the db.json file
file = open("db.json")
db = json.load(file)
file.close()
2016-08-12 13:55:59 +00:00
2016-08-12 14:01:05 +00:00
# Get the discord bot token from "discordtoken.txt"
2016-08-12 19:08:36 +00:00
file = open("discordtoken.txt", "r")
token = file.read()
file.close()
2016-08-12 15:23:45 +00:00
2016-08-12 20:53:12 +00:00
# Every timeout seconds, update player status and check for levelups
2016-08-12 17:21:43 +00:00
async def overwatch_level_up(timeout):
while True:
2016-08-13 13:36:08 +00:00
if discord_is_ready:
2016-08-13 13:52:11 +00:00
print("[Overwatch] Starting check...")
2016-08-13 13:36:08 +00:00
# Update data for every player in list
for player in db:
if "overwatch" in db[player]:
r = await overwatch.get_player_data(**db[player]["overwatch"])
if r["data"]["level"] > db[player]["overwatch"]["level"]:
2016-08-12 20:53:12 +00:00
# Convert user ID into a mention
user = "<@" + player + ">"
# Prepare the message to send
2016-08-13 13:36:08 +00:00
msg = s.overwatch_level_up.format(player=user, level=r["data"]["level"])
2016-08-12 20:53:12 +00:00
# Send the message to the discord channel
loop.create_task(d_client.send_message(d_client.get_channel("213655027842154508"), msg))
2016-08-13 12:19:41 +00:00
# Send the message to the telegram group chat
loop.create_task(telegram.send_message(msg, -2141322))
2016-08-12 20:53:12 +00:00
# Update database
2016-08-13 13:36:08 +00:00
db[player]["overwatch"]["level"] = r["data"]["level"]
2016-08-12 20:53:12 +00:00
f = open("db.json", "w")
json.dump(db, f)
f.close()
2016-08-13 13:52:11 +00:00
print("[Overwatch] Check completed successfully.")
2016-08-13 13:36:08 +00:00
# Wait for the timeout
await asyncio.sleep(timeout)
else:
await asyncio.sleep(1)
# Every timeout seconds, update player league and check for rank changes
async def league_rank_change(timeout):
while True:
if discord_is_ready:
2016-08-13 13:52:11 +00:00
print("[League] Starting check...")
2016-08-13 13:36:08 +00:00
# Update data for every player in list
for player in db:
if "league" in db[player]:
try:
r = await league.get_player_rank(**db[player]["league"])
except league.NoRankedGamesCompletedException:
# If the player has no ranked games completed, skip him
continue
2016-08-13 13:56:27 +00:00
except league.RateLimitException:
# If you've been ratelimited, skip the player and notify the console.
print("[League] Request rejected for rate limit.")
2016-08-13 13:36:08 +00:00
else:
# Convert tier into a number
tier_number = league.ranklist.index(r["tier"])
roman_number = league.roman.index(r["entries"][0]["division"])
# Check for tier changes
if tier_number != db[player]["league"]["tier"] or roman_number != db[player]["league"]["division"]:
# Convert user ID into a mention
user = "<@" + player + ">"
# Prepare the message to send
msg = s.league_rank_up.format(player=user,
tier=s.league_tier_list[tier_number],
division=r["entries"][0]["division"])
# Send the message to the discord channel
loop.create_task(d_client.send_message(d_client.get_channel("213655027842154508"), msg))
# Send the message to the telegram group chat
loop.create_task(telegram.send_message(msg, -2141322))
# Update database
db[player]["league"]["tier"] = tier_number
db[player]["league"]["division"] = roman_number
f = open("db.json", "w")
json.dump(db, f)
f.close()
2016-08-13 13:52:11 +00:00
# Prevent getting ratelimited by Riot
await asyncio.sleep(1)
print("[League] Check completed.")
2016-08-13 13:36:08 +00:00
# Wait for the timeout
await asyncio.sleep(timeout)
else:
await asyncio.sleep(1)
2016-08-12 17:21:43 +00:00
2016-08-12 19:25:03 +00:00
loop.create_task(overwatch_level_up(900))
2016-08-13 13:52:11 +00:00
print("[Overwatch] Added level up check to the queue.")
2016-08-12 21:32:12 +00:00
loop.create_task(league_rank_change(900))
2016-08-13 13:52:11 +00:00
print("[League] Added rank change check to the queue.")
2016-08-12 20:53:12 +00:00
try:
loop.run_until_complete(d_client.start(token))
except KeyboardInterrupt:
loop.run_until_complete(d_client.logout())
# cancel all tasks lingering
finally:
loop.close()