File size: 1,766 Bytes
c01a950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import discord
from discord.ext import commands
import requests

# Load the bot token and API key from environment variables
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
GLIF_API_KEY = os.getenv("GLIF_API_KEY")
GLIF_API_URL = "https://simple-api.glif.app"

# Create a bot instance with the command prefix '!'
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="/imagine", intents=intents)

# Function to call the GLIF API
def generate_image(prompt):
    aspect_ratio = "9:16"  # Hardcoded aspect ratio
    payload = {
        "id": "cm3ugmzv2002gnckiosrwk6xi",  # Your GLIF ID
        "inputs": [prompt, aspect_ratio]
    }
    headers = {"Authorization": f"Bearer {GLIF_API_KEY}"}
    
    try:
        response = requests.post(GLIF_API_URL, json=payload, headers=headers)
        if response.status_code == 200:
            response_data = response.json()
            if "output" in response_data:
                return response_data["output"]  # Image URL
            elif "error" in response_data:
                return f"Error: {response_data['error']}"
        else:
            return f"API request failed with status code: {response.status_code}"
    except Exception as e:
        return f"Error: {str(e)}"

# Command to generate an image
@bot.command(name="generate")
async def generate(ctx, *, prompt: str):
    await ctx.send(f"Generating an image for: `{prompt}`...")
    image_url = generate_image(prompt)
    
    if image_url.startswith("http"):
        await ctx.send(image_url)  # Send the image URL
    else:
        await ctx.send(f"Failed to generate image: {image_url}")

# Event: Bot is ready
@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")

# Run the bot
bot.run(DISCORD_BOT_TOKEN)