Skip to content

Commit

Permalink
fix: update bot
Browse files Browse the repository at this point in the history
  • Loading branch information
i007c committed Jun 17, 2023
1 parent 5eea3f5 commit b0940ac
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
18 changes: 12 additions & 6 deletions bchat/db/chargc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
from settings import sqlx
from sqlalchemy import insert, select, update

Out = list[ChargcModel] | ChargcModel

async def chargc_get(*where) -> ChargcModel:
query = select(ChargcTable).where(*where)

row = await sqlx.fetch_one(query)
if not row:
return None
async def chargc_get(*where, limit=10, offset=0) -> Out:
query = select(ChargcTable).where(*where).offset(offset)

return ChargcModel(**row)
if limit == 1:
row = await sqlx.fetch_one(query)
if not row:
return None

return ChargcModel(**row)

rows = await sqlx.fetch_all(query.limit(limit))
return [ChargcModel(**R) for R in rows]


async def chargc_update(*where, **values):
Expand Down
2 changes: 2 additions & 0 deletions bchat/modules/admin/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ async def help_cmd(update: Update, ctx: Ctx):
'/stats -> user count\n'
'/update_db -> only for developer\n'
'/seen_all -> seen all of your directs\n'
'/charge_codes -> get unused codes\n'
'/shop -> show undone shop\n'
'/charge <Toman> <code> <code> ...\n'
'/user_score <code> -> get the user score\n'
'/user_score <code> set 12 -> set the user used score\n'
Expand Down
25 changes: 22 additions & 3 deletions bchat/modules/admin/shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import logging
import time

from db.chargc import chargc_add
from db.chargc import chargc_add, chargc_get
from db.shop import shop_get, shop_update
from deps import require_admin
from models import ItemType, ShopTable
from models import ChargcTable, ItemType, ShopTable
from modules.shop.common import CHARGE_PTC, CHARGE_RANGE
from telegram import Update
from telegram.ext import CommandHandler, ContextTypes
Expand Down Expand Up @@ -92,7 +92,26 @@ async def show_shop(update: Update, ctx: Ctx):

await update.effective_message.reply_text(text)


@require_admin
async def show_codes(update: Update, ctx: Ctx):
offset = 0
try:
offset = int(ctx.args[0]) * 10
except Exception:
pass

items = await chargc_get(ChargcTable.used == False, offset=offset)
text = 'unused codes:\n'

for i in items:
text += f'[{i.cc_id}] {i.op} {i.amount} {i.code}\n'

await update.effective_message.reply_text(text)


H_SHOP = [
CommandHandler(['charge'], add_charge_codes),
CommandHandler(['shop'], show_shop)
CommandHandler(['shop'], show_shop),
CommandHandler(['charge_codes'], show_codes),
]
1 change: 1 addition & 0 deletions bchat/modules/shop/charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async def buy_phone_charge(update: Update, ctx: Ctx, state: UserModel):
ChargcTable.op == ptc,
ChargcTable.amount == charge,
ChargcTable.used == False,
limit=1
)
if charge_code:
await msg.edit_text(
Expand Down
13 changes: 8 additions & 5 deletions bchat/modules/shop/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
CHARGE_RANGE = (
# score - charge in toman
(100, 20),
(200, 40),
(400, 50),
(800, 810),
(1200, 250),
(200, 45),
(300, 70),
(400, 100),
(500, 120),
(1000, 250),
)
CHARGE_PTC = {
'irancell': 'ایرانسل 🟡',
Expand All @@ -22,7 +23,9 @@
# score - member
(100, 50),
(200, 100),
(500, 150),
(300, 150),
(500, 290),
(1000, 690),
)


Expand Down
5 changes: 0 additions & 5 deletions bchat/modules/shop/shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
from settings import KW_USESCOR
from telegram import Update
from telegram.ext import CallbackQueryHandler, MessageHandler, filters
from utils import config

from .common import CART_IKB, CHARGE_TEXT, MEMBER_TEXT, SHOP_IKB, Ctx


@require_user_data
async def shop(update: Update, ctx: Ctx, state: UserModel):
user = update.effective_user
if user.id not in config['ADMINS']:
return

ava_score = state.total_score - state.used_score

text = (
Expand Down
2 changes: 1 addition & 1 deletion bchat/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
KW_DRTNSEN = 'پیام های خوانده نشده 📬'
KW_SAVELST = 'کاربران ذخیره شده 📋'
KW_CTSPLCN = '💖 به مخاطب خاص وصلم کن'
KW_USESCOR = 'تبدیل امتیاز به شارژ 💎'
KW_USESCOR = 'تبدیل امتیاز به شارژ و ممبر 💎'


MAIN_KEYBOARD = [
Expand Down

0 comments on commit b0940ac

Please sign in to comment.