bump to latest libraries
Marco Andronaco andronacomarco@gmail.com
Mon, 13 Feb 2023 00:08:23 +0100
4 files changed,
104 insertions(+),
109 deletions(-)
M
Constants.py
→
Constants.py
@@ -24,7 +24,7 @@ 'summary': "You bet {}$ and won a total of {}$.",
'current_bet': "{}, your current bet is {:0.2f}$.", 'current_cash': "{}, you currently have {:0.2f}$ in your account.", 'cash_reset': "{}, your cash has been reset to {:0.2f}$. You can do this once per day.", - 'cash_reset_fail': "{}, you have 0$ in your account and you cannot reset it today. Come back tomorrow.", + 'cash_reset_fail': "{}, you have {:0.2f}$ in your account and you cannot reset it today. Come back tomorrow.", 'no_autospin': "Sorry, multiple spins are disabled in group chats.", 'current_language': "Current language: {}.\nChoices: {}\nTo change it, type \"/lang <code>\" or use one of the buttons below.", 'invalid_language': "Invalid language.",@@ -54,7 +54,7 @@ 'summary': "Hai giocato {}€ e vinto un totale di {}€.",
'current_bet': "{}, il tuo bet attuale è {:0.2f}€.", 'current_cash': "{}, il tuo saldo attuale è {:0.2f}€.", 'cash_reset': "{}, il tuo saldo è stato ripristinato a {:0.2f}€. Puoi farlo una volta al giorno.", - 'cash_reset_fail': "{}, il tuo saldo è 0€ e non puoi più resettarlo oggi. Riprova domani.", + 'cash_reset_fail': "{}, il tuo saldo è {:0.2f}€ e non puoi più resettarlo oggi. Riprova domani.", 'no_autospin': "Gli spin multipli sono disabilitati nelle chat di gruppo.", 'current_language': "Lingua attuale: {}.\nAltre lingue: {}\nPer cambiarla, scrivi \"/lang <codice>\" o usa uno dei tasti qui sotto.", 'invalid_language': "Questa lingua non esiste.",@@ -180,8 +180,8 @@ (3, "bar"): 5,
(3, "lemon"): 3, (3, "grape"): 2, - (2, "seven"): 4, - (2, "bar"): 2, - (2, "lemon"): 1, - (2, "grape"): 0.5 + (2, "seven"): 2, + (2, "bar"): 1, + (2, "lemon"): 0.5, + (2, "grape"): 0.3 }
M
Slot.py
→
Slot.py
@@ -46,18 +46,18 @@
def set_bet(context: CallbackContext, amount: int): return set_user_value(context, "bet", max(0, amount)) -def _spin(context: CallbackContext, id: float, delay=True): +async def _spin(context: CallbackContext, id: float, delay=True): bet = get_bet(context) cash = get_cash(context) if cash < bet: - context.bot.send_message(chat_id=id, text=l("not_enough_cash", context)) + await context.bot.send_message(chat_id=id, text=l("not_enough_cash", context)) return None cash = set_cash(context, cash - bet) - message = context.bot.send_dice(chat_id=id, emoji=slot_emoji, disable_notification=True) + message = await context.bot.send_dice(chat_id=id, emoji=slot_emoji, disable_notification=True) multiplier = get_multiplier(message.dice.value)@@ -77,16 +77,16 @@ "chat_id": id,
"text": text, "reply_markup": markup } - context.job_queue.run_once(show_result, 2, context=args, name=str(id)) + context.job_queue.run_once(show_result, 2, data=args, name=str(id)) else: message.edit_reply_markup(InlineKeyboardMarkup([[InlineKeyboardButton(text=l("fast_output", context).format(win / 100), callback_data="none")]])) return win -def show_result(context: CallbackContext): - con = context.job.context - context.bot.send_message(chat_id=con["chat_id"], text=con["text"], reply_markup=con["reply_markup"], disable_notification=True) +async def show_result(context: CallbackContext): + con = context.job.data + return await context.bot.send_message(chat_id=con["chat_id"], text=con["text"], reply_markup=con["reply_markup"], disable_notification=True) -def spin(update: Update, context: CallbackContext): +async def spin(update: Update, context: CallbackContext): bet = get_bet(context) / 100@@ -94,14 +94,14 @@ amount = read_arg(context, 1)
if amount > 1 and update.effective_chat.type != 'private': amount = 1 - context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context)) + await context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context)) if amount == 1: - _spin(context=context, id=update.effective_chat.id) + return await _spin(context=context, id=update.effective_chat.id) else: - autospin(context=context, id=update.effective_chat.id, amount=amount) + return await autospin(context=context, id=update.effective_chat.id, amount=amount) -def autospin(context: CallbackContext, id: int, amount: int): +async def autospin(context: CallbackContext, id: int, amount: int): bet = get_bet(context) / 100 count = 0@@ -110,7 +110,7 @@ amount = max(2, min(autospin_cap, amount))
for i in range(amount): - win = _spin(context=context, id=id, delay=False) + win = await _spin(context=context, id=id, delay=False) if win is None: break@@ -120,9 +120,9 @@ total_win += win
result = l("summary", context).format(count * bet, total_win / 100) markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount), callback_data=f"reroll {amount}")]]) - context.bot.send_message(chat_id=id, text=result, reply_markup=markup, disable_notification=False) + return await context.bot.send_message(chat_id=id, text=result, reply_markup=markup, disable_notification=False) -def bet(update: Update, context: CallbackContext): +async def bet(update: Update, context: CallbackContext): amount = read_arg(context=context, cast=float)@@ -132,12 +132,13 @@ else:
bet = get_bet(context) result = l("current_bet", context).format(c.format_author(update.effective_user), bet / 100) - context.bot.send_message(chat_id=update.effective_chat.id, text=result) + return await context.bot.send_message(chat_id=update.effective_chat.id, text=result) def cash(update: Update, context: CallbackContext): + cash = get_cash(context) / 100 - if cash < cash_default / 2: + if cash < cash_default / 200: lastreset = get_lastreset(context) today = date.today()@@ -148,7 +149,7 @@
result = l("cash_reset", context).format(c.format_author(update.effective_user), cash / 100) return update.message.reply_text(text=result) else: - return update.message.reply_text(text=l("cash_reset_fail", context).format(c.format_author(update.effective_user))) + return update.message.reply_text(text=l("cash_reset_fail", context).format(c.format_author(update.effective_user), cash)) result = l("current_cash", context).format(c.format_author(update.effective_user), cash)
M
main.py
→
main.py
@@ -11,7 +11,7 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
from io import BytesIO from telegram.error import TelegramError -from telegram.ext import Updater, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, Filters, PicklePersistence +from telegram.ext import ApplicationBuilder, Updater, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, PicklePersistence, filters, PersistenceInput from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton def _get_message_content(message):@@ -112,7 +112,7 @@
def start(update: Update, context: CallbackContext): context.bot.send_message(chat_id=update.effective_chat.id, text=l("welcome", context)) -def set_lewd(update: Update, context: CallbackContext): +async def set_lewd(update: Update, context: CallbackContext): try: output = False if context.chat_data["lewd"] else True@@ -122,12 +122,12 @@
context.chat_data['lewd'] = output message = l("lewd_toggle", context).format(l("enabled", context) if output else l("disabled", context)) - context.bot.send_message(chat_id=update.effective_chat.id, text=message) + return await context.bot.send_message(chat_id=update.effective_chat.id, text=message) -def pic(update: Update, context: CallbackContext): +async def pic(update: Update, context: CallbackContext): image, markup = _get_image(context) - update.message.reply_photo(photo=img_to_bio(image), parse_mode="markdown", reply_markup=markup) + return await update.message.reply_photo(photo=img_to_bio(image), parse_mode="markdown", reply_markup=markup) def _get_author(message):@@ -198,107 +198,102 @@ return None
return input_text -def ttbt(update: Update, context: CallbackContext): +async def ttbt(update: Update, context: CallbackContext): content, image, markup = _get_all(update, ttbt_check, context) if image is None: - update.message.reply_text(l("no_caption", context)) - return + return await update.message.reply_text(l("no_caption", context)) image = tt_bt_effect(content, image) if image is None: - update.message.reply_text(l("failed_effect", context)) + return await update.message.reply_text(l("failed_effect", context)) - update.message.reply_photo(photo=image, reply_markup=markup) + return await update.message.reply_photo(photo=image, reply_markup=markup) -def tt(update: Update, context: CallbackContext): +async def tt(update: Update, context: CallbackContext): content, image, markup = _get_all(update, tt_check, context) if image is None: - update.message.reply_text(l("no_caption", context)) - return + return await update.message.reply_text(l("no_caption", context)) image = tt_bt_effect(content, image) if image is None: - update.message.reply_text(l("failed_effect", context)) + return await update.message.reply_text(l("failed_effect", context)) - update.message.reply_photo(photo=image, reply_markup=markup) + return await update.message.reply_photo(photo=image, reply_markup=markup) -def bt(update: Update, context: CallbackContext): +async def bt(update: Update, context: CallbackContext): content, image, markup = _get_all(update, tt_check, context) if image is None: - update.message.reply_text(l("no_caption", context)) - return + return await update.message.reply_text(l("no_caption", context)) image = bt_effect(content, image) if image is None: - update.message.reply_text(l("failed_effect", context)) + return await update.message.reply_text(l("failed_effect", context)) - update.message.reply_photo(photo=image, reply_markup=markup) + return await update.message.reply_photo(photo=image, reply_markup=markup) -def splash(update: Update, context: CallbackContext): +async def splash(update: Update, context: CallbackContext): content, image, markup = _get_all(update, splash_check, context) if image is None: - update.message.reply_text(l("no_caption", context)) - return + return await update.message.reply_text(l("no_caption", context)) image = splash_effect(content, image) if image is None: - update.message.reply_text(l("failed_effect", context)) + return await update.message.reply_text(l("failed_effect", context)) - update.message.reply_photo(photo=image, reply_markup=markup) + return await update.message.reply_photo(photo=image, reply_markup=markup) -def wot(update: Update, context: CallbackContext): +async def wot(update: Update, context: CallbackContext): content, image, markup = _get_all(update, wot_check, context) if image is None: - update.message.reply_text(l("no_caption", context)) - return + return await update.message.reply_text(l("no_caption", context)) image = wot_effect(content, image) if image is None: - update.message.reply_text(l("failed_effect", context)) + await update.message.reply_text(l("failed_effect", context)) - update.message.reply_photo(photo=image, reply_markup=markup) + await update.message.reply_photo(photo=image, reply_markup=markup) -def text(update: Update, context: CallbackContext): +async def text(update: Update, context: CallbackContext): content, image, markup = _get_all(update, wot_check, context) if image is None: - update.message.reply_text(l("no_caption", context)) + await update.message.reply_text(l("no_caption", context)) return image = text_effect(content, image) if image is None: - update.message.reply_text(l("failed_effect", context)) + await update.message.reply_text(l("failed_effect", context)) - update.message.reply_photo(photo=image, reply_markup=markup) + await update.message.reply_photo(photo=image, reply_markup=markup) -def caps(update: Update, context: CallbackContext): +async def caps(update: Update, context: CallbackContext): _, reply, _ = _get_reply(update.message.reply_to_message, ' '.join(context.args)) - context.bot.send_message(chat_id=update.effective_chat.id, text=reply.upper()) + await context.bot.send_message(chat_id=update.effective_chat.id, text=reply.upper()) -def _set_lang(update: Update, context: CallbackContext, lang: str): +async def _set_lang(update: Update, context: CallbackContext, lang: str): context.chat_data["lang"] = lang - context.bot.send_message(chat_id=update.effective_chat.id, text=l("language_set", context).format(format_lang(lang))) + await context.bot.send_message(chat_id=update.effective_chat.id, text=l("language_set", context).format(format_lang(lang))) -def lang(update: Update, context: CallbackContext): +async def lang(update: Update, context: CallbackContext): try: selected = str(context.args[0]) except IndexError:@@ -307,29 +302,28 @@
if selected is None: lang = format_lang(get_lang(context)) choices = ", ".join(langs) + "." - return update.message.reply_text(text=l("current_language", context).format(lang, choices), reply_markup=lang_markup) + return await update.message.reply_text(text=l("current_language", context).format(lang, choices), reply_markup=lang_markup) if selected not in langs: - update.message.reply_text(text=l("invalid_language", context)) - return + return await update.message.reply_text(text=l("invalid_language", context)) - _set_lang(update, context, selected) + return await _set_lang(update, context, selected) def unknown(update: Update, context: CallbackContext): logging.info(f"User {update.message.from_user.full_name} sent {update.message.text_markdown_v2} and I don't know what that means.") -def error_callback(update: Update, context: CallbackContext): +async def error_callback(update: Update, context: CallbackContext): try: raise context.error except TelegramError as e: logging.error("TelegramError! " + str(e)) - context.bot.send_message(chat_id=update.effective_chat.id, text=l('error', context)) + await context.bot.send_message(chat_id=update.effective_chat.id, text=l('error', context)) -def _add_effect_handler(dispatcher, command: str, callback): - dispatcher.add_handler(CommandHandler(command, callback)) - dispatcher.add_handler(MessageHandler(Filters.caption(update=[f"/{command}"]), callback)) +def _add_effect_handler(application: ApplicationBuilder, command: str, callback): + application.add_handler(CommandHandler(command, callback)) + application.add_handler(MessageHandler(filters.Caption([f"/{command}"]), callback)) -def keyboard_handler(update: Update, context: CallbackContext): +async def keyboard_handler(update: Update, context: CallbackContext): query = update.callback_query data = query.data@@ -337,62 +331,62 @@ if data.startswith("reroll"):
amount = int(data.split(" ")[1]) if amount <= 1: - return spin(update, context) - return autospin(context, update.effective_chat.id, amount) + return await spin(update, context) + return await autospin(context, update.effective_chat.id, amount) match data: case "none": return query.answer(l("none_callback", context)) case "set_lang_en": lang = "en" - _set_lang(update, context, lang) - return query.answer(l("language_set", context).format(format_lang(lang))) + await _set_lang(update, context, lang) + return await query.answer(l("language_set", context).format(format_lang(lang))) case "set_lang_it": lang = "it" - _set_lang(update, context, lang) - return query.answer(l("language_set", context).format(format_lang(lang))) + await _set_lang(update, context, lang) + return await query.answer(l("language_set", context).format(format_lang(lang))) case other: logging.error(f"unknown callback: {data}") - return query.answer() + return await query.answer() def main(): - updater = Updater(token=os.getenv("token"), - persistence=PicklePersistence(filename='bot-data.pkl', - store_bot_data=False, - store_callback_data=False - )) + pers = PersistenceInput(bot_data = False, callback_data = False) - dispatcher = updater.dispatcher + application = ApplicationBuilder() + application.token(os.getenv("token")) + application.persistence(PicklePersistence(filepath='bot-data.pkl', store_data=pers)) - dispatcher.add_error_handler(error_callback) - dispatcher.add_handler(CallbackQueryHandler(callback=keyboard_handler)) + application = application.build() + + + application.add_error_handler(error_callback) + application.add_handler(CallbackQueryHandler(callback=keyboard_handler)) # commands - dispatcher.add_handler(CommandHandler('start', start)) - dispatcher.add_handler(CommandHandler('lang', lang)) - dispatcher.add_handler(CommandHandler('lewd', set_lewd)) - dispatcher.add_handler(CommandHandler('caps', caps)) - dispatcher.add_handler(CommandHandler('pic', pic)) + application.add_handler(CommandHandler('start', start)) + application.add_handler(CommandHandler('lang', lang)) + application.add_handler(CommandHandler('lewd', set_lewd)) + application.add_handler(CommandHandler('caps', caps)) + application.add_handler(CommandHandler('pic', pic)) # effects - _add_effect_handler(dispatcher, 'ttbt', ttbt) - _add_effect_handler(dispatcher, 'tt', tt) - _add_effect_handler(dispatcher, 'bt', bt) - _add_effect_handler(dispatcher, 'splash', splash) - _add_effect_handler(dispatcher, 'wot', wot) - _add_effect_handler(dispatcher, 'text', text) + _add_effect_handler(application, 'ttbt', ttbt) + _add_effect_handler(application, 'tt', tt) + _add_effect_handler(application, 'bt', bt) + _add_effect_handler(application, 'splash', splash) + _add_effect_handler(application, 'wot', wot) + _add_effect_handler(application, 'text', text) # games - dispatcher.add_handler(CommandHandler('spin', spin)) - dispatcher.add_handler(CommandHandler('bet', bet)) - dispatcher.add_handler(CommandHandler('cash', cash)) + application.add_handler(CommandHandler('spin', spin)) + application.add_handler(CommandHandler('bet', bet)) + application.add_handler(CommandHandler('cash', cash)) # fallback - dispatcher.add_handler(MessageHandler(Filters.command, unknown)) - updater.start_polling() - updater.idle() + application.add_handler(MessageHandler(filters.Command(), unknown)) + application.run_polling() if __name__ == "__main__": main()
M
requirements.txt
→
requirements.txt
@@ -1,5 +1,5 @@
anime-api==0.15.4 -Pillow==9.2.0 -python-dotenv==0.21.0 -python-telegram-bot==13.14 -requests==2.28.1 +Pillow==9.4.0 +python-dotenv==0.21.1 +python-telegram-bot[job-queue]==20.1 +requests==2.28.2