加入托福词汇内容,

This commit is contained in:
liuwei
2025-03-21 11:28:32 +08:00
parent 5c0a67981b
commit 60420748df
2 changed files with 13522 additions and 12 deletions

View File

@@ -13,7 +13,8 @@ from plugins.stats_collector.decorators import plugin_stats_decorator
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
from db.sign_in import SignInDB
from db.sign_in_redis import SignInRedisDB
import random
import os
class MessageSignPlugin(MessagePluginInterface):
"""签到插件"""
@@ -49,24 +50,28 @@ class MessageSignPlugin(MessagePluginInterface):
self.timezone = 'Asia/Shanghai'
self.sign_in_db = None
self.sign_in_redis = None
# 添加词汇表文件路径和词汇列表
self.vocab_file_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
"resource", "6 托福-乱序.txt")
self.vocab_list = []
def initialize(self, context: Dict[str, Any]) -> bool:
"""初始化插件"""
self.LOG = logging.getLogger(f"Plugin.{self.name}")
self.LOG.info(f"正在初始化 {self.name} 插件...")
# 保存上下文对象
self.wcf = context.get("wcf")
self.event_system = context.get("event_system")
self.message_util:MessageUtil = context.get("message_util")
self.gbm = context.get("gbm")
self.all_contacts = context.get("all_contacts", {})
self.db_manager = DBConnectionManager.get_instance()
# 初始化数据库操作类
self.sign_in_db = SignInDB(self.db_manager)
self.sign_in_redis = SignInRedisDB(self.db_manager)
# 从配置中获取参数
sign_in_config = self._config.get("SignIn", {})
self._commands = sign_in_config.get("command", ["签到", "每日签到", "qd", "Qd", "QD", "上班", "牛马"])
@@ -75,7 +80,7 @@ class MessageSignPlugin(MessagePluginInterface):
self.streak_cycle = sign_in_config.get("streak-cycle", 1)
self.max_streak_point = sign_in_config.get("max-streak-point", 50)
self.enable = sign_in_config.get("enable", True)
# 从 Redis 初始化签到数据
self.today_signin_count = self.sign_in_redis.load_signin_count()
last_reset_date = self.sign_in_redis.get_last_reset_date()
@@ -84,9 +89,31 @@ class MessageSignPlugin(MessagePluginInterface):
else:
self.last_reset_date = datetime.now(tz=pytz.timezone(self.timezone)).date()
self.sign_in_redis.save_last_reset_date(self.last_reset_date)
self.LOG.info(f"[{self.name}] 插件初始化完成,指令:{self._commands}")
# 加载词汇表
self.load_vocabulary()
self.LOG.info(f"[{self.name}] 插件初始化完成,指令:{self._commands},已加载 {len(self.vocab_list)} 个词汇")
return True
def load_vocabulary(self):
"""加载词汇表到内存"""
try:
if os.path.exists(self.vocab_file_path):
with open(self.vocab_file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
self.vocab_list = [line.strip() for line in lines if line.strip()]
self.LOG.info(f"成功加载词汇表,共 {len(self.vocab_list)} 个单词")
else:
self.LOG.error(f"词汇表文件不存在: {self.vocab_file_path}")
except Exception as e:
self.LOG.error(f"加载词汇表出错: {e}")
def get_random_vocabulary(self) -> str:
"""从内存中获取随机词汇"""
if not self.vocab_list:
return "词汇表为空"
return random.choice(self.vocab_list)
def start(self) -> bool:
"""启动插件"""
@@ -121,11 +148,11 @@ class MessageSignPlugin(MessagePluginInterface):
wcf: Wcf = message.get("wcf")
gbm: GroupBotManager = message.get("gbm")
all_contacts = message.get("all_contacts", {})
# 检查权限
if roomid and gbm.get_group_permission(roomid, Feature.SIGNIN) == PermissionStatus.DISABLED:
return False, "没有权限"
try:
# 获取当前时间,带有时区信息
current_time = datetime.now(tz=pytz.timezone(self.timezone))
@@ -192,16 +219,22 @@ class MessageSignPlugin(MessagePluginInterface):
points_to_add, current_time, streak
)
# 在签到成功后添加每日词汇
# 在输出信息中添加每日词汇
output = f"签到成功,加[{points_to_add}]分,第[{today_signin_rank}]个!"
if streak_broken and old_streak > 0: # 只有在真的断签且之前有签到记录时才显示
output += f"断开了 {old_streak} 天连签!"
elif streak > 1:
output += f"连签 {streak} 天!"
# 从内存中获取随机词汇
daily_vocab = self.get_random_vocabulary()
output += f"\n今日词汇:{daily_vocab}"
self.message_util.send_text_msg(output, (roomid if roomid else sender), sender)
return True, "签到成功"
except Exception as e:
self.LOG.error(f"处理签到请求出错: {e}")
self.message_util.send_text_msg(f"签到出错:{e}",

13477
resource/6 托福-乱序.txt Normal file

File diff suppressed because it is too large Load Diff