Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

excepciones básicas añadidas #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cosas_llave/TODO
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
necesito lista actualiza para ver quien tiene permiso de llave

Binary file modified cosas_llave/__pycache__/llave.cpython-311.pyc
Binary file not shown.
77 changes: 45 additions & 32 deletions cosas_llave/llave.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,55 @@ def coger(llavero):
return fecha_mensaje

def consultar():
with open("llave.json", "r") as archivo:
datos_llave = json.load(archivo)
return datos_llave["llavero"]

try:
with open("llave.json", "r") as archivo:
datos_llave = json.load(archivo)
return datos_llave["llavero"]
except FileNotFoundError:
print("Ha pasado algo, y es que el archivo no existe, escribe el comando //llave o //llavent para crearlo")
return -1

def dejar():
#conseguimos la fecha cuando se escribio el mensaje y le cambiamos el formato para adaptarlo al .json y librarnos de los microsegundos
fecha_mensaje = datetime.datetime.now()
fecha_json = datetime.datetime.strftime(fecha_mensaje, "%Y,%m,%d %H,%M,%S")
fecha_mensaje = datetime.datetime.strftime(fecha_mensaje, "%H:%M:%S")
try:
with open("llave.json", "r") as archivo:
dict = json.load(archivo)
except FileNotFoundError:
print("Ha pasado algo, y es que el archivo no existe, escribe el comando //llave o //llavent para crearlo")
return -1
if "conserjeria" in dict.values():
print("Han intentado dejar la llave donde ya está, en consejeria")
return -2
else:
#conseguimos la fecha cuando se escribio el mensaje y le cambiamos el formato para adaptarlo al .json y librarnos de los microsegundos
fecha_mensaje = datetime.datetime.now()
fecha_json = datetime.datetime.strftime(fecha_mensaje, "%Y,%m,%d %H,%M,%S")
fecha_mensaje = datetime.datetime.strftime(fecha_mensaje, "%H:%M:%S")

#divimos el string del datetime para poder meterlo guay en un diccionario
# Split the date and time string by space
date_str, time_str = fecha_json.split(" ")
#divimos el string del datetime para poder meterlo guay en un diccionario
# Split the date and time string by space
date_str, time_str = fecha_json.split(" ")

# Split the date string by comma
year, month, day = date_str.split(",")
# Split the date string by comma
year, month, day = date_str.split(",")

# Split the time string by comma
hour, minute, second = time_str.split(",")
# Split the time string by comma
hour, minute, second = time_str.split(",")

# Modificamos el diccionario
datos_llave = {
"llavero": "copisteria",
"fecha":{
"anio": year,
"mes": month,
"dia": day,
},
"tiempo": {
"hora": hour,
"minuto": minute,
"segundo": second
# Modificamos el diccionario
datos_llave = {
"llavero": "conserjeria",
"fecha":{
"anio": year,
"mes": month,
"dia": day,
},
"tiempo": {
"hora": hour,
"minuto": minute,
"segundo": second
}
}
}
#escribimos en el json
with open("llave.json", "w") as archivo:
json.dump(datos_llave, archivo, indent=4)
return fecha_mensaje
#escribimos en el json
with open("llave.json", "w") as archivo:
json.dump(datos_llave, archivo, indent=4)
return fecha_mensaje
8 changes: 4 additions & 4 deletions llave.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"llavero": "YagoCheca",
"llavero": "conserjeria",
"fecha": {
"anio": "2023",
"mes": "11",
"dia": "10"
"dia": "17"
},
"tiempo": {
"hora": "22",
"minuto": "14",
"segundo": "30"
"minuto": "37",
"segundo": "53"
}
}
38 changes: 13 additions & 25 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from dotenv import load_dotenv
import random
import os
from cosas_llave import llave


from cosas_llave import llave

load_dotenv()

Expand All @@ -26,36 +25,25 @@ def coger_llave(message):
hora_mensaje = llave.coger(username)
bot.reply_to(message, f"@{username} ha cogido la llave a las {hora_mensaje}")

@bot.message_handler(commands=['llave?'])
@bot.message_handler(commands=['where_llave'])
def consultar_llave(message):
llavero = llave.consultar()
bot.reply_to(message, f"La llave la tiene @{llavero}")
if llavero == -1:
bot.reply_to(message, "Ha pasado algo, y es que el archivo no existe, escribe el comando //llave o //llavent para crearlo")
else:
bot.reply_to(message, f"La llave la tiene @{llavero}")

@bot.message_handler(commands=['llavent'])
def dejar_llave(message):
username = message.from_user.username if message.from_user.username else "Usuario_desconocido"
hora_mensaje = llave.dejar()
bot.reply_to(message, f"@{username} ha dejado la llave en copisteria a las {hora_mensaje}")




"""
@bot.message_handler(commands=['tatequieto'])
def tatequieto(message):
bot.reply_to(message, "perdon ya me apago")
bot.stop_polling()
print("apagao")

Bloque de texto comentado, es una funcion que toma cualquier mensaje y menciona a quien lo haya enviado
# Manejador de mensajes que no son comandos
@bot.message_handler(func=lambda message: True)
def echo_all(message):
username = message.from_user.username if message.from_user.username else "Usuario desconocido"
bot.reply_to(message, f"¡Hola, @{username}! Soy un bot de prueba. ¡Gracias por iniciar la conversación!")
"""


match hora_mensaje:
case -1:
bot.reply_to(message, f"@{username} ha pasado algo, y es que el archivo no existe, escribe el comando //llave o //llavent para crearlo")
case -2:
bot.reply_to(message, f"@{username} ha intentado dejar la llave donde ya está, en conserjeria")
case _:
bot.reply_to(message, f"@{username} ha dejado la llave en conserjeria a las {hora_mensaje}")

# Iniciar el bot
bot.polling()
Expand Down