File size: 3,218 Bytes
700b1d7
 
c01a950
3f11bbc
 
700b1d7
3f11bbc
c139e5d
3f11bbc
700b1d7
 
 
c01a950
3f11bbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
700b1d7
 
3f11bbc
 
700b1d7
3f11bbc
dda26c7
3f11bbc
700b1d7
 
7678033
3f11bbc
 
c01a950
3f11bbc
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import discord
from discord import app_commands
import os
from dotenv import load_dotenv
import requests

load_dotenv()

# Set up intents
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

# GLIF Simple API endpoint
GLIF_API_URL = "https://simple-api.glif.app"


# Function to call the GLIF API
def generate_image(prompt, aspect_ratio):
    # Prepare the payload for the GLIF API
    payload = {
        "id": "cm3ugmzv2002gnckiosrwk6xi",  # Glif ID
        "inputs": [prompt, aspect_ratio]
    }

    # Set up headers with the API token
    headers = {"Authorization": f"Bearer {os.getenv('GLIF_API_TOKEN')}"}

    # Make the request to the GLIF API
    response = requests.post(GLIF_API_URL, json=payload, headers=headers)

    if response.status_code == 200:
        # Parse the response
        response_data = response.json()
        if "output" in response_data:
            # Return the image URL
            return response_data["output"]
        elif "error" in response_data:
            # Handle errors (even though the status code is 200)
            return f"Error: {response_data['error']}"
    else:
        return f"API request failed with status code: {response.status_code}"


# Define a slash command for image generation
@tree.command(name="generate",
              description="Generates an image based on a text prompt")
@app_commands.choices(aspect_ratio=[
    app_commands.Choice(name="1:1 (Square)", value="1:1"),
    app_commands.Choice(name="9:16 (Vertical)", value="9:16"),
    app_commands.Choice(name="16:9 (Horizontal)", value="16:9"),
    app_commands.Choice(name="4:5 (Portrait)", value="4:5"),
    app_commands.Choice(name="5:4 (Landscape)", value="5:4"),
    app_commands.Choice(name="3:4 (Portrait)", value="3:4"),
    app_commands.Choice(name="4:3 (Landscape)", value="4:3")
])
async def generate_command(interaction: discord.Interaction, prompt: str,
                           aspect_ratio: app_commands.Choice[str]):
    """Generates an image based on the user's prompt and aspect ratio."""
    await interaction.response.defer()  # Acknowledge the interaction

    try:
        image_url = generate_image(prompt, aspect_ratio.value)

        if image_url.startswith("http"):
            await interaction.followup.send(
                f"Here's your generated image based on the prompt '{prompt}' with aspect ratio {aspect_ratio.name}:\n{image_url}"
            )
        else:
            await interaction.followup.send(
                f"Sorry, I couldn't generate an image. {image_url}")
    except Exception as e:
        await interaction.followup.send(f"An error occurred: {e}")


# Define the hello slash command
@tree.command(name="hello", description="Says hello!")
async def hello_command(interaction):
    await interaction.response.send_message("Hello there!")


@client.event
async def on_ready():
    await tree.sync()  # Sync the slash commands with discord
    print("Bot is ready!")


# Get the token from an environment variable
token = os.getenv("DISCORD_BOT_TOKEN")

# check if the token is a string
if isinstance(token, str):
    client.run(token)
else:
    print("Invalid token format")