[메신저봇R] 닉네임 변경 감지 코드 및 설명

작성자 정보

  • 매니저 작성
  • 작성일

컨텐츠 정보

  • 207 조회
  • 0 추천
  • 0 비추천
  • 목록


본문

주로 카카오톡을 활용한 메신저봇R의 소스코드를 설명 해 드리려고 합니다.

어디까지나 공부 목적이며 카카오톡의 기능을 불법적으로 개조해서 사용하는 것은 금지되어 있습니다.

response의 메소드 내부에서 작성하시길 바랍니다.

메신저봇r 0.7.35a버전을 사용 했으며 로컬 db에서 관리합니다.


    // 절대패스 & 방 & 파일명

    const basePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/dev/";

    const roomPath = basePath + room; // 방 이름을 포함한 디렉터리 경로

    const filePath = roomPath + "/userData.txt"; // 방 이름 기반 파일 경로


    // 디렉터리 생성

    const roomDir = new java.io.File(roomPath);

    if (!roomDir.exists()) {

        roomDir.mkdirs(); // 디렉터리가 없으면 생성

    }


    let userCounts;


    // 파일 읽기

    try {

        const fileContent = FileStream.read(filePath);

        userCounts = fileContent ? JSON.parse(fileContent) : {};

    } catch (e) {

        userCounts = {}; // 파일이 없으면 빈 객체 생성

    }


    const userId = java.lang.String(userHash).hashCode(); // userHash를 사용해 ID 생성


    // userId가 없으면 새로운 데이터로 초기화

    if (!userCounts[userId]) {

        userCounts[userId] = { count: 0, sender: sender, userId: userId };

    }


    // 기존 이름 변경 처리

    if (userCounts[userId].sender !== sender) {

        // 기존 이름과 비교하여 다르면 이름 변경

        const oldName = userCounts[userId].sender;


        // 기존 데이터에서 sender만 변경 (count는 그대로 유지)

        userCounts[userId].sender = sender;


        // 변경 메시지 출력 (필요시)

        replier.reply("닉네임 변경 처리"+"\u200b".repeat(500)+"\n" +

                oldName + " >> " + sender + "완료.");

        } else {

        // 카운트 증가

        if (userCounts[userId]) {

            userCounts[userId].count++;

        }

    }


    // 파일에 저장

    FileStream.write(filePath, JSON.stringify(userCounts));

    }


파일 스트림을 사용해서 로컬 DB로 넣을 수 있습니다.

궁금한 내용은 댓글로 달아주세요.



관련자료

댓글 0
등록된 댓글이 없습니다.
알림 0