Beispiel #1
0
async def successful_payment_handler(message: types.Message) -> None:
    payment = message.successful_payment
    donation_id = str(uuid4())[:8]
    amt = Decimal(payment.total_amount) / 100
    dt = datetime.now().replace(microsecond=0)
    async with pool.acquire() as conn:
        await conn.execute(
            """\
            INSERT INTO donation (
                donation_id, user_id, amount, donate_time,
                telegram_payment_charge_id, provider_payment_charge_id
            )
            VALUES
                ($1, $2, $3::NUMERIC, $4, $5, $6);""",
            donation_id,
            message.from_user.id,
            str(amt),
            dt,
            payment.telegram_payment_charge_id,
            payment.provider_payment_charge_id,
        )
    await asyncio.gather(
        message.answer(
            (f"Your donation of {amt} HKD is successful.\n"
             "Thank you for your support! :D\n"
             f"Donation id: #on9wcbot_{donation_id}"),
            parse_mode=types.ParseMode.HTML,
        ),
        send_admin_group(
            (f"Received donation of {amt} HKD from {message.from_user.get_mention(as_html=True)} "
             f"(id: <code>{message.from_user.id}</code>).\n"
             f"Donation id: #on9wcbot_{donation_id}"),
            parse_mode=types.ParseMode.HTML,
        ))
Beispiel #2
0
async def error_handler(update: types.Update, error: TelegramAPIError) -> None:
    for game in GAMES.values(
    ):  # TODO: Do this for group in which error occurs only
        asyncio.create_task(game.scan_for_stale_timer())

    if isinstance(error, MigrateToChat):
        if update.message.chat.id in GAMES:  # TODO: Test
            old_gid = GAMES[update.message.chat.id].group_id
            GAMES[error.migrate_to_chat_id] = GAMES.pop(update.message.chat.id)
            GAMES[error.migrate_to_chat_id].group_id = error.migrate_to_chat_id
            asyncio.create_task(
                send_admin_group(
                    f"Game moved from {old_gid} to {error.migrate_to_chat_id}."
                ))
        async with pool.acquire() as conn:
            await conn.execute(
                """\
                UPDATE game SET group_id = $1 WHERE group_id = $2;
                UPDATE gameplayer SET group_id = $1 WHERE group_id = $2;
                DELETE FROM game WHERE group_id = $2;
                DELETE FROM gameplayer WHERE group_id = $2;""",
                error.migrate_to_chat_id,
                update.message.chat.id,
            )
        await send_admin_group(f"Group migrated to {error.migrate_to_chat_id}."
                               )
        return

    send_admin_msg = await send_admin_group(
        f"`{error.__class__.__name__} @ "
        f"{update.message.chat.id if update.message and update.message.chat else 'idk'}`:\n"
        f"`{str(error)}`", )
    if not update.message or not update.message.chat:
        return

    try:
        await update.message.reply(
            "Error occurred. My owner has been notified.")
    except TelegramAPIError:
        pass

    if update.message.chat.id in GAMES:
        asyncio.create_task(
            send_admin_msg.reply(
                f"Killing game in {update.message.chat.id} consequently."))
        GAMES[update.message.chat.id].state = GameState.KILLGAME
        await asyncio.sleep(2)
        try:
            del GAMES[update.message.chat.id]
            await update.message.reply("Game ended forcibly.")
        except:
            pass