MKE0108 commited on
Commit
4f6f89f
·
1 Parent(s): 46b7ca6
bot_main.py CHANGED
@@ -164,7 +164,7 @@ def generate_content1(topic):
164
  # Assuming response.candidates is iterable and contains a 'content' attribute
165
  for candidate in response.candidates:
166
  ans = [part.text for part in candidate.content.parts]
167
- except error:
168
  ans=[]
169
 
170
 
@@ -172,22 +172,25 @@ def generate_content1(topic):
172
  return ans[0] if len(ans) > 0 else None
173
 
174
 
 
 
 
175
  async def create_bad_message(topic, n=10):
176
  result = []
177
  for i in range(n):
178
- print("gen",i)
179
- result = generate_content1(topic)
180
- print("end_gen",i)
181
  if result is not None:
182
  return result
183
  return "為什麼要演奏春日影?"
184
 
185
-
186
- # Updated bot command with asynchronous execution
187
  @bot.hybrid_command(name="重力場", description="重力發言")
188
  @app_commands.describe(topic="主題")
189
  async def join(ctx, topic="樂團"):
 
 
190
  messege = await create_bad_message(topic)
191
  await ctx.send(f'## {messege}', delete_after=100)
192
 
 
193
  bot.run(os.getenv('BOT_TOKENS'))
 
164
  # Assuming response.candidates is iterable and contains a 'content' attribute
165
  for candidate in response.candidates:
166
  ans = [part.text for part in candidate.content.parts]
167
+ except:
168
  ans=[]
169
 
170
 
 
172
  return ans[0] if len(ans) > 0 else None
173
 
174
 
175
+ import asyncio
176
+
177
+ # 將函數轉為異步版本
178
  async def create_bad_message(topic, n=10):
179
  result = []
180
  for i in range(n):
181
+ result = await asyncio.to_thread(generate_content1, topic)
 
 
182
  if result is not None:
183
  return result
184
  return "為什麼要演奏春日影?"
185
 
186
+ # 使用非同步的 Discord bot command
 
187
  @bot.hybrid_command(name="重力場", description="重力發言")
188
  @app_commands.describe(topic="主題")
189
  async def join(ctx, topic="樂團"):
190
+ # 異步等待訊息生成
191
+ await ctx.defer()
192
  messege = await create_bad_message(topic)
193
  await ctx.send(f'## {messege}', delete_after=100)
194
 
195
+
196
  bot.run(os.getenv('BOT_TOKENS'))
keys/__pycache__/init.cpython-39.pyc CHANGED
Binary files a/keys/__pycache__/init.cpython-39.pyc and b/keys/__pycache__/init.cpython-39.pyc differ
 
keys/init.py CHANGED
@@ -2,3 +2,4 @@ import os
2
  def set_env():
3
  BOT_TOKENS="MTI4MDAzNjQxNjYyOTUwNjExMA.Ga0m_Y.Dch8wMPrvZs8CLXrqhJH55qx-h6mfv0Esxuvs0"
4
  os.environ['BOT_TOKENS'] = BOT_TOKENS
 
 
2
  def set_env():
3
  BOT_TOKENS="MTI4MDAzNjQxNjYyOTUwNjExMA.Ga0m_Y.Dch8wMPrvZs8CLXrqhJH55qx-h6mfv0Esxuvs0"
4
  os.environ['BOT_TOKENS'] = BOT_TOKENS
5
+ os.environ["GOOGLE_API_KEY"] = "AIzaSyBXkj0eymogFbX65hVNnZ8ioQrFtOE3tlE"