-
-
Notifications
You must be signed in to change notification settings - Fork 41
Better version of cowsay #1028
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
Closed
Closed
Better version of cowsay #1028
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||
| import cowsay | ||||||||||||||||||||||||||||||||||
| from discord.ext import commands | ||||||||||||||||||||||||||||||||||
| from tux.bot import Tux | ||||||||||||||||||||||||||||||||||
| from tux.utils.functions import generate_usage | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Initialize cooldown tracking | ||||||||||||||||||||||||||||||||||
| user_cooldowns = {} | ||||||||||||||||||||||||||||||||||
| cooldown_duration = 180 # seconds | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class Cowsay(commands.Cog): | ||||||||||||||||||||||||||||||||||
| def __init__(self, bot: Tux) -> None: | ||||||||||||||||||||||||||||||||||
| self.bot = bot | ||||||||||||||||||||||||||||||||||
| self.cowsay_command.usage = generate_usage(self.cowsay_command) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @commands.hybrid_command( | ||||||||||||||||||||||||||||||||||
| name="cowsay", | ||||||||||||||||||||||||||||||||||
| description="Make the cow say something.", | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| @commands.guild_only() | ||||||||||||||||||||||||||||||||||
| async def cowsay_command( | ||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||
| ctx: commands.Context[Tux], | ||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||
| text: str | ||||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||||
| user_id = ctx.author.id | ||||||||||||||||||||||||||||||||||
| now = time.time() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if user_id in user_cooldowns: | ||||||||||||||||||||||||||||||||||
| elapsed = now - user_cooldowns[user_id] | ||||||||||||||||||||||||||||||||||
| if elapsed < cooldown_duration: | ||||||||||||||||||||||||||||||||||
| remaining = int(cooldown_duration - elapsed) | ||||||||||||||||||||||||||||||||||
| await ctx.respond( | ||||||||||||||||||||||||||||||||||
| f"⏳ You're on cooldown! Try again in {remaining} seconds.", | ||||||||||||||||||||||||||||||||||
| ephemeral=True | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| user_cooldowns[user_id] = now | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Enforce character limit and sanitize input | ||||||||||||||||||||||||||||||||||
| max_length = 200 | ||||||||||||||||||||||||||||||||||
| sanitized_text = ''.join(c for c in text if c.isprintable()).strip() | ||||||||||||||||||||||||||||||||||
| if len(sanitized_text) > max_length: | ||||||||||||||||||||||||||||||||||
| await ctx.send(content=f"❌ Input too long! Please limit your message to {max_length} characters.") | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| cow_text = cowsay.get_output_string("cow", sanitized_text) | ||||||||||||||||||||||||||||||||||
| await ctx.send(content=f"```{cow_text}```") | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Potential for cowsay output to exceed Discord's message length limit. Check cow_text length before sending, and truncate or split if it exceeds Discord's 2000 character limit.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| async def setup(bot: Tux) -> None: | ||||||||||||||||||||||||||||||||||
| await bot.add_cog(Cowsay(bot)) | ||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Sanitization may not prevent all unwanted input (e.g., backticks or code block escapes).
Consider escaping or removing backticks and other Markdown-sensitive characters to avoid Discord code block formatting issues.