Spaces:
Running
Running
import discord | |
import logging | |
import os | |
from huggingface_hub import InferenceClient | |
import asyncio | |
import subprocess # subprocess ๋ชจ๋์ ์ถ๊ฐํฉ๋๋ค. | |
# ๋ก๊น ์ค์ | |
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 | |
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์ | |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN")) | |
# ํน์ ์ฑ๋ ID | |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID")) | |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ๋ณ์ | |
conversation_history = [] | |
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}๋ก ๋ก๊ทธ์ธ๋์์ต๋๋ค!') | |
# web.py๋ฅผ ์๋ก์ด ํ๋ก์ธ์ค๋ก ์คํํฉ๋๋ค. | |
subprocess.Popen(["python", "web.py"]) | |
logging.info("Web.py server has been started.") | |
async def on_message(self, message): | |
if message.author == self.user: | |
return | |
if message.channel.id != SPECIFIC_CHANNEL_ID: | |
return | |
if self.is_processing: | |
return | |
self.is_processing = True | |
try: | |
response = await generate_response(message.content) | |
await message.channel.send(response) | |
finally: | |
self.is_processing = False | |
async def generate_response(user_input): | |
# (์ค๋ต: ๊ธฐ์กด์ generate_response ํจ์ ๋ก์ง ์ ์ง) | |
if __name__ == "__main__": | |
discord_client = MyClient(intents=intents) | |
discord_client.run(os.getenv('DISCORD_TOKEN')) | |