Beispiel #1
0
    async def allcoinstats(self, ctx, timeframe: TimeframeType):
        # delete week old data
        data.delete_week_old_events()

        # if default coin isn't set, send info to user about how to set it
        default_coin = data.get_default_coin(ctx.guild.id)
        if not default_coin:
            return await pretty_print(
                ctx,
                "A default coin has not been set. An admin can set the default coin by typing $setdefaultcoin . Type $help for more information.",
                title="Error",
                color=ERROR_COLOR)

        # get statistics
        if timeframe == 'day':
            coin_stats = alerts.get_day_stats(default_coin)
        else:
            coin_stats = alerts.get_week_stats(default_coin)

        rewards = rally_api.get_coin_rewards(default_coin)
        coin_image_url = rally_api.get_coin_image_url(default_coin)

        # format message, done through dict to make keeping this and daily_stats message similar easier
        extra_str = 'Today' if timeframe == 'day' else 'This Week'
        reward_str = 'last24HourEarned' if timeframe == 'day' else 'weeklyAccumulatedReward'
        message = {
            "description":
            f"```xl\n"
            f"- {extra_str}`s purchases: {len(coin_stats['buy'])}\n\n"
            f"- {extra_str}`s donations: {len(coin_stats['donate'])}\n\n"
            f"- {extra_str}`s transfers: {len(coin_stats['transfer'])}\n\n"
            f"- {extra_str}`s conversions: {len(coin_stats['convert'])}\n\n"
            f"- {extra_str}`s redeems: {len(coin_stats['redeem'])}\n\n"
            f"- {extra_str}`s rewards earned: {round(rewards[reward_str], 3)}\n"
            f"```",
            "color":
            0xff0000,
            "author": {
                "name": f"{default_coin} Stats {extra_str}",
                "icon_url": coin_image_url
            },
            "timestamp":
            datetime.datetime.now().isoformat()
        }

        # send message
        embed = discord.Embed.from_dict(message)
        return await ctx.send(embed=embed)
Beispiel #2
0
    async def on_daily_stats_timer_over(self, timer: dict) -> None:
        """
        Function called when daily_stats timer is over.

        @param timer: timer object dict
        """
        # delete week old stats
        data.delete_week_old_events()

        # gather some needed data
        guild_id = timer['guildId']
        channel_name = timer['extras']['channel_name']
        webhook_url = await alerts.get_webhook_url(guild_id, channel_name)
        default_coin = data.get_default_coin(int(guild_id))

        # check if there is a webhook url to send stats to
        if webhook_url:
            # gather stats data
            coin_day_stats = alerts.get_day_stats(default_coin)
            total_stats = rally_api.get_coin_summary(default_coin)
            rewards = rally_api.get_coin_rewards(default_coin)

            # create stats message
            coin_image_url = rally_api.get_coin_image_url(default_coin)
            message = {
                "embeds": [
                    {
                        "description": f"```xl\n- Total coins: {round(total_stats['totalCoins'], 3)}\n\n"
                                       f"- Total supporters: {round(total_stats['totalSupporters'], 3)}\n\n"
                                       f"- Total support volume: {round(total_stats['totalSupportVolume'], 3)} USD\n\n\n"
                                       f"- Today`s purchases: {len(coin_day_stats['buy'])}\n\n"
                                       f"- Today`s donations: {len(coin_day_stats['donate'])}\n\n"
                                       f"- Today`s transfers: {len(coin_day_stats['transfer'])}\n\n"
                                       f"- Today`s conversions: {len(coin_day_stats['convert'])}\n\n"
                                       f"- Today`s redeems: {len(coin_day_stats['redeem'])}\n\n"
                                       f"- Today`s rewards earned: {round(rewards['last24HourEarned'], 3)}\n```",
                        "color": 0xff0000,
                        "author": {
                            "name": f"{default_coin} Daily Stats",
                            "icon_url": coin_image_url
                        },
                        "timestamp": datetime.datetime.now().isoformat()
                    }
                ]
            }

            requests.post(webhook_url, json=message)

        # start timer again
        if timer['bot_id'] in main.running_bots:
            bot_object = main.running_bots[timer['bot_id']]['bot']
        else:
            bot_object = main.main_bot

        if not timer['extras']['timezone']:
            timer['extras']['timezone'] = 0

        # get time until next midnight
        dt = datetime.datetime.utcnow() + datetime.timedelta(hours=int(timer['extras']['timezone']))
        time_midnight = time.time() + (((24 - dt.hour - 1) * 60 * 60) + ((60 - dt.minute - 1) * 60) + (60 - dt.second))

        # if the timezone is whacky and time_midnight ends up coming up before current time,
        # just add 24h to current time and set that as time_midnight
        if time_midnight < round(time.time()):
            time_midnight = round(time.time()) + (24 * 3600)  # 24h

        self.bot.timers.create(
            guild_id=guild_id,
            expires=time_midnight,
            event='daily_stats',
            extras=timer['extras'],
            bot_id=bot_object.user.id
        )
Beispiel #3
0
async def add_mapping(mapping: CoinMapping, guildId: str):
    data.add_default_coin(guildId, mapping.coinKind)
    coinKind = data.get_default_coin(guildId)
    if not coinKind:
        raise HTTPException(status_code=404, detail="Coin not found")
    return {"guildId": guildId, "coinKind": coinKind}