seawolf2357 commited on
Commit
1a8ca9e
ยท
verified ยท
1 Parent(s): 6b7b97d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -34,7 +34,6 @@ class MyClient(discord.Client):
34
  subprocess.Popen(["python", "web.py"])
35
  logging.info("Web.py server has been started.")
36
 
37
-
38
  async def on_message(self, message):
39
  if message.author == self.user:
40
  return
@@ -44,20 +43,29 @@ class MyClient(discord.Client):
44
  return
45
  self.is_processing = True
46
  try:
 
47
  response = await generate_response(message)
48
- await message.channel.send(response)
 
 
 
49
  finally:
50
  self.is_processing = False
51
 
52
  def is_message_in_specific_channel(self, message):
53
- # ๋ฉ”์‹œ์ง€๊ฐ€ ์ง€์ •๋œ ์ฑ„๋„์ด๊ฑฐ๋‚˜, ํ•ด๋‹น ์ฑ„๋„์˜ ์“ฐ๋ ˆ๋“œ์ธ ๊ฒฝ์šฐ True ๋ฐ˜ํ™˜
54
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
55
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
56
  )
57
 
 
 
 
 
 
 
58
 
59
  async def generate_response(message):
60
- global conversation_history # ์ „์—ญ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๋ช…์‹œ
61
  user_input = message.content
62
  user_mention = message.author.mention
63
  system_message = f"{user_mention}, Discord์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
@@ -65,7 +73,7 @@ async def generate_response(message):
65
  ๋‹น์‹ ์€ ๋ธ”๋กœ๊ทธ๋ฅผ ์šด์˜ํ•˜๋Š” ์ „๋ฌธ ๋ธ”๋กœ๊ฑฐ์ž…๋‹ˆ๋‹ค.
66
  SEO์— ๋งž๋Š” ํ€„๋ฆฌํ‹ฐ ๋†’์€ ํฌ์ŠคํŒ…์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ตœ์šฐ์„  ๋ชฉํ‘œ๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋ฉฐ, ๋ธ”๋กœ๊ทธ์˜ ๊ธ€์„ ์ž‘์„ฑํ• ๋•Œ๋Š”
67
  ๋ฒˆ์—ญ์ฒด๊ฐ€ ์•„๋‹Œ ์ž์—ฐ์Šค๋Ÿฌ์šด ํ•œ๊ตญ์–ด๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ฌด์—‡๋ณด๋‹ค ์ตœ์„ ์„ ๋‹ค ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
68
- ๋Œ€ํ™” ์‹œ์ž‘์‹œ "์–ด๋–ค ์ฃผ์ œ๋กœ ๋ธ”๋กœ๊ทธ๋ฅผ ์ž‘์„ฑํ• ์ง€ ๋ฌผ์–ด๋ณด๋ฉฐ, ๊ทธ ์ฃผ์ œ์— ๋Œ€ํ•ด ์ƒ๋Œ€๋ฐฉ๊ณผ ๋Œ€ํ™”๋ฅผ ํ•˜์—ฌ ์ตœ์ข… ์ฃผ์ œ๋ฅผ ๊ฒฐ์ •ํ•˜๋ผ"
69
  ๊ฒฐ์ •๋œ ์ฃผ์ œ์— ๋Œ€ํ•ด ์•„์ฃผ ์ „๋ฌธ์ ์ด๊ณ  ํ›Œ๋ฅญํ•œ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ž‘์„ฑํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
70
  ๋ธ”๋กœ๊ทธ ์ž‘์„ฑ ์‹œ์ž‘์ „์— ๋ฐ˜๋“œ์‹œ "๊ทธ๋Ÿผ ์ด์ œ ๋ธ”๋กœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”"๋ผ๊ณ  ์ถœ๋ ฅํ• ๊ฒƒ.
71
 
@@ -104,8 +112,8 @@ SEO์— ๋งž๋Š” ํ€„๋ฆฌํ‹ฐ ๋†’์€ ํฌ์ŠคํŒ…์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ตœ์šฐ์„  ๋ชฉํ‘œ๊ฐ€
104
  7. ๊ธ€์˜ ์ „์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ์ฑ•ํ„ฐ ๋งˆ๋‹ค ์ตœ์†Œ 1,000์ž ์ด์ƒ์œผ๋กœ ์„ธ ์ฑ•ํ„ฐ๋ฅผ ํฌํ•จํ•˜๋ฉด 3,000์ž ์ด์ƒ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
105
  8. "#ํƒœ๊ทธ"๋ฅผ 10๊ฐœ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
106
  """
107
-
108
- conversation_history.append({"role": "user", "content": user_input})
109
  logging.debug(f'Conversation history updated: {conversation_history}')
110
 
111
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
@@ -129,4 +137,4 @@ SEO์— ๋งž๋Š” ํ€„๋ฆฌํ‹ฐ ๋†’์€ ํฌ์ŠคํŒ…์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ตœ์šฐ์„  ๋ชฉํ‘œ๊ฐ€
129
 
130
  if __name__ == "__main__":
131
  discord_client = MyClient(intents=intents)
132
- discord_client.run(os.getenv('DISCORD_TOKEN'))
 
34
  subprocess.Popen(["python", "web.py"])
35
  logging.info("Web.py server has been started.")
36
 
 
37
  async def on_message(self, message):
38
  if message.author == self.user:
39
  return
 
43
  return
44
  self.is_processing = True
45
  try:
46
+ # ๋ฉ”์‹œ์ง€์— ๋Œ€ํ•œ ์‘๋‹ต ์ƒ์„ฑ
47
  response = await generate_response(message)
48
+ # ์Šค๋ ˆ๋“œ ์ƒ์„ฑ ๋˜๋Š” ๊ธฐ์กด ์Šค๋ ˆ๋“œ ์‚ฌ์šฉ
49
+ thread = await self.ensure_thread(message)
50
+ # ์Šค๋ ˆ๋“œ์— ์‘๋‹ต ์ „์†ก
51
+ await thread.send(response)
52
  finally:
53
  self.is_processing = False
54
 
55
  def is_message_in_specific_channel(self, message):
 
56
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
57
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
58
  )
59
 
60
+ async def ensure_thread(self, message):
61
+ # ์Šค๋ ˆ๋“œ๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜๋ฉด ๊ทธ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑ
62
+ if isinstance(message.channel, discord.Thread):
63
+ return message.channel
64
+ else:
65
+ return await message.channel.create_thread(name=f"Response to {message.author.display_name}", message=message)
66
 
67
  async def generate_response(message):
68
+ global conversation_history
69
  user_input = message.content
70
  user_mention = message.author.mention
71
  system_message = f"{user_mention}, Discord์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
 
73
  ๋‹น์‹ ์€ ๋ธ”๋กœ๊ทธ๋ฅผ ์šด์˜ํ•˜๋Š” ์ „๋ฌธ ๋ธ”๋กœ๊ฑฐ์ž…๋‹ˆ๋‹ค.
74
  SEO์— ๋งž๋Š” ํ€„๋ฆฌํ‹ฐ ๋†’์€ ํฌ์ŠคํŒ…์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ตœ์šฐ์„  ๋ชฉํ‘œ๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋ฉฐ, ๋ธ”๋กœ๊ทธ์˜ ๊ธ€์„ ์ž‘์„ฑํ• ๋•Œ๋Š”
75
  ๋ฒˆ์—ญ์ฒด๊ฐ€ ์•„๋‹Œ ์ž์—ฐ์Šค๋Ÿฌ์šด ํ•œ๊ตญ์–ด๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ฌด์—‡๋ณด๋‹ค ์ตœ์„ ์„ ๋‹ค ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
76
+ ๋Œ€ํ™” ์‹œ์ž‘์‹œ "์–ด๋–ค ์ฃผ์ œ๋กœ ๋ธ”๋กœ๊ทธ๋ฅผ ์ž‘์„ฑํ• ์ง€ ๋ฌผ์–ด๋ณด๋ฉฐ, ๊ทธ ์ฃผ์ œ์— ๋Œ€ํ•ด ์ƒ๋Œ€๋ฐฉ๊ณผ ๋Œ€ํ™”๋ฅผ ํ•˜์—ฌ ์ตœ์ข… ์ฃผ์ œ๋ฅผ ๊ฒฐ์ •ํ•˜๋ผ. ์ค‘๊ฐ„์— ์ถœ๋ ฅ์ด ๋Š๊ธธ๊ฒฝ์šฐ '๊ณ„์†'์„ ์ž…๋ ฅํ•˜๋ผ๊ณ  ๋ฐ˜๋“œ์‹œ ์•Œ๋ ค์ค˜๋ผ"
77
  ๊ฒฐ์ •๋œ ์ฃผ์ œ์— ๋Œ€ํ•ด ์•„์ฃผ ์ „๋ฌธ์ ์ด๊ณ  ํ›Œ๋ฅญํ•œ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ž‘์„ฑํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
78
  ๋ธ”๋กœ๊ทธ ์ž‘์„ฑ ์‹œ์ž‘์ „์— ๋ฐ˜๋“œ์‹œ "๊ทธ๋Ÿผ ์ด์ œ ๋ธ”๋กœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”"๋ผ๊ณ  ์ถœ๋ ฅํ• ๊ฒƒ.
79
 
 
112
  7. ๊ธ€์˜ ์ „์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ์ฑ•ํ„ฐ ๋งˆ๋‹ค ์ตœ์†Œ 1,000์ž ์ด์ƒ์œผ๋กœ ์„ธ ์ฑ•ํ„ฐ๋ฅผ ํฌํ•จํ•˜๋ฉด 3,000์ž ์ด์ƒ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
113
  8. "#ํƒœ๊ทธ"๋ฅผ 10๊ฐœ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
114
  """
115
+
116
+ conversation_history.append({"role": "user", "content": user_input})
117
  logging.debug(f'Conversation history updated: {conversation_history}')
118
 
119
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
 
137
 
138
  if __name__ == "__main__":
139
  discord_client = MyClient(intents=intents)
140
+ discord_client.run(os.getenv('DISCORD_TOKEN'))