Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
57 changes: 57 additions & 0 deletions tux/cogs/fun/cowsay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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
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))