From c85e1c909c4ea656d2c231ec125e356d41e8233e Mon Sep 17 00:00:00 2001 From: Esteban Vincent Date: Mon, 13 Oct 2025 18:53:23 +0200 Subject: [PATCH] =?UTF-8?q?feat(bot):=20add=20initial=20bot=20setup=20and?= =?UTF-8?q?=20commands=20=F0=9F=8E=89=20-=20Created=20a=20Dockerfile=20for?= =?UTF-8?q?=20containerization.=20-=20Added=20.env.example=20for=20environ?= =?UTF-8?q?ment=20variable=20configuration.=20-=20Implemented=20main=20bot?= =?UTF-8?q?=20functionality=20with=20meme=20command=20and=20webhook=20inte?= =?UTF-8?q?gration.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit *Generated by Github Copilot* --- .env.example | 3 +++ Dockerfile | 10 ++++++++ src/main.py | 61 ++++++++++++++++++++++++++++++++++++++++++++ src/requirements.txt | 2 ++ 4 files changed, 76 insertions(+) create mode 100644 .env.example create mode 100644 Dockerfile create mode 100644 src/main.py create mode 100644 src/requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8162b8a --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +DISCORD_TOKEN= +WEBHOOK_URL= +API_KEY= \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..50e773a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.12-slim + +WORKDIR /app + +COPY . . +RUN pip install --no-cache-dir -r src/requirements.txt + + +# Run the bot +CMD ["python", "src/main.py"] \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..db4ebe6 --- /dev/null +++ b/src/main.py @@ -0,0 +1,61 @@ +import os + +import discord +import httpx + +intents = discord.Intents.default() +client = discord.Client(intents=intents) +tree = discord.app_commands.CommandTree(client) + + +@client.event +async def on_ready(): + print(f"Bot logged in as {client.user}") + try: + await tree.sync() + print("Slash commands synced successfully.") + except Exception as e: + print(f"Failed to sync commands: {e}") + + +@tree.command(name="meme", description="Create a meme based on your prompt") +async def meme(interaction: discord.Interaction, prompt: str): + # Defer the response immediately to avoid timeout + await interaction.response.defer() + + try: + # Call the webhook with the prompt and API key + webhook_payload = {"query": prompt} + headers = {"apiKey": os.getenv("API_KEY"), "Content-Type": "application/json"} + async with httpx.AsyncClient() as httpx_client: + response = await httpx_client.post( + os.getenv("WEBHOOK_URL"), + json=webhook_payload, + headers=headers, + timeout=120, + ) + + if response.status_code == 200: + image_url = response.json()["data"]["url"] + embed = discord.Embed( + title="New meme just dropped!", description=prompt, color=0x00FF00 + ) + embed.set_image(url=image_url) + await interaction.followup.send(embed=embed) + else: + embed = discord.Embed( + title=f"Webhook call failed - {response.status_code}", + description=response.text, + color=0xFF0000, + ) + await interaction.followup.send(embed=embed) + + except Exception as e: + embed = discord.Embed( + title="Exception", + description=str(e), + color=0xFF0000, + ) + + +client.run(os.getenv("DISCORD_TOKEN")) diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..5608952 --- /dev/null +++ b/src/requirements.txt @@ -0,0 +1,2 @@ +discord.py==2.6.3 +httpx==0.28.1 \ No newline at end of file