main.py (view raw)
1import os, logging
2logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
3from telegram import Update
4from telegram.ext import Updater, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, PicklePersistence
5from dotenv import load_dotenv
6load_dotenv()
7from Gardening import Plant, get_plant_info
8
9def reply(update: Update, context: CallbackContext, text: str = "", markup: str = ""):
10 return context.bot.send_message(chat_id=update.effective_chat.id, text=text, reply_markup=markup, parse_mode='Markdown')
11
12def get_plant(update: Update, context: CallbackContext):
13 try:
14 plant = context.bot_data[update.effective_user.id]["plant"]
15 except KeyError:
16 return None
17
18 if plant is None: # probably useless but heh
19 print("############### This wasn't so useless after all! ###############")
20 return None
21
22 plant.update()
23 return plant
24
25def start(update: Update, context: CallbackContext):
26 plant = get_plant(update, context)
27
28 if plant is not None:
29 return reply(update, context, "Hai già una pianta. Usa /water se vuoi innaffiarla.")
30
31 context.bot_data[update.effective_user.id] = { "plant" : Plant(update.effective_user.id) }
32 return reply(update, context, "Hai piantato un seme! Adesso usa /water per innaffiarlo.")
33
34def water(update: Update, context: CallbackContext):
35 plant = get_plant(update, context)
36
37 if plant is None:
38 return reply(update, context, "Non hai nessuna pianta da innaffiare! Usa /start per piantarne una.")
39
40 if plant.dead:
41 return reply(update, context, "La tua pianta è morta... Usa /harvest per piantarne un'altra.")
42
43 plant.water()
44 return reply(update, context, "Pianta innaffiata.")
45
46def show(update: Update, context: CallbackContext):
47 plant = get_plant(update, context)
48
49 if plant is None:
50 return reply(update, context, "Non hai nessuna pianta da mostrare! Usa /start per piantarne una.")
51
52 return reply(update, context, get_plant_info(plant))
53
54def harvest(update: Update, context: CallbackContext):
55 plant = get_plant(update, context)
56
57 if plant is None:
58 return reply(update, context, "Non hai nessuna pianta! Usa /start per piantarne una.")
59
60 if plant.dead:
61 plant.start_over()
62 return reply(update, context, "Hai piantato un nuovo seme. Usa /water per innaffiarlo, magari stavolta un po' più spesso.")
63
64 if plant.stage != 5:
65 return reply(update, context, "La tua pianta non è ancora pronta per andarsene!")
66
67 plant.start_over()
68 return reply(update, context, "Complimenti, hai piantato un nuovo seme! Usa /water per innaffiarlo.")
69'''
70def keyboard_handler(update: Update, context: CallbackContext):
71 query = update.callback_query
72 data = query.data
73
74 if data.startswith("reroll"):
75 amount = int(data.split(" ")[1])
76
77 if amount <= 1:
78 return spin(update, context)
79 return autospin(context, update.effective_chat.id, amount)
80
81 match data:
82 case "none":
83 return query.answer("This button doesn't do anything.", context))
84 case other:
85 logging.error(f"unknown callback: {data}")
86
87 return query.answer()
88
89def unknown(update: Update, context: CallbackContext):
90 logging.info(f"User {update.message.from_user.full_name} sent {update.message.text_markdown_v2} and I don't know what that means.")
91'''
92def main():
93
94 updater = Updater(token=os.getenv("token"),
95 persistence=PicklePersistence(filename='bot-data.pkl',
96 store_user_data=False,
97 store_bot_data=True,
98 store_callback_data=False,
99 store_chat_data=False
100 ))
101
102 dispatcher = updater.dispatcher
103
104
105 # commands
106 dispatcher.add_handler(CommandHandler('start', start))
107 dispatcher.add_handler(CommandHandler('water', water))
108 dispatcher.add_handler(CommandHandler('show', show))
109 dispatcher.add_handler(CommandHandler('harvest', harvest))
110
111 '''
112 # slot
113 dispatcher.add_handler(CommandHandler('spin', spin))
114 dispatcher.add_handler(CommandHandler('bet', bet))
115 dispatcher.add_handler(CommandHandler('cash', cash))
116 '''
117
118 '''
119 dispatcher.add_handler(CallbackQueryHandler(callback=keyboard_handler))
120 dispatcher.add_handler(MessageHandler(Filters.command, unknown))
121 '''
122
123 updater.start_polling()
124 print(os.getenv("bot_name"))
125 updater.idle()
126
127if __name__ == "__main__":
128 main()