MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / processGreeting

Method processGreeting

goodnight/goodnight.ts:223–290  ·  view source on GitHub ↗
(msg: Api.Message, chatId: string, userId: string, type: "sleep" | "wake")

Source from the content-addressed store, hash-verified

221 const utcTime = now.getTime() + (now.getTimezoneOffset() * 60000);
222 // 加上目标时区偏移 (小时 * 60 * 60 * 1000)
223 return new Date(utcTime + (timezoneOffset * 3600000));
224 }
225
226 // 核心处理逻辑
227 private async processGreeting(msg: Api.Message, chatId: string, userId: string, type: "sleep" | "wake") {
228 // 确保数据库已加载
229 if (!this.db) await this.initDB();
230
231 // 获取群组数据(listener 已确保数据存在且 enabled=true)
232 let groupData = this.db.data.groups[chatId];
233
234 // 确保 timezone 存在 (向后兼容)
235 const timezone = typeof groupData.timezone === 'number' ? groupData.timezone : 8;
236
237 // 基于群组时区计算当前日期和时间
238 const targetDate = this.getDateByTimezone(timezone);
239 const today = dayjs(targetDate).format("YYYY-MM-DD");
240
241 // 如果日期不是今天,则重置每日数据
242 if (groupData.date !== today) {
243 groupData.date = today;
244 groupData.sleepUsers = [];
245 groupData.wakeUsers = [];
246 // 如果旧数据没有 timezone 字段,借此机会补上
247 groupData.timezone = timezone;
248 }
249
250 // 获取对应的用户列表
251 const list = type === "sleep" ? groupData.sleepUsers : groupData.wakeUsers;
252
253 // 计算排名
254 let rank = 0;
255 const userIndex = list.indexOf(userId);
256
257 if (userIndex !== -1) {
258 // 如果用户已经在列表中,使用已有排名(索引+1)
259 rank = userIndex + 1;
260 } else {
261 // 如果是新用户,添加到列表末尾
262 list.push(userId);
263 rank = list.length;
264 // 保存数据库
265 await this.db.write();
266 }
267
268 // 获取用户显示名称
269 let senderName = "神秘人";
270 try {
271 const sender = await msg.getSender() as any;
272 if (sender) {
273 senderName = sender.firstName || sender.username || "群友";
274 }
275 } catch (e) {
276 console.error("获取用户信息失败", e);
277 }
278
279 // 构建回复内容
280 const currentTime = dayjs(targetDate).format("YYYY-MM-DD HH:mm:ss");

Callers 1

GreetingPluginClass · 0.95

Calls 4

initDBMethod · 0.95
getDateByTimezoneMethod · 0.95
writeMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected