66
77import discord
88from discord .ext import commands
9+ from discord .utils import get as discord_get
910
1011from europython_discord .animals import providers
1112from europython_discord .animals .config import AnimalsConfig
@@ -28,17 +29,31 @@ def __init__(
2829 self ._providers = providers_by_animal
2930 self ._rate_limiter = rate_limiter
3031
31- for animal in self ._providers :
32- self ._bot .add_command (self ._create_animal_command (animal ))
33-
3432 _logger .info ("Cog 'Animals' has been initialized" )
3533
34+ @commands .Cog .listener ()
35+ async def on_ready (self ) -> None :
36+ # (re-) register commands on each connected guild
37+ for guild in self ._bot .guilds :
38+ self ._bot .tree .clear_commands (guild = guild )
39+ for animal in self ._providers :
40+ self ._bot .tree .add_command (self ._create_animal_command (animal ), guild = guild )
41+ synced_commands = await self ._bot .tree .sync (guild = guild )
42+ _logger .info (
43+ "Synced %d application command(s) to guild %s" , len (synced_commands ), guild .id
44+ )
45+
3646 async def post_animal_picture (self , animal : str , ctx : commands .Context ) -> None :
47+ # send typing indicator
48+ await ctx .defer (ephemeral = False )
49+
50+ # check if command shall be executed
3751 if ctx .channel .name != self ._channel_name :
52+ channel = discord_get (ctx .guild .channels , name = self ._channel_name )
53+ await ctx .send (f"This command can only be used in { channel .mention } ." )
3854 return
3955 if self ._rate_limiter .is_rate_limited (ctx .author .id ):
40- return
41- if animal not in self ._providers :
56+ await ctx .send ("You are being rate limited. Please try again later." )
4257 return
4358
4459 provider = random .choice (self ._providers [animal ]) # noqa: S311 suspicious-non-cryptographic-random-usage
@@ -51,7 +66,13 @@ async def post_animal_picture(self, animal: str, ctx: commands.Context) -> None:
5166
5267 if image is None :
5368 _logger .error ("Failed to fetch %s pictures" , animal )
54- await ctx .send (f"Failed to fetch { animal } picture." )
69+ await ctx .send (
70+ (
71+ f"Failed to fetch { animal } picture. "
72+ f"If this happens repeatedly, please report it."
73+ ),
74+ ephemeral = True ,
75+ )
5576 return
5677
5778 embed = discord .Embed ()
@@ -60,9 +81,10 @@ async def post_animal_picture(self, animal: str, ctx: commands.Context) -> None:
6081 self ._rate_limiter .register_usage (ctx .author .id )
6182 await ctx .send (embed = embed )
6283
63- def _create_animal_command (self , animal : str ) -> commands .HybridCommand :
64- @commands .hybrid_command (name = animal , description = f"Get a random { animal } picture" )
65- async def callback (ctx : commands .Context ) -> None :
84+ def _create_animal_command (self , animal : str ) -> discord .app_commands .Command :
85+ @discord .app_commands .command (name = animal , description = f"Get a random { animal } picture" )
86+ async def callback (interaction : discord .Interaction ) -> None :
87+ ctx = await self ._bot .get_context (interaction )
6688 await self .post_animal_picture (animal , ctx )
6789
6890 return callback
0 commit comments