seawolf2357 commited on
Commit
a088b10
ยท
verified ยท
1 Parent(s): 88bd181

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -27
app.py CHANGED
@@ -15,6 +15,7 @@ intents.message_content = True
15
  intents.messages = True
16
  intents.guilds = True
17
  intents.guild_messages = True
 
18
 
19
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
20
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
@@ -57,10 +58,10 @@ class MyClient(discord.Client):
57
  keywords = await extract_keywords(message)
58
  if keywords:
59
  # Pexels API๋กœ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰
60
- image_infos = await search_images(keywords)
61
- if image_infos:
62
- # ๋””์Šค์ฝ”๋“œ ์ฑ„๋„์— ์ด๋ฏธ์ง€ ์ „์†ก
63
- await send_images(message.channel, keywords, image_infos)
64
  else:
65
  await message.channel.send(f"**{keywords}**์— ๋Œ€ํ•œ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
66
  else:
@@ -106,31 +107,16 @@ async def search_images(keywords):
106
  response = requests.get(PEXELS_API_URL, headers=headers, params=params)
107
  if response.status_code == 200:
108
  data = response.json()
109
- return [{
110
- "url": photo['src']['large2x'],
111
- "thumbnail": photo['src']['medium'], # ์ธ๋„ค์ผ์„ medium ์‚ฌ์ด์ฆˆ๋กœ ์‚ฌ์šฉ
112
- "title": photo['alt']
113
- } for photo in data['photos']]
114
  return None
115
 
116
- async def send_images(channel, keywords, image_infos):
117
- message_content = f"**{keywords}**์— ๋Œ€ํ•œ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ {len(image_infos)}์žฅ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค:"
118
- await channel.send(message_content)
119
- embeds = []
120
-
121
- for info in image_infos:
122
- embed = discord.Embed(title=info["title"], url=info["url"], description=f"[๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ ๋ณด๊ธฐ]({info['url']})")
123
- embed.set_thumbnail(url=info["thumbnail"])
124
- embeds.append(embed)
125
-
126
- # ๋””์Šค์ฝ”๋“œ ์ž„๋ฒ ๋“œ ๋ฉ”์‹œ์ง€๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 10๊ฐœ๊นŒ์ง€ ๋ณด๋‚ด๋ฏ€๋กœ ํ•œ ๋ฒˆ์— ๋ณด๋‚ด๊ธฐ
127
- if len(embeds) == 10:
128
- await channel.send(embeds=embeds)
129
- embeds = []
130
-
131
- # ๋‚จ์€ ์ž„๋ฒ ๋“œ ์ „์†ก
132
- if embeds:
133
- await channel.send(embeds=embeds)
134
 
135
  if __name__ == "__main__":
136
  discord_client = MyClient(intents=intents)
 
15
  intents.messages = True
16
  intents.guilds = True
17
  intents.guild_messages = True
18
+ intents.threads = True # ์Šค๋ ˆ๋“œ ๊ถŒํ•œ ์ถ”๊ฐ€
19
 
20
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
21
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
 
58
  keywords = await extract_keywords(message)
59
  if keywords:
60
  # Pexels API๋กœ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰
61
+ image_urls = await search_images(keywords)
62
+ if image_urls:
63
+ # ์š”์ฒญ์ž์™€์˜ ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ ๋ฐ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ ์ „์†ก
64
+ await create_thread_and_send_images(message, keywords, image_urls)
65
  else:
66
  await message.channel.send(f"**{keywords}**์— ๋Œ€ํ•œ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
67
  else:
 
107
  response = requests.get(PEXELS_API_URL, headers=headers, params=params)
108
  if response.status_code == 200:
109
  data = response.json()
110
+ return [photo['src']['large2x'] for photo in data['photos']]
 
 
 
 
111
  return None
112
 
113
+ async def create_thread_and_send_images(message, keywords, image_urls):
114
+ # ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ
115
+ thread = await message.channel.create_thread(name=f"{message.author.name}์˜ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ", message=message)
116
+ message_content = f"**{keywords}**์— ๋Œ€ํ•œ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ {len(image_urls)}์žฅ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค:"
117
+ await thread.send(message_content)
118
+ for url in image_urls:
119
+ await thread.send(url)
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  if __name__ == "__main__":
122
  discord_client = MyClient(intents=intents)