지금까지 내용 커밋
This commit is contained in:
77
bot/src/classes/RedisClient.ts
Normal file
77
bot/src/classes/RedisClient.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Redis } from "ioredis";
|
||||
import { Config } from "../utils/Config";
|
||||
import { Logger } from "../utils/Logger";
|
||||
import { YoutubeMusic } from "../utils/api/YoutubeMusic";
|
||||
import { Spotify } from "../utils/api/Spotify";
|
||||
|
||||
export class RedisClient {
|
||||
public pub: Redis = new Redis({ host: Config.redis.host, port: Config.redis.port });
|
||||
public sub: Redis = new Redis({ host: Config.redis.host, port: Config.redis.port });
|
||||
|
||||
constructor() {
|
||||
this.pub.on("connect", () => {
|
||||
Logger.ready(`[Redis Pub] 연결 완료 (말하는 입)`);
|
||||
});
|
||||
this.sub.on("connect", () => {
|
||||
Logger.ready(`[Redis Sub] 연결 완료 (듣는 귀)`);
|
||||
});
|
||||
|
||||
this.sub.subscribe("site-bot", (err, count) => {
|
||||
if (err) return Logger.error(`[Redis Sub] 구독 실패: ${err.message}`);
|
||||
Logger.log(`[Redis Sub] 'bot-commands' 채널 구독 중... (현재 구독 채널 수: ${count})`);
|
||||
});
|
||||
|
||||
this.sub.on("message", async (ch, msg) => {
|
||||
if (ch !== "site-bot") return;
|
||||
Logger.log(`[Redis Sub] [Message] 수신: {\n 채널: ${ch}\n 내용: ${msg}\n}`);
|
||||
try {
|
||||
const data = JSON.parse(msg) as { action: "search"; requestId: string; [key: string]: any; };
|
||||
|
||||
if (data.action === "search") {
|
||||
const resultKey = `search:result:${data.requestId}`;
|
||||
const results = await Spotify.getSearchFull(data.query) ?? await YoutubeMusic.getSearchFull(data.query) ?? [];
|
||||
await this.pub.setex(resultKey, 60, JSON.stringify(results));
|
||||
Logger.log(`[Redis Pub] [setex] 결과 저장: (${resultKey})`);
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(`명령어 처리 중 에러: ${String(err)}`);
|
||||
}
|
||||
});
|
||||
|
||||
this.pub.on("error", (err) => {
|
||||
Logger.error(`[Redis Pub] [Error] ${err.message}`);
|
||||
});
|
||||
this.sub.on("error", (err) => {
|
||||
Logger.error(`[Redis Sub] [Error] ${err.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
public publishState(event: string, data: any) {
|
||||
const payload = JSON.stringify({
|
||||
event,
|
||||
timestamp: Date.now(),
|
||||
...data,
|
||||
});
|
||||
this.pub.publish("bot-site", payload);
|
||||
Logger.log(`[Redis Pub] bot -> site 전송: ${event}`);
|
||||
}
|
||||
|
||||
public runTest() {
|
||||
Logger.debug(`[Redis Test] 3초 뒤에 테스트 통신 시작...`);
|
||||
setTimeout(() => {
|
||||
// 1. 봇 -> 사이트(웹) 방향 전송 테스트
|
||||
this.publishState("TRACK_START", {
|
||||
author: "테스트",
|
||||
title: "제목",
|
||||
duration: 196000,
|
||||
});
|
||||
|
||||
// 2. 사이트(웹) -> 봇 방향 수신 테스트 (가짜 명령을 쏴서 스스로 수신하는지 확인)
|
||||
setTimeout(() => {
|
||||
const mockCommand = JSON.stringify({ action: "skip", userId: "12345" });
|
||||
// 테스트를 위해 본인이 site-bot 채널로 발행해 봅니다.
|
||||
this.pub.publish("site-bot", mockCommand);
|
||||
}, 1000);
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user