这让我抓狂。
我的应用针对开启“hold posts”功能的子reddit,因此每条帖子都会被过滤。我的应用会在所有新帖子中评论,然后在帖子被批准、删除或移除时删除该评论。我曾让它工作过一次,但当然没有保存代码。我随后更新了它并把它弄坏了,现在根本弄不清怎么让它再次工作。当新帖子被创建时,我收到 PostDelete 触发器,随后收到 PostCreate 触发器。我批准、删除或移除帖子时不会收到后续触发器,因此评论永远不会被删除。

// 将帖子 ID 正常化为始终带有 t3_ 前缀
function normalizePostId(id: string): string {
  return id.startsWith('t3_') ? id : `t3_${id}`;
}

// 为帖子 ID 无状态地删除已存储的机器人评论的助手函数
async function deleteBotCommentForPost(postId: string, context: any) {
  try {
    const normalizedId = normalizePostId(postId);
    const key = `bot_comment:${normalizedId}`;
    console.log(`[deleteBotCommentForPost] 查找 Redis 键: ${key}`);

    const commentId = await context.redis.get(key);
    console.log(`[deleteBotCommentForPost] Redis 结果 for ${key}: ${commentId ?? 'not found'}`);

    if (commentId) {
      console.log(`[deleteBotCommentForPost] 尝试删除评论 ${commentId} 为帖子 ${normalizedId}`);
      const comment = await context.reddit.getCommentById(commentId);
      await comment.delete();
      await context.redis.del(key);
      console.log(`[deleteBotCommentForPost] 成功删除评论 ${commentId} 并移除 Redis 键 ${key}`);
    } else {
      console.log(`[deleteBotCommentForPost] 在 Redis 中未找到帖子 ${normalizedId} 的机器人评论 — 无需删除`);
    }
  } catch (error) {
    console.error(`[deleteBotCommentForPost] 删除帖子 ${postId} 的机器人评论时出错:`, error);
  }
}

// 帖子创建的处理器
await recordTriggerLog('PostCreate', { postId: event.post?.id, author: event.author?.id }, context);

try {
  const postId = event.post?.id;
  if (!postId) {
    console.log('[handlePost] 事件 payload 中未找到 postId — 中止');
    return;
  }

  if (event.post?.deleted || event.post?.isLocked) {
    console.log(`[handlePost] 帖子 ${postId} 在事件快照中已被删除或锁定 — 中止`);
    return;
  }

  // 1. 监听新帖子
  Devvit.addTrigger({
    event: 'PostCreate',
    onEvent: handlePost,
  });

  // 2. 当帖子被删除或移除时自动删除评论
  Devvit.addTrigger({
    event: 'PostDelete',
    onEvent: async (event, context) => {
      const postId = event.postId || event.post?.id;
      if (event.source === 2 && event.reason === 4) {
        return;
      }
      await recordTriggerLog('PostDelete', { postId, source: event.source, reason: event.reason }, context);

      if (postId) {
        await deleteBotCommentForPost(postId, context);
      } else {
        console.log('[PostDelete] 事件 payload 中未找到 postId — 跳过');
      }
    },
  });

  // 3. 当版 moderator 批准或移除帖子时自动删除评论
  Devvit.addTrigger({
    event: 'ModAction',
    onEvent: async (event, context) => {
      const action = event.action || event.type;
      const targetPostId = event.targetPost?.id || event.target?.id;

      await recordTriggerLog('ModAction', { action, targetPostId }, context);

      try {
        const targetActions = ['approvelink', 'removelink', 'spamlink'];

        if (!action) {
          console.log('[ModAction] 事件 payload 中未找到 action — 跳过');
          return;
        }

        if (!targetActions.includes(action)) {
          console.log(`[ModAction] 动作 "${action}" 不在目标列表中 — 跳过`);
          return;
        }

        if (!targetPostId) {
          console.log(`[ModAction] 动作 "${action}" 匹配但未找到 targetPostId — 跳过`);
          return;
        }

        console.log(`[ModAction] 匹配到动作 "${action}" 为帖子 ${targetPostId} — 删除机器人评论`);
        await deleteBotCommentForPost(targetPostId, context);
      } catch (error) {
        console.error('[ModAction] ModAction 处理器中的未捕获错误:', error);
      }
    },
  });
});