@@ -212,11 +212,17 @@ async def ejercicio_al_azar(self,
212212 """
213213
214214 guia = get_guia_por_sv (interaccion .guild_id )
215+ guia .pop ("version" )
216+
217+ if isinstance (sentido , Choice ):
218+ sentido_parseado = sentido .value
219+ else : # str
220+ sentido_parseado = sentido
215221
216222 unidad_posible_real = (unidad_posible if unidad_posible is None else unidad_posible .value )
217- unidad_pivote = (unidad_posible_real
218- if CogEjercicios .existe_unidad (unidad_posible_real , guia )
219- else choice (list (guia .keys ())))
223+ unidad_pivote = int (unidad_posible_real
224+ if CogEjercicios .existe_unidad (unidad_posible_real , guia )
225+ else choice (list (guia .keys ())))
220226 unidad_elegida = ''
221227 ejercicio_elegido = ''
222228
@@ -226,29 +232,44 @@ def _expresion_busqueda(unidad: int) -> bool:
226232 incluirlo entre los candidatos.
227233 """
228234
229- match sentido :
235+ match sentido_parseado :
230236 case '=' :
231- return unidad == int ( unidad_pivote )
237+ return unidad == unidad_pivote
232238
233239 case '<' :
234- return unidad < int ( unidad_pivote )
240+ return unidad < unidad_pivote
235241
236242 case "<=" :
237- return unidad <= int ( unidad_pivote )
243+ return unidad <= unidad_pivote
238244
239245 case '>' :
240- return unidad > int ( unidad_pivote )
246+ return unidad > unidad_pivote
241247
242248 case ">=" :
243- return unidad >= int ( unidad_pivote )
249+ return unidad >= unidad_pivote
244250
245251 case _:
246252 # Viene de un Choice, así que nunca va a ocurrir. Pero por si acaso, suponemos
247253 # que una expresión de 'sentido' inválida, hace incorrectas a todos los números.
248254 return False
249255
250- unidad_elegida = choice ([unidad for unidad in list (guia .keys ())[1 :]
251- if _expresion_busqueda (int (unidad ))])
256+ candidatos = [unidad for unidad in guia .keys () if _expresion_busqueda (int (unidad ))]
257+
258+ # El usuario eligió justo un parámetro de búsqueda que no retorna ningún candidato viable:
259+ # Un ejemplo sería si se elige la unidad 1 de pivote pero con parámetro "Antes de".
260+ # Naturalmente, no va a retornar nada.
261+ if not candidatos :
262+ await interaccion .response .send_message (
263+ content = "No hay unidades de la guía que coincidan con los parámetros de búsqueda "
264+ f"dados y la unidad pivote `{ unidad_pivote } . "
265+ f"{ guia [str (unidad_pivote )]['titulo' ]} `." ,
266+ ephemeral = True
267+ )
268+ return
269+
270+ unidad_elegida = choice (candidatos )
271+ # para que no agarre el titulo ni por casualidad, lo sacamos
272+ guia [unidad_elegida ].pop ("titulo" )
252273 ejercicio_elegido = choice (list (guia [unidad_elegida ].keys ()))
253274
254275 await self .mandar_ejercicio (interaccion ,
0 commit comments