Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tux/cogs/fun/cowsay.py
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}```")
Copy link
Contributor

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.

Suggested change
# 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}```")
# Enforce character limit and sanitize input
max_length = 200
sanitized_text = ''.join(c for c in text if c.isprintable()).strip()
# Remove Markdown-sensitive characters (backticks and triple backticks)
sanitized_text = sanitized_text.replace("`", "")
sanitized_text = sanitized_text.replace("```", "")
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
cow_text = cowsay.get_output_string("cow", sanitized_text)
await ctx.send(content=f"```{cow_text}```")
cow_text = cowsay.get_output_string("cow", sanitized_text)
# Discord message limit is 2000 characters
max_discord_length = 2000
cow_text_wrapped = f"```{cow_text}```"
if len(cow_text_wrapped) <= max_discord_length:
await ctx.send(content=cow_text_wrapped)
else:
# Split cow_text into chunks that fit within the Discord limit, accounting for code block markers
code_block_start = "```"
code_block_end = "```"
max_chunk_length = max_discord_length - len(code_block_start) - len(code_block_end)
for i in range(0, len(cow_text), max_chunk_length):
chunk = cow_text[i:i+max_chunk_length]
await ctx.send(content=f"{code_block_start}{chunk}{code_block_end}")


async def setup(bot: Tux) -> None:
await bot.add_cog(Cowsay(bot))