Skip to content

Instantly share code, notes, and snippets.

@jadc
Created June 15, 2022 18:42
Show Gist options
  • Save jadc/dd9d5b14bc4ff4345b19ad7156940723 to your computer and use it in GitHub Desktop.
Save jadc/dd9d5b14bc4ff4345b19ad7156940723 to your computer and use it in GitHub Desktop.
Discord bot as an interface for DALL-E Playground (https://github.com/saharmor/dalle-playground)
import discord # pip install discord.py
import aiohttp, io, base64
### EDIT HERE ###
token = ''
backend_url = ''
#################
bot = discord.Client()
bot.queue = set()
@bot.event
async def on_message(msg):
ping = f'<@{bot.user.id}> '
if msg.content.startswith(ping):
query = msg.content.replace(ping, '')
if( len(query) > 0 and msg.author.id not in bot.queue ):
print(f'\'{msg.author.name}\' -> \'{query}\'')
bot.queue.add(msg.author.id)
await msg.add_reaction('✅')
req_url = backend_url + '/dalle'
req_headers = {'Bypass-Tunnel-Reminder': 'go', 'mode': 'no-cors'}
req_body = {'text': query, 'num_images': 1}
async with aiohttp.ClientSession(headers = req_headers) as session:
async with session.post(req_url, json = req_body) as resp:
res = await resp.json()
file = discord.File( io.BytesIO( base64.b64decode(res[0]) ), filename=f'{query}.jpg' )
await msg.channel.send(file=file, reference=msg)
bot.queue.remove(msg.author.id)
else:
await msg.add_reaction('❌')
bot.run(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment