Dev

개인 서버 CLI에 Claude Code + Discord - #2 Session 추가

ybjeon.today 2026. 6. 5. 21:07

목표

  • 귀찮은 prefix 삭제: ex) !claude show me the last commit
  • Chatting history + multi-channel을 위한
    • Session 추가
    • Chatting history 추가

코드

코드 링크: https://github.com/ybjeon/claude-discord-bot

버전: tag/v1.0.1

 

 

Claude code에는 session 개념이 있다. 이를 이용하면 앞에서 한 얘기를 연속해서 기억할 수 있고, 다른 채널에서 병렬적으로 Session이 holding 되어 편리하다.

 

 

index.js

claude 명령어 session flag이용

function runClaude(prompt, sessionEntry) {
  return new Promise((resolve, reject) => {
    const cwd = process.env.PROJECT_DIR || process.cwd();
    const sessionFlag = sessionEntry.initialized
      ? ["--resume", sessionEntry.sessionId]
      : ["--session-id", sessionEntry.sessionId];

    execFile(
      "claude",
      [
        "-p",
        prompt,
        ...sessionFlag,
        "--append-system-prompt",
        "You are being called from a Discord bot. Be concise. Do not expose secrets.",
      ],
      {
        cwd,
        timeout: 120_000,
        maxBuffer: 1024 * 1024 * 5,
      },
      (error, stdout, stderr) => {
        if (error) return reject(stderr || error.message);
        resolve(stdout.trim());
      }
    );
  });
}

 

결과

동시에 다른 채널에서 얘기를 해도 session이 분리된 상태로 유지됨

한 세션 안에서 내 별명을 기억
다른 세션에서는 기존 세션에서의 대화를 알지 못한다.

 

todos

  • 채널 & 프로젝트 디렉토리 1-to-1 mapping
  • Write/Execute 테스트