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

Method handleCommand

goodnight/goodnight.ts:80–177  ·  view source on GitHub ↗
(msg: Api.Message)

Source from the content-addressed store, hash-verified

78 gn: async (msg: Api.Message) => {
79 await this.handleCommand(msg);
80 }
81 };
82
83 // 统一处理指令逻辑
84 private async handleCommand(msg: Api.Message) {
85 if (!this.db) await this.initDB();
86
87 const chatId = msg.chatId?.toString();
88 if (!chatId) return;
89
90 // 获取或初始化数据
91 let groupData = this.db.data.groups[chatId];
92
93 // 如果是新群组,默认初始化
94 if (!groupData) {
95 const defaultTimezone = 8; // 默认 UTC+8
96 const targetDate = this.getDateByTimezone(defaultTimezone);
97
98 groupData = {
99 enabled: false,
100 timezone: defaultTimezone,
101 date: dayjs(targetDate).format("YYYY-MM-DD"),
102 sleepUsers: [],
103 wakeUsers: []
104 };
105 this.db.data.groups[chatId] = groupData;
106 }
107
108 // 确保旧数据有 timezone 字段
109 if (typeof groupData.timezone === 'undefined') {
110 groupData.timezone = 8;
111 }
112
113 // 解析参数
114 const text = msg.text || "";
115 const parts = text.trim().split(/\s+/);
116 // parts[0] 是命令,parts[1] 是参数
117 const subCommand = parts[1]?.toLowerCase();
118
119 // 解析时区输入
120 // 支持格式: 8, +8, -5, utc+8, utc-5, gmt+8
121 let timezoneInput = NaN;
122 if (subCommand && subCommand !== "on" && subCommand !== "off") {
123 // 移除 utc 或 gmt 前缀,保留符号和数字
124 const cleaned = subCommand.replace(/^(utc|gmt)/i, '');
125 timezoneInput = parseInt(cleaned);
126 }
127
128 if (subCommand === "on") {
129 if (groupData.enabled) {
130 await msg.edit({ text: "✅ 本群早晚安统计已经是<b>开启</b>状态", parseMode: "html" });
131 } else {
132 groupData.enabled = true;
133 await this.db.write();
134 await msg.edit({ text: "✅ 本群早晚安统计已<b>开启</b>", parseMode: "html" });
135 }
136 } else if (subCommand === "off") {
137 if (!groupData.enabled) {

Callers 1

GreetingPluginClass · 0.95

Calls 4

initDBMethod · 0.95
getDateByTimezoneMethod · 0.95
writeMethod · 0.80
editMethod · 0.45

Tested by

no test coverage detected