viewing paste Unknown #29288 | Python

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
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(60)
 
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)
 
Viewed 789 times, submitted by Guest.