forked from nvmax/FluxComfyDiscordbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.py
75 lines (62 loc) · 3.2 KB
/
web_server.py
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
66
67
68
69
70
71
72
73
74
75
from aiohttp import web
from Main.custom_commands.web_handlers import handle_generated_image
import logging
from Main.custom_commands.message_constants import STATUS_MESSAGES
from config import server_address
import discord
logger = logging.getLogger(__name__)
async def update_progress(request):
try:
data = await request.json()
request_id = data.get('request_id')
progress_data = data.get('progress_data', {})
if not request_id:
return web.Response(text="Missing request_id", status=400)
if request_id not in request.app['bot'].pending_requests:
return web.Response(text="Unknown request_id", status=404)
request_item = request.app['bot'].pending_requests[request_id]
try:
channel = await request.app['bot'].fetch_channel(int(request_item.channel_id))
message = await channel.fetch_message(int(request_item.original_message_id))
status = progress_data.get('status', '')
progress_message = progress_data.get('message', 'Processing...')
progress = progress_data.get('progress', 0)
status_info = STATUS_MESSAGES.get(status, {
'message': progress_message,
'emoji': '⚙️'
})
if status == 'generating':
formatted_message = f"{status_info['emoji']} {status_info['message']} {progress}%"
elif status == 'error':
formatted_message = f"{status_info['emoji']} {status_info['message']} {progress_message}"
# Only remove on error
if request_id in request.app['bot'].pending_requests:
del request.app['bot'].pending_requests[request_id]
else:
formatted_message = f"{status_info['emoji']} {status_info['message']}"
await message.edit(content=formatted_message)
logger.debug(f"Updated progress message: {formatted_message}")
return web.Response(text="Progress updated")
except discord.errors.NotFound:
logger.warning(f"Message {request_item.original_message_id} not found")
return web.Response(text="Message not found", status=404)
except discord.errors.Forbidden:
logger.warning("Bot lacks permission to edit message")
return web.Response(text="Permission denied", status=403)
except Exception as e:
logger.error(f"Error updating progress message: {str(e)}")
return web.Response(text=f"Error: {str(e)}", status=500)
except Exception as e:
logger.error(f"Error in update_progress: {str(e)}")
return web.Response(text="Internal server error", status=500)
async def start_web_server(bot):
app = web.Application()
app['bot'] = bot
app.router.add_post('/send_image', handle_generated_image)
app.router.add_post('/update_progress', update_progress)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host="0.0.0.0", port=8080)
await site.start()
logger.info(f"Web server started on 0.0.0.0:8080 (ComfyUI server: {server_address})")
return app