File size: 6,503 Bytes
78efe79
440418c
f3985af
7edf3cc
dc80b35
b4bdc7a
4636b5a
7edf3cc
f2ef6a6
4f7204d
 
 
 
407a575
32c38ef
672b059
440418c
1831164
440418c
22dee1c
440418c
22dee1c
 
08baccf
dc80b35
 
 
4636b5a
 
 
 
dc80b35
40d0e92
74ccf1c
78efe79
08baccf
 
dc80b35
08baccf
78efe79
40d0e92
78efe79
672b059
 
 
7edf3cc
672b059
78efe79
dc80b35
 
6a30e5d
78efe79
dc80b35
 
 
 
7edf3cc
 
f2ef6a6
7edf3cc
 
 
 
1e4bf1a
7edf3cc
1e4bf1a
7edf3cc
dc80b35
 
 
6a30e5d
 
 
 
 
 
7edf3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2ef6a6
7edf3cc
 
 
 
951f374
 
7edf3cc
951f374
f2ef6a6
951f374
 
f2ef6a6
951f374
 
 
 
 
 
 
 
f2ef6a6
 
 
 
 
 
7edf3cc
 
 
 
 
 
 
4636b5a
7edf3cc
 
4636b5a
7edf3cc
4636b5a
7edf3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0926d14
34428f1
dc80b35
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import discord
import logging
import os
import re
import asyncio
from huggingface_hub import InferenceClient
from googleapiclient.discovery import build
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
from dotenv import load_dotenv

# ν™˜κ²½ λ³€μˆ˜ λ‘œλ“œ
load_dotenv()

# λ‘œκΉ… μ„€μ •
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("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))

# YouTube API ν‚€ μ„€μ •
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)

# νŠΉμ • 채널 ID
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))

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}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')

        # 봇이 μ‹œμž‘λ  λ•Œ μ•ˆλ‚΄ λ©”μ‹œμ§€λ₯Ό 전솑
        channel = self.get_channel(SPECIFIC_CHANNEL_ID)
        if channel:
            await channel.send("유튜브 λΉ„λ””μ˜€ URL을 μž…λ ₯ν•˜λ©΄, μžλ§‰κ³Ό λŒ“κΈ€μ„ 기반으둜 닡글을 μž‘μ„±ν•©λ‹ˆλ‹€.")

    async def on_message(self, message):
        if message.author == self.user:
            return
        if not self.is_message_in_specific_channel(message):
            return
        if self.is_processing:
            return
        self.is_processing = True
        try:
            video_id = extract_video_id(message.content)
            if video_id:
                transcript = await get_best_available_transcript(video_id)
                comments = await get_video_comments(video_id)
                if comments and transcript:
                    replies = await generate_replies(comments, transcript)
                    await create_thread_and_send_replies(message, video_id, comments, replies)
                else:
                    await message.channel.send("μžλ§‰μ΄λ‚˜ λŒ“κΈ€μ„ κ°€μ Έμ˜¬ 수 μ—†μŠ΅λ‹ˆλ‹€.")
            else:
                await message.channel.send("μœ νš¨ν•œ 유튜브 λΉ„λ””μ˜€ URL을 μ œκ³΅ν•΄ μ£Όμ„Έμš”.")
        finally:
            self.is_processing = False

    def is_message_in_specific_channel(self, message):
        # λ©”μ‹œμ§€κ°€ μ§€μ •λœ μ±„λ„μ΄κ±°λ‚˜, ν•΄λ‹Ή μ±„λ„μ˜ μ“°λ ˆλ“œμΈ 경우 True λ°˜ν™˜
        return message.channel.id == SPECIFIC_CHANNEL_ID or (
            isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
        )

def extract_video_id(url):
    """
    YouTube λΉ„λ””μ˜€ URLμ—μ„œ λΉ„λ””μ˜€ IDλ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.
    """
    video_id = None
    youtube_regex = (
        r'(https?://)?(www\.)?'
        '(youtube|youtu|youtube-nocookie)\.(com|be)/'
        '(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')

    match = re.match(youtube_regex, url)
    if match:
        video_id = match.group(6)
    logging.debug(f'Extracted video ID: {video_id}')
    return video_id

async def get_best_available_transcript(video_id):
    """
    YouTube λΉ„λ””μ˜€μ˜ μžλ§‰μ„ κ°€μ Έμ˜΅λ‹ˆλ‹€.
    """
    try:
        # ν•œκ΅­μ–΄ μžλ§‰ μ‹œλ„
        transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ko'])
    except Exception as e:
        logging.warning(f'Error fetching Korean transcript: {e}')
        try:
            # ν•œκ΅­μ–΄ μžλ§‰μ΄ μ—†μœΌλ©΄ μ˜μ–΄ μžλ§‰ μ‹œλ„
            transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
        except Exception as e:
            logging.warning(f'Error fetching English transcript: {e}')
            try:
                # μ˜μ–΄ μžλ§‰λ„ μ—†μœΌλ©΄ λ‹€λ₯Έ μ–Έμ–΄ μžλ§‰ μ‹œλ„
                transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
                transcript = transcripts.find_manually_created_transcript().fetch()
            except Exception as e:
                logging.error(f'Error fetching alternative transcript: {e}')
                return None

    # μžλ§‰ ν¬λ§·νŒ…
    formatter = TextFormatter()
    transcript_text = formatter.format_transcript(transcript)
    logging.debug(f'Fetched transcript: {transcript_text}')
    return transcript_text

async def get_video_comments(video_id):
    """
    YouTube λΉ„λ””μ˜€μ˜ λŒ“κΈ€μ„ κ°€μ Έμ˜΅λ‹ˆλ‹€.
    """
    comments = []
    response = youtube_service.commentThreads().list(
        part='snippet',
        videoId=video_id,
        maxResults=100  # μ΅œλŒ€ 100개의 λŒ“κΈ€ κ°€μ Έμ˜€κΈ°
    ).execute()

    for item in response.get('items', []):
        comment = item['snippet']['topLevelComment']['snippet']['textOriginal']
        comments.append(comment)
    
    logging.debug(f'Fetched comments: {comments}')
    return comments

async def generate_replies(comments, transcript):
    """
    λŒ“κΈ€κ³Ό μžλ§‰μ„ 기반으둜 LLM 닡글을 μƒμ„±ν•©λ‹ˆλ‹€.
    """
    replies = []
    for comment in comments:
        messages = [
            {"role": "system", "content": f"λΉ„λ””μ˜€ μžλ§‰: {transcript}"},
            {"role": "user", "content": comment}
        ]
        loop = asyncio.get_event_loop()
        response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
            messages, max_tokens=50, temperature=0.7, top_p=0.85))
        
        if response.choices and response.choices[0].message:
            reply = response.choices[0].message['content'].strip()
        else:
            reply = "닡글을 생성할 수 μ—†μŠ΅λ‹ˆλ‹€."
        replies.append(reply)
    
    logging.debug(f'Generated replies: {replies}')
    return replies

async def create_thread_and_send_replies(message, video_id, comments, replies):
    """
    λŒ“κΈ€κ³Ό 닡글을 μƒˆλ‘œμš΄ μ“°λ ˆλ“œμ— μ „μ†‘ν•©λ‹ˆλ‹€.
    """
    thread = await message.channel.create_thread(name=f"{message.author.name}의 λŒ“κΈ€ λ‹΅κΈ€", message=message)
    for comment, reply in zip(comments, replies):
        embed = discord.Embed(description=f"**λŒ“κΈ€**: {comment}\n**λ‹΅κΈ€**: {reply}")
        await thread.send(embed=embed)

if __name__ == "__main__":
    discord_client = MyClient(intents=intents)
    discord_client.run(os.getenv('DISCORD_TOKEN'))