99from discord .ext import commands
1010
1111from europython_discord .animals .clients import AnimalClient
12- from europython_discord .animals .config import AnimalsConfig
12+ from europython_discord .animals .config import AnimalKind , AnimalsConfig
1313
1414_logger = logging .getLogger (__name__ )
1515
@@ -29,45 +29,58 @@ def __init__(
2929 self ._last_usage_timestamp_by_user_id : OrderedDict [int , float ] = OrderedDict ()
3030 _logger .info ("Cog 'Animals' has been initialized" )
3131
32- async def _handle_animal_command (
33- self , ctx : commands .Context , animal : str , source_url : str
34- ) -> None :
32+ async def handle_animal_command (self , ctx : commands .Context , animal : AnimalKind ) -> None :
3533 if ctx .channel .name != self .config .channel_name :
3634 return
3735
3836 if self ._is_rate_limited (ctx .author .id ):
3937 return
4038
41- image_url = await self ._client .fetch_image (animal )
42- if image_url is None :
43- # Get the error messages for this specific animal
44- animal_config = getattr (self .config , animal )
45- message = random .choice (animal_config .error_messages ) # noqa: S311
46- await ctx .send (message )
39+ result = await self ._client .fetch_image (animal )
40+ if result is not None :
41+ embed = discord .Embed ()
42+ embed .description = f"Behold! A friendly { animal } appeared from { result .source } "
43+ embed .set_image (url = result .url )
44+ self ._update_rate_limit_cache (ctx .author .id )
45+ await ctx .send (embed = embed )
4746 return
4847
49- embed = discord .Embed ()
50- embed .description = f"Behold! A friendly { animal } appeared from { source_url } "
51- embed .set_image (url = image_url )
52-
53- self ._update_rate_limit_cache (ctx .author .id )
54- await ctx .send (embed = embed )
48+ await ctx .send (self ._error_message_for (animal ))
5549
5650 @commands .hybrid_command (name = "dog" , description = "Get a random dog picture" )
5751 async def dog_command (self , ctx : commands .Context ) -> None :
58- await self ._handle_animal_command (ctx , "dog" , "https://dog.ceo" )
52+ await self .handle_animal_command (ctx , AnimalKind . DOG )
5953
6054 @commands .hybrid_command (name = "cat" , description = "Get a random cat picture" )
6155 async def cat_command (self , ctx : commands .Context ) -> None :
62- await self ._handle_animal_command (ctx , "cat" , "https://cataas.com" )
56+ await self .handle_animal_command (ctx , AnimalKind . CAT )
6357
6458 @commands .hybrid_command (name = "duck" , description = "Get a random duck picture" )
6559 async def duck_command (self , ctx : commands .Context ) -> None :
66- await self ._handle_animal_command (ctx , "duck" , "https://random-d.uk" )
60+ await self .handle_animal_command (ctx , AnimalKind . DUCK )
6761
6862 @commands .hybrid_command (name = "fox" , description = "Get a random fox picture" )
6963 async def fox_command (self , ctx : commands .Context ) -> None :
70- await self ._handle_animal_command (ctx , "fox" , "https://randomfox.ca/floof/" )
64+ await self .handle_animal_command (ctx , AnimalKind .FOX )
65+
66+ async def cog_load (self ) -> None :
67+ existing = {"dog" , "cat" , "duck" , "fox" }
68+ for animal in AnimalKind :
69+ if animal in existing :
70+ continue
71+ cmd = _make_animality_command (animal , self )
72+ self .bot .add_command (cmd )
73+
74+ def _error_message_for (self , animal : AnimalKind ) -> str :
75+ if animal_config := self .config .config_by_kind .get (animal ):
76+ return random .choice (animal_config .error_messages ) # noqa: S311
77+
78+ if not self .config .animality_error_messages :
79+ return f"Couldn't find a { animal } picture right now."
80+ plural = f"{ animal } s" if animal != "fish" else "fish"
81+ return random .choice (self .config .animality_error_messages ).format ( # noqa: S311
82+ animal = animal , plural = plural
83+ )
7184
7285 def _is_rate_limited (self , user_id : int ) -> bool :
7386 last_usage_timestamp = self ._last_usage_timestamp_by_user_id .get (user_id , 0 )
@@ -81,3 +94,11 @@ def _update_rate_limit_cache(self, user_id: int) -> None:
8194 self ._last_usage_timestamp_by_user_id .move_to_end (user_id )
8295 if len (self ._last_usage_timestamp_by_user_id ) > _MAX_COOLDOWN_TRACKING :
8396 self ._last_usage_timestamp_by_user_id .popitem (last = False )
97+
98+
99+ def _make_animality_command (name : AnimalKind , cog : AnimalsCog ) -> commands .HybridCommand :
100+ @commands .hybrid_command (name = name , description = f"Get a random { name } picture" )
101+ async def callback (ctx : commands .Context ) -> None :
102+ await cog .handle_animal_command (ctx , name )
103+
104+ return callback
0 commit comments