Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MinightDev committed Aug 16, 2023
1 parent fa205fb commit 5a6d390
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 53 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<p align="center">
<img src="https://starsky.pro/uploads/brand/cEKpGkKA8tD5P2nKh47CsigASMEwJCWRn4k3dRW5.png" alt="Starsky Bot Logo" width="200">
</p>

# Starsky Bot

Starsky Bot is a Discord bot that interacts with the Starsky API to generate documents and images using AI. This bot allows you to create documents based on templates, retrieve account information, and generate images based on prompts.
Expand Down Expand Up @@ -46,6 +50,14 @@ Display a help message with information about available commands.

- Ensure that you've obtained your Starsky API key from https://starsky.pro/developers before using the bot.

## Installation

Before starting the bot, run the `install.sh` script to install the required dependencies:

```bash
chmod +x install.sh
./install.sh

## Customization

Feel free to modify and customize this code to better suit your requirements. Whether you want to add new features, adjust command behavior, or integrate additional APIs, this code serves as a starting point for your own creative ideas. You're encouraged to make changes, experiment, and adapt the bot to your specific use cases.
Expand All @@ -54,3 +66,9 @@ Feel free to modify and customize this code to better suit your requirements. Wh
This project is licensed under the [MIT License](LICENSE).
---
To run the bot, make sure to insert your Discord bot token at the end of the `bot.py` file:
```python
bot.run('YOUR TOKEN GOES HERE')
89 changes: 36 additions & 53 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import requests
import urllib.parse
import asyncio
import os
import aiohttp
import random
import base64
import io

Expand All @@ -27,7 +30,7 @@
document_counter = 1

async def get_user_account():
global API_KEY # Use the global variable to store the API key
global API_KEY
if API_KEY:
headers = {
'Authorization': f'Bearer {API_KEY}',
Expand Down Expand Up @@ -58,7 +61,7 @@ async def fetch_document_details(document_id):
bot.remove_command('help')


user_image_limit = 10
user_image_limit = 10 # number img generations per user
user_image_counter = {}

@bot.command()
Expand All @@ -76,67 +79,47 @@ async def image(ctx, *, prompt):

headers = {
'Authorization': f'Bearer {API_KEY}',
'Accept': 'application/json'
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.get(API_URL, headers=headers)

if response.status_code != 200:
await ctx.send("Image generation failed. Please try again.")
return

data = response.json().get('data', [])
if data:
image = data[0]

image_url = image.get('url')

if not image_url:
await ctx.send("Image URL not found. Please try again.")
return

embed = discord.Embed(title="Starsky Bot Image Generator", color=discord.Color.brand_red())
embed.set_image(url=image_url)
embed.add_field(name="Prompt:", value=prompt, inline=False)

generated_message = await ctx.send(embed=embed)
await generated_message.add_reaction('🔄')

def check(reaction, user):
return user == ctx.author and str(reaction.emoji) == '🔄' and reaction.message.id == generated_message.id

try:
while True:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
await generated_message.remove_reaction('🔄', user)

response = requests.get(API_URL, headers=headers)
payload = {
'prompt': prompt,
'name': 'Generated Image',
'description': prompt,
'resolution': '1024x1024', #the higher the better
}

if response.status_code != 200:
await ctx.send("Image generation failed. Please try again.")
return
response = requests.post('https://starsky.pro/api/v1/images', headers=headers, data=payload)

data = response.json().get('data', [])
if data:
image = data[0]
if response.status_code == 201:
image_id = response.json().get('data', {}).get('id')

image_url = image.get('url')
# Fetch image details
image_details_response = requests.get(f'https://starsky.pro/api/v1/images/{image_id}', headers=headers)
if image_details_response.status_code == 200:
image_details = image_details_response.json().get('data', {})

if not image_url:
await ctx.send("Image URL not found. Please try again.")
return
image_url = image_details.get('url', '')
image_result = image_details.get('result', '')

embed.set_image(url=image_url)
embed = discord.Embed(title="Starsky Bot Image Generator", color=discord.Color.brand_red())
embed.add_field(name="Prompt:", value=prompt, inline=False)

await generated_message.edit(embed=embed)
else:
await ctx.send("Image generation failed. Please try again.")
return
except asyncio.TimeoutError:
await generated_message.remove_reaction('🔄', bot.user)
if image_url:
embed.set_image(url=image_url)
await ctx.send(embed=embed)
else:
await ctx.send("Failed to fetch the generated image.")
else:
await ctx.send("Failed to fetch image details.")
else:
await ctx.send("Image generation failed. Please try again.")

if not API_KEY:
await ctx.send("API key is not valid. Please run `$setup` to provide a valid API key.")


@bot.command()
async def account(ctx):
account_info = await get_user_account()
Expand Down Expand Up @@ -267,4 +250,4 @@ async def help(ctx):
embed.add_field(name="$setup", value="Set up your Starsky API key.", inline=False)
await ctx.send(embed=embed)

bot.run('Your bot token')
bot.run('YOUR TOKEN GOES HERE')
7 changes: 7 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
pip install -U discord.py
pip install aiohttp
pip install requests

# Run the bot
python bot.py

0 comments on commit 5a6d390

Please sign in to comment.