Skip to content

Commit 361eb24

Browse files
author
Flamingo Fiesta
committed
Remove .DS_Store files and update .gitignore and bot scripts
1 parent 9cf3874 commit 361eb24

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed

.DS_Store

-8 KB
Binary file not shown.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ dmypy.json
135135
# Custom
136136
config/config.yml
137137
config/config.env
138+
.DS_Store
138139

139140
docker-compose.dev.yml
140141

bot/.DS_Store

-6 KB
Binary file not shown.

bot/bot.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,44 @@ async def help_group_chat_handle(update: Update, context: CallbackContext):
159159
await update.message.reply_text(text, parse_mode=ParseMode.HTML)
160160
await update.message.reply_video(config.help_group_chat_video_path)
161161

162+
#untested
163+
#async def token_balance_preprocessor(update: Update, context: CallbackContext):
164+
#user_id = update.effective_user.id
165+
166+
#if db.check_token_balance(user_id) >= 11: # Assuming 1 token is needed
167+
#context.user_data['process_allowed'] = True
168+
#else:
169+
#context.user_data['process_allowed'] = False
170+
#await update.message.reply_text("Insufficient tokens. Please top up to continue.")
171+
172+
#untested
173+
async def token_balance_preprocessor(update: Update, context: CallbackContext):
174+
user_id = update.effective_user.id
175+
current_balance = db.check_token_balance(user_id)
176+
177+
if db.check_token_balance(user_id) < 1: # Assuming 1 token is needed
178+
context.user_data['process_allowed'] = False
179+
await update.message.reply_text(
180+
f"_Insufficient tokens. Please top up to continue._ \n\n Your current balance is {current_balance}",
181+
parse_mode='Markdown'
182+
)
183+
return False
184+
else:
185+
context.user_data['process_allowed'] = True
186+
return True
187+
162188

163189
async def retry_handle(update: Update, context: CallbackContext):
164190
await register_user_if_not_exists(update, context, update.message.from_user)
165191
if await is_previous_message_not_answered_yet(update, context): return
166-
192+
167193
user_id = update.message.from_user.id
168194
db.set_user_attribute(user_id, "last_interaction", datetime.now())
169195

196+
#untested1
197+
if not await token_balance_preprocessor(update, context):
198+
return
199+
170200
dialog_messages = db.get_dialog_messages(user_id, dialog_id=None)
171201
if len(dialog_messages) == 0:
172202
await update.message.reply_text("No message to retry 🤷‍♂️")
@@ -200,11 +230,16 @@ async def message_handle(update: Update, context: CallbackContext, message=None,
200230
user_id = update.message.from_user.id
201231
chat_mode = db.get_user_attribute(user_id, "current_chat_mode")
202232

233+
#untested1
234+
if not await token_balance_preprocessor(update, context):
235+
return
236+
203237
if chat_mode == "artist":
204238
await generate_image_handle(update, context, message=message)
205239
return
206240

207241
async def message_handle_fn():
242+
208243
# new dialog timeout
209244
if use_new_dialog_timeout:
210245
if (datetime.now() - db.get_user_attribute(user_id, "last_interaction")).seconds > config.new_dialog_timeout and len(db.get_dialog_messages(user_id)) > 0:
@@ -338,6 +373,10 @@ async def voice_message_handle(update: Update, context: CallbackContext):
338373
user_id = update.message.from_user.id
339374
db.set_user_attribute(user_id, "last_interaction", datetime.now())
340375

376+
#untested1
377+
if not await token_balance_preprocessor(update, context):
378+
return
379+
341380
voice = update.message.voice
342381
voice_file = await context.bot.get_file(voice.file_id)
343382

@@ -606,6 +645,10 @@ async def show_balance_handle(update: Update, context: CallbackContext):
606645

607646

608647
async def edited_message_handle(update: Update, context: CallbackContext):
648+
649+
#untested
650+
651+
609652
if update.edited_message.chat.type == "private":
610653
text = "🥲 Unfortunately, message <b>editing</b> is not supported"
611654
await update.edited_message.reply_text(text, parse_mode=ParseMode.HTML)

bot/database.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def add_new_user(
5050
"n_used_tokens": {},
5151

5252
"n_generated_images": 0,
53-
"n_transcribed_seconds": 0.0 # voice message transcription
53+
"n_transcribed_seconds": 0.0, # voice message transcription
54+
"token_balance": 100 # Initialize token balance for new users
5455
}
5556

5657
if not self.check_if_user_exists(user_id):
@@ -126,3 +127,18 @@ def set_dialog_messages(self, user_id: int, dialog_messages: list, dialog_id: Op
126127
{"_id": dialog_id, "user_id": user_id},
127128
{"$set": {"messages": dialog_messages}}
128129
)
130+
#untested
131+
def check_token_balance(self, user_id: int) -> int:
132+
"""Check the user's current token balance."""
133+
user = self.user_collection.find_one({"_id": user_id})
134+
return user.get("token_balance", 0)
135+
136+
def deduct_tokens(self, user_id: int, tokens_used: int):
137+
"""Deduct a certain number of tokens from the user's balance."""
138+
self.user_collection.update_one(
139+
{"_id": user_id},
140+
{"$inc": {"token_balance": -tokens_used}}
141+
)
142+
143+
144+

config/.DS_Store

-6 KB
Binary file not shown.

0 commit comments

Comments
 (0)