From e1d62585ee6f6dfe6839898eafa1a84cd474c15a Mon Sep 17 00:00:00 2001 From: driftywinds Date: Sun, 11 May 2025 18:28:28 +0000 Subject: [PATCH] implement ephemeral responses for servers --- DSbot.py | 152 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 78 insertions(+), 74 deletions(-) diff --git a/DSbot.py b/DSbot.py index 11846e2..bdc2a60 100644 --- a/DSbot.py +++ b/DSbot.py @@ -91,53 +91,64 @@ class ResultButton(Button): @bot.event async def on_ready(): log_activity(f'Logged in as {bot.user.name}') + try: + synced = await bot.tree.sync() + log_activity(f"Synced {len(synced)} commands") + except Exception as e: + log_activity(f"Command sync error: {str(e)}") + +async def validate_channel(ctx): + if isinstance(ctx.channel, discord.DMChannel) or ctx.channel.id in ALLOWED_CHANNELS: + return True + embed = discord.Embed( + description="❌ This command is not available in this channel.", + color=discord.Color.red() + ) + await ctx.send(embed=embed, ephemeral=True, delete_after=5) + return False + +@bot.hybrid_command(name="mpbot", description="Search addons or lookup by UUID") +async def mpbot(ctx: commands.Context, *, query: str): + """Main command handler for addon searches""" + if not await validate_channel(ctx): + return + + # Delete original message if in guild channel + if ctx.guild and ctx.interaction is None: + try: + await ctx.message.delete() + except (discord.NotFound, discord.Forbidden): + pass + + query = query.strip().lower() + + if query == "help": + await send_start(ctx) + elif query == "cancel": + await send_cancel(ctx) + elif UUID_PATTERN.match(query): + await process_uuid(ctx, query) + else: + await perform_search(ctx, query) async def send_start(ctx): embed = discord.Embed( title="Addon Search Bot", description="I can help you find addons!\n\n" - "🔍 **Search**: `!mpbot [search terms]`\n" - "🔎 **UUID Lookup**: `!mpbot [uuid]`\n" - "🆘 **Help**: `!mpbot help`\n" - "❌ **Cancel**: `!mpbot cancel`", + "🔍 **Search**: `/mpbot [search terms]`\n" + "🔎 **UUID Lookup**: `/mpbot [uuid]`\n" + "🆘 **Help**: `/mpbot help`\n" + "❌ **Cancel**: `/mpbot cancel`", color=discord.Color.green() ) - await ctx.send(embed=embed) + await ctx.send(embed=embed, ephemeral=True) async def send_cancel(ctx): embed = discord.Embed( description="⚠️ No active operation to cancel", color=discord.Color.orange() ) - await ctx.send(embed=embed, delete_after=5) - -@bot.event -async def on_message(message): - if message.author == bot.user: - return - - # Allow DMs and specified channels - if not isinstance(message.channel, discord.DMChannel) and message.channel.id not in ALLOWED_CHANNELS: - return - - if not message.content.startswith('!mpbot'): - return - - ctx = await bot.get_context(message) - content = message.content[len('!mpbot'):].strip() - - if not content: - await send_start(ctx) - return - - if content.lower() == 'help': - await send_start(ctx) - elif content.lower() == 'cancel': - await send_cancel(ctx) - elif UUID_PATTERN.match(content): - await process_uuid(ctx, content) - else: - await perform_search(ctx, content) + await ctx.send(embed=embed, ephemeral=True, delete_after=5) async def perform_search(ctx, query): try: @@ -204,52 +215,45 @@ async def process_uuid(ctx, uuid): response.raise_for_status() data = response.json().get('data', {}) - if data: - download_hashes = data.get('download_hash', '') - decoded_urls = [] - - if download_hashes: - for i, hash_part in enumerate(download_hashes.split(','), 1): - try: - decoded = base64.b64decode(hash_part.strip()).decode('utf-8') - decoded_urls.append(f"{i}. {decoded}") - except: - decoded_urls.append(f"{i}. Invalid hash") - - embed = discord.Embed( - title=data.get('name', 'N/A'), - color=discord.Color.blue() - ) - embed.add_field(name="UUID", value=data.get('uuid', 'N/A'), inline=False) - embed.add_field(name="Creator", value=data.get('creator', 'N/A'), inline=True) - embed.add_field(name="Version", value=data.get('version', 'N/A'), inline=True) - - if decoded_urls: - embed.add_field( - name="Download URLs", - value="\n".join(decoded_urls), - inline=False - ) - - await ctx.send(embed=embed, ephemeral=True) - else: + if not data: embed = discord.Embed( description="❌ No information found for this UUID.", color=discord.Color.red() ) await ctx.send(embed=embed, ephemeral=True) - + return + + download_hashes = data.get('download_hash', '') + decoded_urls = [] + + if download_hashes: + for i, hash_part in enumerate(download_hashes.split(','), 1): + try: + decoded = base64.b64decode(hash_part.strip()).decode('utf-8') + decoded_urls.append(f"{i}. {decoded}") + except: + decoded_urls.append(f"{i}. Invalid hash") + + embed = discord.Embed( + title=data.get('name', 'N/A'), + color=discord.Color.blue() + ) + embed.add_field(name="UUID", value=data.get('uuid', 'N/A'), inline=False) + embed.add_field(name="Creator", value=data.get('creator', 'N/A'), inline=True) + embed.add_field(name="Version", value=data.get('version', 'N/A'), inline=True) + + if decoded_urls: + embed.add_field( + name="Download URLs", + value="\n".join(decoded_urls), + inline=False + ) + + await ctx.send(embed=embed, ephemeral=True) + except requests.exceptions.HTTPError as e: - if e.response.status_code == 403: - embed = discord.Embed( - description="⛔ Access denied for this UUID", - color=discord.Color.red() - ) - else: - embed = discord.Embed( - description=f"🔧 API Error: {e.response.status_code}", - color=discord.Color.red() - ) + description = f"🔧 API Error: {e.response.status_code}" if e.response.status_code != 403 else "⛔ Access denied for this UUID" + embed = discord.Embed(description=description, color=discord.Color.red()) await ctx.send(embed=embed, ephemeral=True) except Exception as e: log_activity(f"UUID processing error: {str(e)}")