예제 #1
0
    async def job_queue(self):
        """Updates Alpha Bot user status with latest prices

		"""

        while True:
            try:
                await asyncio.sleep(Utils.seconds_until_cycle())
                if not await self.guildProperties.check_status(): continue
                t = datetime.datetime.now().astimezone(pytz.utc)
                timeframes = Utils.get_accepted_timeframes(t)

                isPremium = self.tickerId in [
                    "EURUSD", "GBPUSD", "AUDJPY", "AUDUSD", "EURJPY", "GBPJPY",
                    "NZDJPY", "NZDUSD", "CADUSD", "JPYUSD", "ZARUSD"
                ]
                refreshRate = "5m" if len(client.guilds) > 1 and (
                    not isPremium or len(client.guilds) > 15) else "15m"

                if "1m" in timeframes:
                    self.get_assigned_id()
                if refreshRate in timeframes:
                    await asyncio.sleep(self.timeOffset)

                    try:
                        outputMessage, request = Processor.process_quote_arguments(
                            client.user.id,
                            [] if self.exchange is None else [self.exchange],
                            tickerId=self.tickerId,
                            platformQueue=[self.platform])
                    except:
                        continue
                    if outputMessage is not None:
                        print(outputMessage)
                        if os.environ["PRODUCTION_MODE"]:
                            self.logging.report(outputMessage)
                        continue

                    try:
                        payload, quoteText = await Processor.execute_data_server_request(
                            "quote", request, timeout=30)
                    except:
                        continue
                    if payload is None or payload["quotePrice"] is None:
                        print("Requested price for `{}` is not available".
                              format(request.get_ticker().name
                                     ) if quoteText is None else quoteText)
                        continue

                    self.priceText = "{} {}".format(payload["quotePrice"],
                                                    payload["quoteTicker"])
                    changeText = "" if payload[
                        "change"] is None else "{:+.2f} % | ".format(
                            payload["change"])
                    tickerText = "{} | ".format(
                        request.get_ticker().id) if request.get_exchange(
                        ) is None else "{} on {} | ".format(
                            request.get_ticker().id,
                            request.get_exchange().name)
                    statusText = "{}{}alphabotsystem.com".format(
                        changeText, tickerText)
                    status = discord.Status.online if payload[
                        "change"] is None or payload[
                            "change"] >= 0 else discord.Status.dnd

                    for guild in client.guilds:
                        properties = await self.guildProperties.get(guild.id)
                        if properties is None:
                            continue
                        elif not self.isFree and not properties["addons"][
                                "satellites"]["enabled"]:
                            try:
                                await guild.me.edit(nick="Disabled")
                            except:
                                continue
                        elif self.isFree or properties["addons"]["satellites"][
                                "connection"] is not None:
                            try:
                                await guild.me.edit(nick=self.priceText)
                            except:
                                continue
                        else:
                            try:
                                await guild.me.edit(nick="Alpha Pro required")
                            except:
                                continue

                    try:
                        await client.change_presence(
                            status=status,
                            activity=discord.Activity(
                                type=discord.ActivityType.watching,
                                name=statusText))
                    except:
                        pass

            except asyncio.CancelledError:
                return
            except Exception:
                print(traceback.format_exc())
                if os.environ["PRODUCTION_MODE"]:
                    self.logging.report_exception()