seawolf2357 commited on
Commit
6dd0483
β€’
1 Parent(s): e285c06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -21
app.py CHANGED
@@ -48,36 +48,50 @@ class MyClient(discord.Client):
48
  super().__init__(*args, **kwargs)
49
  self.session = None
50
  self.last_comments = {}
 
51
 
52
  async def on_ready(self):
53
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
54
  self.session = aiohttp.ClientSession()
55
  self.loop.create_task(self.check_for_new_comments())
 
56
 
57
  async def check_for_new_comments(self):
58
  while True:
59
  try:
60
- channel = self.get_channel(SPECIFIC_CHANNEL_ID)
61
- if channel:
62
- logging.info(f"채널 {channel}μ—μ„œ μƒˆλ‘œμš΄ λŒ“κΈ€μ„ ν™•μΈν•©λ‹ˆλ‹€.")
63
- async for message in channel.history(limit=10):
64
- video_id = extract_video_id(message.content)
65
- if video_id:
66
- logging.info(f"λΉ„λ””μ˜€ ID: {video_id} - λ©”μ‹œμ§€: {message.content}")
67
- new_comments = await get_video_comments(video_id)
68
- old_comments = self.last_comments.get(video_id, [])
69
- for comment in new_comments:
70
- if comment not in old_comments:
71
- logging.info(f"μƒˆ λŒ“κΈ€ 발견: {comment[0]}")
72
- transcript = await get_best_available_transcript(video_id)
73
- reply = await generate_reply(comment[0], transcript)
74
- logging.info(f"μƒμ„±λœ λ‹΅λ³€: {reply}")
75
- await send_reply(message, video_id, comment, reply)
76
- old_comments.append(comment)
77
- self.last_comments[video_id] = old_comments
78
  except Exception as e:
79
  logging.error(f"Error in check_for_new_comments: {e}")
80
- await asyncio.sleep(30) # 주기적인 체크 간격을 30초둜 μ„€μ •
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  async def close(self):
83
  if self.session:
@@ -133,11 +147,9 @@ async def generate_reply(comment, transcript):
133
  response = hf_client.text_generation(prompt)
134
  if response:
135
  try:
136
- # JSON νŒŒμ‹± μ‹œλ„
137
  response_json = json.loads(response)
138
  return response_json.get("generated_text", "").strip()
139
  except json.JSONDecodeError:
140
- # JSON νŒŒμ‹± μ‹€νŒ¨ μ‹œ 둜그 남기고 응닡 κ·ΈλŒ€λ‘œ λ°˜ν™˜
141
  logging.error(f"Failed to decode JSON: {response}")
142
  return response.strip()
143
  except Exception as e:
 
48
  super().__init__(*args, **kwargs)
49
  self.session = None
50
  self.last_comments = {}
51
+ self.processed_comments = set()
52
 
53
  async def on_ready(self):
54
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
55
  self.session = aiohttp.ClientSession()
56
  self.loop.create_task(self.check_for_new_comments())
57
+ self.loop.create_task(self.reply_to_unanswered_comments())
58
 
59
  async def check_for_new_comments(self):
60
  while True:
61
  try:
62
+ await self.process_comments()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  except Exception as e:
64
  logging.error(f"Error in check_for_new_comments: {e}")
65
+ await asyncio.sleep(30)
66
+
67
+ async def reply_to_unanswered_comments(self):
68
+ while True:
69
+ try:
70
+ await self.process_comments()
71
+ except Exception as e:
72
+ logging.error(f"Error in reply_to_unanswered_comments: {e}")
73
+ await asyncio.sleep(60)
74
+
75
+ async def process_comments(self):
76
+ channel = self.get_channel(SPECIFIC_CHANNEL_ID)
77
+ if channel:
78
+ logging.info(f"채널 {channel}μ—μ„œ μƒˆλ‘œμš΄ λŒ“κΈ€μ„ ν™•μΈν•©λ‹ˆλ‹€.")
79
+ async for message in channel.history(limit=10):
80
+ video_id = extract_video_id(message.content)
81
+ if video_id:
82
+ logging.info(f"λΉ„λ””μ˜€ ID: {video_id} - λ©”μ‹œμ§€: {message.content}")
83
+ new_comments = await get_video_comments(video_id)
84
+ old_comments = self.last_comments.get(video_id, [])
85
+ for comment in new_comments:
86
+ if comment not in old_comments:
87
+ logging.info(f"μƒˆ λŒ“κΈ€ 발견: {comment[0]}")
88
+ transcript = await get_best_available_transcript(video_id)
89
+ reply = await generate_reply(comment[0], transcript)
90
+ logging.info(f"μƒμ„±λœ λ‹΅λ³€: {reply}")
91
+ await send_reply(message, video_id, comment, reply)
92
+ old_comments.append(comment)
93
+ self.processed_comments.add(comment[1])
94
+ self.last_comments[video_id] = old_comments
95
 
96
  async def close(self):
97
  if self.session:
 
147
  response = hf_client.text_generation(prompt)
148
  if response:
149
  try:
 
150
  response_json = json.loads(response)
151
  return response_json.get("generated_text", "").strip()
152
  except json.JSONDecodeError:
 
153
  logging.error(f"Failed to decode JSON: {response}")
154
  return response.strip()
155
  except Exception as e: