1+ # cogs/rickbot/cmds_botinfo.py
12"""
23(c) 2024 Lagden Development (All Rights Reserved)
34Licensed for non-commercial use with attribution required; provided 'as is' without warranty.
67RickBot Bot Info Chat Commands Cog
78
89This module provides a cog with commands to retrieve information about the bot,
9- including recent GitHub updates and latency checks. It is part of the RickBot default cog set.
10+ including recent GitHub updates, latency checks, and general bot information.
11+ It is part of the RickBot default cog set.
1012"""
1113
14+ from typing import Optional
1215from discord .ext import commands
1316import discord
1417
@@ -24,8 +27,8 @@ class RickBot_BotInfoCommands(commands.Cog):
2427 """
2528 A cog that provides commands to retrieve information about the bot.
2629
27- This cog includes functionality to check for updates from the bot's GitHub repository
28- and to ping the bot to check its latency.
30+ This cog includes functionality to check for updates from the bot's GitHub repository,
31+ ping the bot to check its latency, and get general bot information .
2932
3033 Attributes:
3134 bot (commands.Bot): The instance of the bot.
@@ -41,12 +44,30 @@ def __init__(self, bot: commands.Bot):
4144 bot (commands.Bot): The instance of the bot to which this cog is being added.
4245 """
4346 self .bot = bot
44- self .github_repo = CONFIG ["REPO" ][ "url" ]
47+ self .github_repo = CONFIG ["REPO" ]. get ( "url" , "" ). strip ()
4548 self .github_api = (
4649 convert_repo_url_to_api (self .github_repo ) if self .github_repo else None
4750 )
4851
49- @commands .command (name = "updates" )
52+ async def create_error_embed (self , title : str , description : str ) -> discord .Embed :
53+ """
54+ Create a standardized error embed message.
55+
56+ Args:
57+ title (str): The title for the error embed.
58+ description (str): The description/message for the error embed.
59+
60+ Returns:
61+ discord.Embed: The formatted error embed.
62+ """
63+ embed = discord .Embed (
64+ title = title , description = description , color = ERROR_EMBED_COLOR
65+ )
66+ embed .set_footer (text = "🛠️ RickBot - A project by lagden.dev" )
67+ return embed
68+
69+ @commands .command (name = "updates" , help = "Check GitHub for the latest commits" )
70+ @commands .cooldown (1 , 30 , commands .BucketType .user )
5071 async def _updates (self , ctx : commands .Context ) -> None :
5172 """
5273 Retrieve and display the latest commits from the bot's GitHub repository.
@@ -58,19 +79,24 @@ async def _updates(self, ctx: commands.Context) -> None:
5879 Args:
5980 ctx (commands.Context): The context in which the command was called.
6081 """
61- if not self .github_repo or not self .github_repo .strip ():
62- embed = discord .Embed (
63- title = "Sorry!" ,
64- description = "This command is disabled." ,
65- color = ERROR_EMBED_COLOR ,
82+ try :
83+ if not self .github_repo :
84+ embed = await self .create_error_embed (
85+ "Sorry!" , "The updates command is currently disabled."
86+ )
87+ else :
88+ embed = get_github_updates (self .github_repo )
89+
90+ await ctx .reply (embed = embed , mention_author = False )
91+
92+ except Exception as e :
93+ error_embed = await self .create_error_embed (
94+ "Error!" , f"Failed to fetch updates: { str (e )} "
6695 )
67- embed .set_footer (text = "🛠️ RickBot - A project by lagden.dev" )
68- else :
69- embed = get_github_updates (self .github_repo )
96+ await ctx .reply (embed = error_embed , mention_author = False )
7097
71- await ctx .reply (embed = embed , mention_author = False )
72-
73- @commands .command (name = "ping" )
98+ @commands .command (name = "ping" , help = "Check the bot's latency" )
99+ @commands .cooldown (1 , 10 , commands .BucketType .user )
74100 async def _ping (self , ctx : commands .Context ) -> None :
75101 """
76102 Check and display the bot's current latency.
@@ -81,11 +107,84 @@ async def _ping(self, ctx: commands.Context) -> None:
81107 Args:
82108 ctx (commands.Context): The context in which the command was called.
83109 """
84- latency = round (self .bot .latency * 1000 )
85- embed = discord .Embed (
86- title = "Pong!" , description = f"Latency: { latency } ms" , color = MAIN_EMBED_COLOR
87- )
88- await ctx .reply (embed = embed , mention_author = False )
110+ try :
111+ latency = round (self .bot .latency * 1000 )
112+ embed = discord .Embed (
113+ title = "Pong!" ,
114+ description = f"🏓 Latency: { latency } ms" ,
115+ color = MAIN_EMBED_COLOR ,
116+ )
117+ embed .set_footer (text = "🛠️ RickBot - A project by lagden.dev" )
118+ await ctx .reply (embed = embed , mention_author = False )
119+
120+ except Exception as e :
121+ error_embed = await self .create_error_embed (
122+ "Error!" , f"Failed to check latency: { str (e )} "
123+ )
124+ await ctx .reply (embed = error_embed , mention_author = False )
125+
126+ @commands .command (name = "info" , help = "Get information about the bot" )
127+ @commands .cooldown (1 , 30 , commands .BucketType .user )
128+ async def _info (self , ctx : commands .Context ) -> None :
129+ """
130+ Provide general information about the bot.
131+
132+ This command displays various details about the bot, including its version,
133+ developer information, and GitHub repository link (if available).
134+
135+ Args:
136+ ctx (commands.Context): The context in which the command was called.
137+ """
138+ try :
139+ embed = discord .Embed (
140+ title = "RickBot Information" ,
141+ description = "RickBot is a versatile Discord bot developed by Lagden Development." ,
142+ color = MAIN_EMBED_COLOR ,
143+ )
144+ embed .add_field (
145+ name = "Version" ,
146+ value = CONFIG ["VERSION" ].get ("version" , "Unknown" ),
147+ inline = True ,
148+ )
149+ embed .add_field (
150+ name = "Developer" ,
151+ value = f"<@{ CONFIG ['MAIN' ].get ('dev' , 'Unknown' )} >" ,
152+ inline = True ,
153+ )
154+ embed .add_field (
155+ name = "GitHub" , value = self .github_repo or "Not available" , inline = False
156+ )
157+ embed .set_footer (text = "🛠️ RickBot - A project by lagden.dev" )
158+ await ctx .reply (embed = embed , mention_author = False )
159+
160+ except Exception as e :
161+ error_embed = await self .create_error_embed (
162+ "Error!" , f"Failed to fetch bot info: { str (e )} "
163+ )
164+ await ctx .reply (embed = error_embed , mention_author = False )
165+
166+ @_updates .error
167+ @_ping .error
168+ @_info .error
169+ async def command_error (self , ctx : commands .Context , error : Exception ) -> None :
170+ """
171+ Handle errors that occur in commands.
172+
173+ Args:
174+ ctx (commands.Context): The context in which the error occurred.
175+ error (Exception): The error that occurred.
176+ """
177+ if isinstance (error , commands .CommandOnCooldown ):
178+ embed = await self .create_error_embed (
179+ "Slow down!" ,
180+ f"Please wait { error .retry_after :.1f} s before using this command again." ,
181+ )
182+ await ctx .reply (embed = embed , mention_author = False )
183+ else :
184+ embed = await self .create_error_embed (
185+ "Error!" , "An unexpected error occurred. Please try again later."
186+ )
187+ await ctx .reply (embed = embed , mention_author = False )
89188
90189
91190async def setup (bot : commands .Bot ) -> None :
0 commit comments