Files
bot.py
downloads.json
requirements.txt
Code
3 134
bot.py
import discord
from discord.ext import commands
from discord import app_commands
import os
import json
from dotenv import load_dotenv
load_dotenv()
DOWNLOADS_FILE = "downloads.json"
def load_downloads():
with open(DOWNLOADS_FILE, "r") as f:
return json.load(f)
def save_downloads(data):
with open(DOWNLOADS_FILE, "w") as f:
json.dump(data, f, indent=2)
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
tree = bot.tree
class DownloadSelect(discord.ui.Select):
def __init__(self, downloads):
options = [
discord.SelectOption(
label=item["name"][:100],
description=item["description"][:100] if item.get("description") else None,
value=str(i)
)
for i, item in enumerate(downloads)
]
super().__init__(placeholder="Choose a file to download...", options=options)
self.downloads = downloads
async def callback(self, interaction: discord.Interaction):
item = self.downloads[int(self.values[0])]
embed = discord.Embed(
title=f"⬇️ {item['name']}",
description=item.get("description", ""),
color=discord.Color.green()
)
embed.add_field(name="Download Link", value=item["url"], inline=False)
await interaction.response.send_message(embed=embed, ephemeral=True)
class DownloadView(discord.ui.View):
def __init__(self, downloads):
super().__init__(timeout=60)
self.add_item(DownloadSelect(downloads))
download_group = app_commands.Group(name="download", description="Download menu commands")
@download_group.command(name="menu", description="Browse and download available files")
async def download_menu(interaction: discord.Interaction):
downloads = load_downloads()
if not downloads:
await interaction.response.send_message(
"❌ No downloads have been added yet.", ephemeral=True
)
return
embed = discord.Embed(
title="📦 Download Menu",
description="Select a file from the dropdown below to get its download link.",
color=discord.Color.blurple()
)
embed.set_footer(text=f"{len(downloads)} file(s) available")
view = DownloadView(downloads)
await interaction.response.send_message(embed=embed, view=view)
@download_group.command(name="add", description="Add a new download entry (Admin only)")
@app_commands.describe(name="Display name of the file", url="Direct download URL", description="Short description of the file")
@app_commands.default_permissions(administrator=True)
async def download_add(interaction: discord.Interaction, name: str, url: str, description: str = ""):
downloads = load_downloads()
for item in downloads:
if item["name"].lower() == name.lower():
await interaction.response.send_message(
f"❌ An entry named **{name}** already exists.", ephemeral=True
)
return
downloads.append({"name": name, "url": url, "description": description})
save_downloads(downloads)
await interaction.response.send_message(
f"✅ Added **{name}** to the download menu.", ephemeral=True
)
@download_group.command(name="remove", description="Remove a download entry (Admin only)")
@app_commands.describe(name="Name of the entry to remove")
@app_commands.default_permissions(administrator=True)
async def download_remove(interaction: discord.Interaction, name: str):
downloads = load_downloads()
updated = [item for item in downloads if item["name"].lower() != name.lower()]
if len(updated) == len(downloads):
await interaction.response.send_message(
f"❌ No entry named **{name}** found.", ephemeral=True
)
return
save_downloads(updated)
await interaction.response.send_message(
f"✅ Removed **{name}** from the download menu.", ephemeral=True
)
@download_group.command(name="list", description="List all download entries (Admin only)")
@app_commands.default_permissions(administrator=True)
async def download_list(interaction: discord.Interaction):
downloads = load_downloads()
if not downloads:
await interaction.response.send_message("📭 No downloads added yet.", ephemeral=True)
return
embed = discord.Embed(title="📋 All Download Entries", color=discord.Color.blurple())
for i, item in enumerate(downloads, 1):
embed.add_field(
name=f"{i}. {item['name']}",
value=f"**URL:** {item['url']}\n**Desc:** {item.get('description') or 'None'}",
inline=False
)
await interaction.response.send_message(embed=embed, ephemeral=True)
tree.add_command(download_group)
@bot.event
async def on_ready():
await tree.sync()
print(f"✅ Logged in as {bot.user} | Slash commands synced")
bot.run(os.getenv("DISCORD_TOKEN"))