Spaces:
Running
Running
import discord | |
import logging | |
import os | |
import asyncio | |
import subprocess | |
from huggingface_hub import InferenceClient | |
from PIL import Image | |
import io | |
import base64 | |
# ๋ก๊น ์ค์ | |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()]) | |
# ์ธํ ํธ ์ค์ | |
intents = discord.Intents.default() | |
intents.message_content = True | |
intents.messages = True | |
intents.guilds = True | |
intents.guild_messages = True | |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์ | |
hf_client = InferenceClient(token=os.getenv("HF_TOKEN")) | |
class MyClient(discord.Client): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.is_processing = False | |
async def on_ready(self): | |
logging.info(f'{self.user} has logged in!') | |
subprocess.Popen(["python", "web.py"]) | |
logging.info("Web.py server has been started.") | |
async def on_message(self, message): | |
if message.author == self.user or not message.content: | |
return | |
if message.channel.id != SPECIFIC_CHANNEL_ID and not isinstance(message.channel, discord.Thread): | |
return | |
if self.is_processing: | |
return | |
self.is_processing = True | |
try: | |
image_path = await generate_image(message.content) | |
if image_path: | |
await send_image(message.channel, image_path) | |
else: | |
await message.channel.send("Failed to generate the image.") | |
finally: | |
self.is_processing = False | |
async def send_image(channel, image_path): | |
file = discord.File(image_path) | |
await channel.send(file=file) | |
if __name__ == "__main__": | |
discord_client = MyClient(intents=intents) | |
discord_client.run(os.getenv('DISCORD_TOKEN')) | |