Upload PromptBot.py
Browse files- PromptBot.py +64 -0
PromptBot.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import discord
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import asyncio
|
| 6 |
+
|
| 7 |
+
# Create an instance of Intents
|
| 8 |
+
intents = discord.Intents.default()
|
| 9 |
+
intents.message_content = True # Enable message content events
|
| 10 |
+
|
| 11 |
+
client = discord.Client(intents=intents)
|
| 12 |
+
|
| 13 |
+
@client.event
|
| 14 |
+
async def on_ready():
|
| 15 |
+
print(f'We have logged in as {client.user}')
|
| 16 |
+
|
| 17 |
+
@client.event
|
| 18 |
+
async def on_message(message):
|
| 19 |
+
if message.author == client.user:
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
# Change the command trigger to "/prompt"
|
| 23 |
+
if message.content.startswith('/prompt'):
|
| 24 |
+
input_text = message.content[len('/prompt'):].strip()
|
| 25 |
+
if input_text:
|
| 26 |
+
# Mention the user in the response
|
| 27 |
+
user_mention = message.author.mention
|
| 28 |
+
|
| 29 |
+
print(f'Received message: {message.content}')
|
| 30 |
+
|
| 31 |
+
# Send typing indicator
|
| 32 |
+
print(f'Sending typing indicator...')
|
| 33 |
+
async with message.channel.typing():
|
| 34 |
+
print(f'Typing indicator sent.')
|
| 35 |
+
|
| 36 |
+
# Simulate a delay (you can adjust this based on the actual response time)
|
| 37 |
+
print(f'Simulating delay...')
|
| 38 |
+
await asyncio.sleep(2)
|
| 39 |
+
|
| 40 |
+
# Generate the prompt
|
| 41 |
+
print(f'Generating prompt...')
|
| 42 |
+
prompt = get_prompt(input_text)
|
| 43 |
+
|
| 44 |
+
# Reply directly to the input message
|
| 45 |
+
print(f'Replying to message...')
|
| 46 |
+
await message.reply(f'{user_mention} Generated Prompt: {prompt}')
|
| 47 |
+
print(f'Reply sent.')
|
| 48 |
+
|
| 49 |
+
def get_prompt(input_text):
|
| 50 |
+
api_url = "https://gustavosta-magicprompt-stable-diffusion.hf.space/api/predict"
|
| 51 |
+
headers = {"Content-Type": "application/json"}
|
| 52 |
+
payload = {"data": [input_text]}
|
| 53 |
+
|
| 54 |
+
print(f'Sending API request...')
|
| 55 |
+
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
|
| 56 |
+
result = response.json()
|
| 57 |
+
|
| 58 |
+
print(f'API response: {result}')
|
| 59 |
+
|
| 60 |
+
return result['data'][0] if result['data'][0] else 'Prompt generation failed.'
|
| 61 |
+
|
| 62 |
+
# Replace 'YOUR_BOT_TOKEN' with the actual token you obtained from the Discord Developer Portal
|
| 63 |
+
print('Starting the bot...')
|
| 64 |
+
client.run('MTE2OTIzNDc0ODc1Mzg0NjMxMg.GOlz8M.rQlvjfQWLYPNpEhWVQ0vJ7sUw_IV0TooMdN8Os')
|