import discord import json import requests import asyncio from settings import * global loop loop = asyncio.get_event_loop() async def backgroundTask(client): while not client.is_closed: await client.wait_until_ready() await showPSO2EQ(client) await changeGame(client) await asyncio.sleep(30) # Task runs every 30 seconds async def changeGame(client): for game in games: await client.change_status(discord.Game(name=game), idle=False) await asyncio.sleep(30) async def showPSO2EQ(client): # Async shit future1 = loop.run_in_executor(None, requests.get, 'http://pso2emq.flyergo.eu/api/v2/') r = await future1 # Loads EQ data r2 = json.loads(r.text) eq = r2[0]['text'].splitlines() eqtime = r2[0]['jst'] eqs = [] i = 0 # Adds EQ data to eqs and formats them properly EqAtThisHour = 'true' for line in eq: if 'Emergency Quest' not in line and line != 'Ship%02d: -' % i and line.startswith('Ship'): line = '``' + line.replace(':', ':``') line = line.replace('Ship', 'SHIP ') eqs.append(line) if line == 'All ships are in event preparation.': eqs.append('``' + line + '``') if 'no emergency quest' in line: EqAtThisHour = 'false' if line.startswith('[In Progress]'): line = line.replace('[In Progress]', '``IN PROGRESS:``') eqs.append(line) if line.startswith('[In Preparation]'): line = line.replace('[In Preparation]', '``IN 1 HOUR:``') eqs.append(line) if line.startswith('[1 hour later]'): line = line.replace('[1 hour later]', '``IN 2 HOURS:``') eqs.append(line) if line.startswith('[2 hours later]'): line = line.replace('[2 hours later]', '``IN 3 HOURS:``') eqs.append(line) i = i + 1 # Loads last_eq.json with open('json/last_eq.json', encoding="utf8") as in_f: last_eq = json.load(in_f) # If current EQ is different than last EQ recorded, send alert and update last_eq file with open('json/eq_channels.json', encoding="utf8") as eq_channels: eq_channels = json.load(eq_channels) string = '\n'.join(eqs) message = ':mega: **%s JST Emergency Quest Notice**\n\n%s' % (eqtime, string) if last_eq['jst'] != eqtime: if EqAtThisHour == 'false': pass else: for item in eq_channels['channels']: if client.get_channel(item): channel = client.get_channel(item) try: await client.send_message(discord.Object(item), message) except: print('Something went wrong when sending a message to "{}"'.format(channel.server.name)) if client.get_channel(test_channel): await client.send_message(discord.Object(test_channel), 'EQ Alert sent to: ``%s`` (%s)' % ( channel.server.name, channel.server.id)) else: msg = ':mega: **Alert!**\n Channel %s does not exist. Removing...' % item if client.get_channel(test_channel): await client.send_message(discord.Object(test_channel), msg) await removeEQChannel(item) if client.get_channel(test_channel): await client.send_message(discord.Object(test_channel), '-------------------') with open('json/last_eq.json', 'w') as file: json.dump(r2[0], file) async def showLastEQ(client, message): eqs = [] with open('json/last_eq.json', 'r') as file: eq = json.load(file) eqtime = eq['jst'] i = 0 # Adds EQ data to eqs and formats them properly for line in eq['text'].splitlines(): if 'Emergency Quest' not in line and line != 'Ship%02d: -' % i and line.startswith('Ship'): line = '``' + line.replace(':', ':``') line = line.replace('Ship', 'SHIP ') eqs.append(line) if line == 'All ships are in event preparation.': eqs.append('``' + line + '``') if line.startswith('[In Progress]'): line = line.replace('[In Progress]', '``IN PROGRESS:``') eqs.append(line) if line.startswith('[In Preparation]'): line = line.replace('[In Preparation]', '``IN 1 HOUR:``') eqs.append(line) if line.startswith('[1 hour later]'): line = line.replace('[1 hour later]', '``IN 2 HOURS:``') eqs.append(line) if line.startswith('[2 hours later]'): line = line.replace('[2 hours later]', '``IN 3 HOURS:``') eqs.append(line) if line == "1 hour later is maintenance.": line = line.replace('1 hour later is maintenance.', 'NotLikeThis ``M A I N T E N A N C E`` NotLikeThis') eqs.append(line) i = i + 1 try: string = '\n'.join(eqs) lasteq = ':mega: **%02d JST Emergency Quest Notice**\n\n%s' % (eqtime, string) await client.send_message(message.channel, lasteq) except: pass async def addEQChannel(message, client): # Loads eq_channels.json file with open('json/eq_channels.json', encoding="utf8") as eq_channels: eq_channels = json.load(eq_channels) if message.channel.id not in eq_channels['channels']: # Writes channel ID to file with open('json/eq_channels.json', 'w') as outfile: eq_channels['channels'].append(message.channel.id) json.dump(eq_channels, outfile) await client.send_message(message.channel, "EQ Alerts successfully enabled on this channel.") else: await client.send_message(message.channel, 'EQ Alerts are already enabled on this channel.') async def removeEQChannel(id): # Loads eq_channels.json file with open('json/eq_channels.json', encoding="utf8") as eq_channels: eq_channels = json.load(eq_channels) if id in eq_channels['channels']: eq_channels['channels'].remove(id) # Writes channel ID to file with open('json/eq_channels.json', 'w') as outfile: json.dump(eq_channels, outfile)