File size: 2,195 Bytes
b63efdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import discord
import requests
import json
import os
import asyncio

# Create an instance of Intents
intents = discord.Intents.default()
intents.message_content = True  # Enable message content events

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    # Change the command trigger to "/prompt"
    if message.content.startswith('/prompt'):
        input_text = message.content[len('/prompt'):].strip()
        if input_text:
            # Mention the user in the response
            user_mention = message.author.mention

            print(f'Received message: {message.content}')
            
            # Send typing indicator
            print(f'Sending typing indicator...')
            async with message.channel.typing():
                print(f'Typing indicator sent.')
                
                # Simulate a delay (you can adjust this based on the actual response time)
                print(f'Simulating delay...')
                await asyncio.sleep(2)

                # Generate the prompt
                print(f'Generating prompt...')
                prompt = get_prompt(input_text)

                # Reply directly to the input message
                print(f'Replying to message...')
                await message.reply(f'{user_mention} Generated Prompt: {prompt}')
                print(f'Reply sent.')

def get_prompt(input_text):
    api_url = "https://gustavosta-magicprompt-stable-diffusion.hf.space/api/predict"
    headers = {"Content-Type": "application/json"}
    payload = {"data": [input_text]}

    print(f'Sending API request...')
    response = requests.post(api_url, data=json.dumps(payload), headers=headers)
    result = response.json()

    print(f'API response: {result}')

    return result['data'][0] if result['data'][0] else 'Prompt generation failed.'

# Replace 'YOUR_BOT_TOKEN' with the actual token you obtained from the Discord Developer Portal
print('Starting the bot...')
client.run('MTE2OTIzNDc0ODc1Mzg0NjMxMg.GOlz8M.rQlvjfQWLYPNpEhWVQ0vJ7sUw_IV0TooMdN8Os')