Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
slippersheepig authored Jan 21, 2024
1 parent c6bce71 commit 31714d4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
HUGGINGFACE_TOKEN = config('HUGGINGFACE_TOKEN')
API_URL = config('API_URL')

# 全局常量
REQUEST_INTERVAL = 60

# 创建机器人
bot = telebot.TeleBot(BOT_TOKEN)
bot.set_webhook()
Expand All @@ -35,7 +38,6 @@ def stablediffusion(payload):

def generate_image(message, user, prompt):
try:
request_interval = 60
if check_request_limit(user): # 检查频率限制
# 发送 "upload_photo" 动作
bot.send_chat_action(message.chat.id, "upload_photo")
Expand All @@ -47,7 +49,7 @@ def generate_image(message, user, prompt):
bot.send_message(message.chat.id, text=f"请求: {prompt}\nstablediffusion:")
bot.send_photo(message.chat.id, photo)
else:
bot.reply_to(message, f"请求太频繁,请等待 {request_interval} 秒后再试.")
bot.reply_to(message, f"请求太频繁,请等待 {REQUEST_INTERVAL} 秒后再试.")
except Exception as e:
bot.reply_to(message, f"生成图片错误: {str(e)}")
finally:
Expand All @@ -56,7 +58,11 @@ def generate_image(message, user, prompt):
# 增加频率限制函数
def check_request_limit(user):
current_time = time.time()
last_request_time = user_last_request_time.get(user, 0)
# 如果用户不在字典中,添加到字典并设置初始请求时间
if user not in user_last_request_time:
user_last_request_time[user] = current_time
return True
last_request_time = user_last_request_time[user]
time_since_last_request = current_time - last_request_time
# 设置请求时间间隔为60秒
request_interval = 60
Expand Down

0 comments on commit 31714d4

Please sign in to comment.