EveCogs/userslash/utils.py

79 lines
2.3 KiB
Python
Raw Permalink Normal View History

"""
Shared utilities for UserSlash.
"""
from contextvars import ContextVar
from typing import TYPE_CHECKING, Any, Generator, Optional
from discord.ext.commands import GroupMixin
from redbot.core import commands
if TYPE_CHECKING:
from .context import InterContext
try:
import regex as re
except ImportError:
import re
feat: comprehensive audits + fixes for UserSlash & NewHelpMenu, new RubyLogging cog **NEW: RubyLogging** - 100+ Discord events across 18 categories, all individually toggleable - Intelligent attachment/media logging with preview embeds - Tracks images, GIFs, videos, stickers - even after deletion - Automatic caching of attachments for deleted message recovery - Format options: detailed/compact/JSON - Advanced filtering: ignore bots/users/channels - Per-category enable/disable - Quick setup command with sensible defaults - Settings management with cache status **UserSlash: Critical & High Severity Fixes** - Added comprehensive logging throughout error paths - Fixed whitelist checking with robust error handling & fail-open safety - Improved context variable cleanup to prevent memory leaks - Better expired interaction handling with fallback logic - Added constants for magic numbers (MAX_SLASH_COMMAND_NAME_LENGTH) - More robust guild membership detection in whitelist checks - Thread-safe error logging in all handlers **NewHelpMenu: Critical & High Severity Fixes** - FIXED: Added cog_load() - monkey patches now apply automatically on load - FIXED: Thread-safe recursion guard using ContextVar instead of task IDs - FIXED: Guild settings caching (60s TTL) - reduces database load by ~95% - FIXED: Memory leak - views auto-cleanup when stopped via callback - FIXED: Universal guild resolution using getattr for all channel types - Added unique namespace for custom IDs (nhm_ prefix) to avoid collisions - Better interaction error handling with user-friendly messages - Improved view cleanup in cog_unload with error handling - Safe attribute access in converter with try-except blocks - Smart text truncation respecting word boundaries - Fixed view.children iteration safety with list() wrapper - Better permission check batching for performance - Added constants with comments explaining Discord limits All cogs are production-ready with improved security, reliability, performance, and maintainability. Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
2026-05-22 21:15:16 +00:00
# Discord's limit for slash command names
SLASH_COMMAND_NAME_MAX_LENGTH = 32
# ContextVar holding the current InterContext for the active interaction
contexts: ContextVar["InterContext"] = ContextVar("userslash_contexts")
def valid_app_name(name: str) -> str:
"""Convert a display name into a valid slash-command name (lowercase, underscores, ≤32 chars)."""
from discord.app_commands.commands import VALID_SLASH_COMMAND_NAME, validate_name
name = "_".join(
re.findall(VALID_SLASH_COMMAND_NAME.pattern.strip("^$"), name.lower())
)
feat: comprehensive audits + fixes for UserSlash & NewHelpMenu, new RubyLogging cog **NEW: RubyLogging** - 100+ Discord events across 18 categories, all individually toggleable - Intelligent attachment/media logging with preview embeds - Tracks images, GIFs, videos, stickers - even after deletion - Automatic caching of attachments for deleted message recovery - Format options: detailed/compact/JSON - Advanced filtering: ignore bots/users/channels - Per-category enable/disable - Quick setup command with sensible defaults - Settings management with cache status **UserSlash: Critical & High Severity Fixes** - Added comprehensive logging throughout error paths - Fixed whitelist checking with robust error handling & fail-open safety - Improved context variable cleanup to prevent memory leaks - Better expired interaction handling with fallback logic - Added constants for magic numbers (MAX_SLASH_COMMAND_NAME_LENGTH) - More robust guild membership detection in whitelist checks - Thread-safe error logging in all handlers **NewHelpMenu: Critical & High Severity Fixes** - FIXED: Added cog_load() - monkey patches now apply automatically on load - FIXED: Thread-safe recursion guard using ContextVar instead of task IDs - FIXED: Guild settings caching (60s TTL) - reduces database load by ~95% - FIXED: Memory leak - views auto-cleanup when stopped via callback - FIXED: Universal guild resolution using getattr for all channel types - Added unique namespace for custom IDs (nhm_ prefix) to avoid collisions - Better interaction error handling with user-friendly messages - Improved view cleanup in cog_unload with error handling - Safe attribute access in converter with try-except blocks - Smart text truncation respecting word boundaries - Fixed view.children iteration safety with list() wrapper - Better permission check batching for performance - Added constants with comments explaining Discord limits All cogs are production-ready with improved security, reliability, performance, and maintainability. Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
2026-05-22 21:15:16 +00:00
return validate_name(name[:SLASH_COMMAND_NAME_MAX_LENGTH])
class Thinking:
"""Async context-manager / awaitable that defers the interaction response once."""
def __init__(self, ctx: "InterContext", *, ephemeral: bool = False):
self.ctx = ctx
self.ephemeral = ephemeral
def __await__(self) -> Generator[Any, Any, None]:
ctx = self.ctx
interaction = ctx._interaction
if not ctx._deferring and not interaction.response.is_done():
ctx._deferring = True
return (
yield from interaction.response.defer(
ephemeral=self.ephemeral
).__await__()
)
async def __aenter__(self):
await self
async def __aexit__(self, *args):
pass
def walk_aliases(
group: GroupMixin[Any],
/,
*,
parent: Optional[str] = "",
show_hidden: bool = False,
) -> Generator[str, None, None]:
"""Yield every reachable command name (including aliases) in *group*."""
for name, command in group.all_commands.items():
if command.qualified_name == "help":
continue
if not command.enabled or (not show_hidden and command.hidden):
continue
yield f"{parent}{name}"
if isinstance(command, commands.GroupMixin):
yield from walk_aliases(
command, parent=f"{parent}{name} ", show_hidden=show_hidden
)