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

Function scheduleTask

acron/acron.ts:278–451  ·  view source on GitHub ↗
(task: AcronTask)

Source from the content-addressed store, hash-verified

276}
277
278async function scheduleTask(task: AcronTask) {
279 const key = makeCronKey(task.id);
280 if (task.disabled) return;
281 if (cronManager.has(key)) return;
282
283 cronManager.set(key, task.cron, async () => {
284 const db = await getDB();
285 const idx = db.data.tasks.findIndex((t) => t.id === task.id);
286 const now = Date.now();
287 try {
288 const client = await getGlobalClient();
289 // NOTE: https://docs.telethon.dev/en/stable/concepts/entities.html
290 await client.getDialogs();
291 const chatIdNum = toInt((task as any).chatId);
292 const entityLike = (chatIdNum as any) ?? task.chat;
293
294 if (task.type === "send") {
295 const t = task as SendTask;
296 const entities = reviveEntities(t.entities);
297 const replyTo = t.replyTo ? toInt(t.replyTo) : undefined;
298 await client.sendMessage(entityLike, {
299 message: t.message,
300 formattingEntities: entities,
301 ...(replyTo ? { replyTo } : {}),
302 });
303 if (idx >= 0) {
304 db.data.tasks[idx].lastRunAt = String(now);
305 db.data.tasks[idx].lastResult = `已发送 1 条消息`;
306 db.data.tasks[idx].lastError = undefined;
307 await db.write();
308 }
309 } else if (task.type === "cmd") {
310 const t = task as CmdTask;
311 const cmd = await getCommandFromMessage(t.message);
312 const replyTo = t.replyTo ? toInt(t.replyTo) : undefined;
313 const sudoMsg = await client.sendMessage(entityLike, {
314 message: t.message,
315 ...(replyTo ? { replyTo } : {}),
316 });
317 if (cmd && sudoMsg)
318 await dealCommandPluginWithMessage({ cmd, msg: sudoMsg as any });
319 if (idx >= 0) {
320 db.data.tasks[idx].lastRunAt = String(now);
321 db.data.tasks[idx].lastResult = `已执行命令`;
322 db.data.tasks[idx].lastError = undefined;
323 await db.write();
324 }
325 } else if (task.type === "copy") {
326 const t = task as CopyTask;
327 const fromChatIdNum = toInt(t.fromChatId);
328 const fromMsgIdNum = toInt(t.fromMsgId);
329 try {
330 // 获取源消息(尽量使用完整实体以避免 hash 失效)
331 const fromEntityLike = (fromChatIdNum as any) ?? t.fromChatId;
332 const messages = await safeGetMessages(client, fromEntityLike as any, {
333 ids: fromMsgIdNum,
334 });
335 const realtimeMsg = messages?.[0] as any;

Callers 2

bootstrapTasksFunction · 0.70
AcronPluginClass · 0.70

Calls 8

writeMethod · 0.80
pinMessageMethod · 0.80
errorMethod · 0.80
makeCronKeyFunction · 0.70
getDBFunction · 0.70
toIntFunction · 0.70
tryParseRegexFunction · 0.70
setMethod · 0.45

Tested by

no test coverage detected