支持添加 log 频道

This commit is contained in:
xtaodada 2021-06-20 18:16:06 +08:00
parent 844bbb7331
commit 2fa5cae93b
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
3 changed files with 44 additions and 6 deletions

View File

@ -20,3 +20,6 @@ REDIS_CONFIG = {'host': '127.0.0.1', 'port': 6379, 'db': 1}
# 拥有者 id 配置 # 拥有者 id 配置
OWNER = 0 OWNER = 0
# 日志频道 id 0 为不启用
CHANNEL = 0

30
func.py
View File

@ -3,7 +3,7 @@ import time
import connector import connector
import telegram import telegram
from telegram.ext import CommandHandler, MessageHandler, Filters from telegram.ext import CommandHandler, MessageHandler, Filters
from config import TOKEN, LIMIT_COUNT, EXCLUSIVE_MODE, RANK_COMMAND_MODE, OWNER, EXCLUSIVE_LIST from config import TOKEN, LIMIT_COUNT, EXCLUSIVE_MODE, RANK_COMMAND_MODE, OWNER, EXCLUSIVE_LIST, CHANNEL
import schedule import schedule
from task import add_task from task import add_task
@ -42,6 +42,10 @@ def rank(update, context):
if chat_type != "supergroup": if chat_type != "supergroup":
update.message.reply_text("此命令只有在群组中有效!") update.message.reply_text("此命令只有在群组中有效!")
return return
try:
msg = update.message.reply_text("正在添加任务,请稍等~")
except:
pass
if RANK_COMMAND_MODE: if RANK_COMMAND_MODE:
try: try:
chat_member = bot.get_chat_member(chat_id, user_id) chat_member = bot.get_chat_member(chat_id, user_id)
@ -50,8 +54,16 @@ def rank(update, context):
if status == "creator" or status == "administrator": if status == "creator" or status == "administrator":
print("用户权限正确") print("用户权限正确")
else: else:
try:
bot.delete_message(chat_id=chat_id, message_id=msg.message_id)
except:
pass
return return
except Exception as e: except Exception as e:
try:
bot.delete_message(chat_id=chat_id, message_id=msg.message_id)
except:
pass
print(e) print(e)
print("获取用户身份失败") print("获取用户身份失败")
if r.exists("{}_frequency_limit".format(chat_id)): if r.exists("{}_frequency_limit".format(chat_id)):
@ -70,11 +82,23 @@ def rank(update, context):
r.expireat("{}_frequency_limit".format(chat_id), ex_time) r.expireat("{}_frequency_limit".format(chat_id), ex_time)
count = int(r.get("{}_frequency_limit".format(chat_id))) count = int(r.get("{}_frequency_limit".format(chat_id)))
if count > LIMIT_COUNT: if count > LIMIT_COUNT:
update.message.reply_text("该群组在这个小时内的生成配额已经用完,请稍后再试~") try:
bot.edit_message_text(chat_id=chat_id, message_id=msg.message_id, text="该群组在这个小时内的生成配额已经用完,请稍后再试~")
except:
pass
return return
add_task(chat_id) add_task(chat_id)
print("群组: {},用户: {}|{} 发起了主动触发请求".format(chat_id, username, user_id, )) print("群组: {},用户: {}|{} 发起了主动触发请求".format(chat_id, username, user_id, ))
update.message.reply_text("统计数据将在分析完毕后发送到当前群组,请稍等~") if not CHANNEL == 0:
ctext = f'#WORDCLOUD #APPLY \n' \
f'群组 ID`{chat_id}`\n' \
f'用户 ID`{user_id}' \
f'执行操作:`主动生成词云`\n'
bot.send_message(chat_id=chat_id, text=ctext, parse_mode="Markdown")
try:
bot.edit_message_text(chat_id=chat_id, message_id=msg.message_id, text="统计数据将在分析完毕后发送到当前群组,请稍等~")
except:
pass
except Exception as e: except Exception as e:
print("主动触发任务失败,请检查") print("主动触发任务失败,请检查")
print(e) print(e)

17
task.py
View File

@ -8,7 +8,7 @@ import telegram
import time import time
import os import os
import connector import connector
from config import TOKEN, FRONT from config import TOKEN, FRONT, CHANNEL
bot = telegram.Bot(token=TOKEN) bot = telegram.Bot(token=TOKEN)
@ -24,7 +24,7 @@ def schedule_task():
if "chat_content" in i: if "chat_content" in i:
group_list.append(i[:i.find("_")]) group_list.append(i[:i.find("_")])
# print(group_list) # print(group_list)
print("运行定时任务,任务队列中添加任务,任务数量:{}".format(len(group_list))) print("运行定时任务,任务队列中添加任务,任务数量:{}".format(len(group_list)))
for group in group_list: for group in group_list:
try: try:
# 向任务队列中添加任务 # 向任务队列中添加任务
@ -49,9 +49,20 @@ def do_task():
stop_time = float(time.time()) stop_time = float(time.time())
print("当前群组处理耗时:" + str(stop_time - start_time)) print("当前群组处理耗时:" + str(stop_time - start_time))
print("---------------------------") print("---------------------------")
ctext = f'#WORDCLOUD \n' \
f'群组 ID`{group}`\n' \
f'执行操作:`生成词云`\n' \
f'结果:`成功`\n' \
f'处理耗时:`{(stop_time - start_time)[:5]}`'
except Exception as e: except Exception as e:
print("群组: {} | 处理失败,可能是机器人已经被移出群组,请检查报错!".format(group)) print("群组: {} | 处理失败,可能是机器人已经被移出群组,请检查报错!".format(group))
print(e) print(e)
ctext = f'#WORDCLOUD #SCHEDULE \n' \
f'群组 ID`{group}`\n' \
f'执行操作:`生成词云`\n' \
f'结果:`失败`\n'
if not CHANNEL == 0:
bot.send_message(chat_id=CHANNEL, text=ctext, parse_mode="Markdown")
time.sleep(1) time.sleep(1)
@ -166,7 +177,7 @@ def generate(group):
chat_id=group, chat_id=group,
photo=open(img_path, "rb"), photo=open(img_path, "rb"),
caption=text, caption=text,
parse_mode='markdown', parse_mode='Markdown',
disable_notification=True disable_notification=True
) )
except Exception as e: except Exception as e: