implement ephemeral responses for servers

This commit is contained in:
driftywinds
2025-05-11 18:28:28 +00:00
parent 4e71faf0fe
commit e1d62585ee

152
DSbot.py
View File

@@ -91,53 +91,64 @@ class ResultButton(Button):
@bot.event @bot.event
async def on_ready(): async def on_ready():
log_activity(f'Logged in as {bot.user.name}') 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): async def send_start(ctx):
embed = discord.Embed( embed = discord.Embed(
title="Addon Search Bot", title="Addon Search Bot",
description="I can help you find addons!\n\n" description="I can help you find addons!\n\n"
"🔍 **Search**: `!mpbot [search terms]`\n" "🔍 **Search**: `/mpbot [search terms]`\n"
"🔎 **UUID Lookup**: `!mpbot [uuid]`\n" "🔎 **UUID Lookup**: `/mpbot [uuid]`\n"
"🆘 **Help**: `!mpbot help`\n" "🆘 **Help**: `/mpbot help`\n"
"❌ **Cancel**: `!mpbot cancel`", "❌ **Cancel**: `/mpbot cancel`",
color=discord.Color.green() color=discord.Color.green()
) )
await ctx.send(embed=embed) await ctx.send(embed=embed, ephemeral=True)
async def send_cancel(ctx): async def send_cancel(ctx):
embed = discord.Embed( embed = discord.Embed(
description="⚠️ No active operation to cancel", description="⚠️ No active operation to cancel",
color=discord.Color.orange() color=discord.Color.orange()
) )
await ctx.send(embed=embed, delete_after=5) await ctx.send(embed=embed, ephemeral=True, 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)
async def perform_search(ctx, query): async def perform_search(ctx, query):
try: try:
@@ -204,52 +215,45 @@ async def process_uuid(ctx, uuid):
response.raise_for_status() response.raise_for_status()
data = response.json().get('data', {}) data = response.json().get('data', {})
if data: if not 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:
embed = discord.Embed( embed = discord.Embed(
description="❌ No information found for this UUID.", description="❌ No information found for this UUID.",
color=discord.Color.red() color=discord.Color.red()
) )
await ctx.send(embed=embed, ephemeral=True) 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: except requests.exceptions.HTTPError as e:
if e.response.status_code == 403: description = f"🔧 API Error: {e.response.status_code}" if e.response.status_code != 403 else "⛔ Access denied for this UUID"
embed = discord.Embed( embed = discord.Embed(description=description, color=discord.Color.red())
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()
)
await ctx.send(embed=embed, ephemeral=True) await ctx.send(embed=embed, ephemeral=True)
except Exception as e: except Exception as e:
log_activity(f"UUID processing error: {str(e)}") log_activity(f"UUID processing error: {str(e)}")