검색해서 재생기능 제작
검색하고 재생 누르면 재생됨 플레이리스트 재생 기능(주소 전달해서 재생하는 방식)
This commit is contained in:
@@ -3,6 +3,11 @@ import { Config } from "../utils/Config";
|
||||
import { Logger } from "../utils/Logger";
|
||||
import { YoutubeMusic } from "../utils/api/YoutubeMusic";
|
||||
import { Spotify } from "../utils/api/Spotify";
|
||||
import { lavalinkManager } from "../index";
|
||||
import { getGuildById, getVoiceChannelById } from "../utils/music/Channel";
|
||||
import { channelJoin } from "../commands/join";
|
||||
|
||||
type SubAction = "search" | "player_play" | "player_playlist";
|
||||
|
||||
export class RedisClient {
|
||||
public pub: Redis = new Redis({ host: Config.redis.host, port: Config.redis.port });
|
||||
@@ -21,18 +26,50 @@ export class RedisClient {
|
||||
Logger.log(`[Redis Sub] 'bot-commands' 채널 구독 중... (현재 구독 채널 수: ${count})`);
|
||||
});
|
||||
|
||||
this.sub.on("message", async (ch, msg) => {
|
||||
this.sub.on("message", async (ch, msg): Promise<any> => {
|
||||
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; };
|
||||
const data = JSON.parse(msg) as { action: SubAction; requestId: string; userId?: string; [key: string]: any; };
|
||||
|
||||
if (data.action === "search") {
|
||||
const resultKey = `search:result:${data.requestId}`;
|
||||
const resultKey = `search:${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})`);
|
||||
}
|
||||
else if (data.action === "player_play") {
|
||||
const resultKey = `player:play:${data.requestId}`;
|
||||
if (!data.serverId) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "serverId를 찾을수 없습니다." }));
|
||||
if (!data.userId) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "userId를 찾을수 없습니다." }));
|
||||
const guild = await getGuildById(data.serverId);
|
||||
if (!guild) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "guild를 찾을수 없습니다." }));
|
||||
let player = lavalinkManager.getPlayer(guild.id);
|
||||
const voiceChannel = await getVoiceChannelById(guild, data.userId);
|
||||
if (!player) {
|
||||
if (!voiceChannel) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "음성채널에 들어가서 이용해주세요." }));
|
||||
player = (await channelJoin(guild, voiceChannel.id)).player;
|
||||
}
|
||||
if (!player) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "세션을 찾을수 없습니다." }));
|
||||
await lavalinkManager.search(guild.id, data.track.url, data.userId, player);
|
||||
// await this.pub.setex(resultKey, 60, JSON.stringify({ success: true, message: "노래 추가 완료" }));
|
||||
}
|
||||
else if (data.action === "player_playlist") {
|
||||
const resultKey = `player:play:${data.requestId}`;
|
||||
if (!data.serverId) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "serverId를 찾을수 없습니다." }));
|
||||
if (!data.userId) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "userId를 찾을수 없습니다." }));
|
||||
const guild = await getGuildById(data.serverId);
|
||||
if (!guild) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "guild를 찾을수 없습니다." }));
|
||||
let player = lavalinkManager.getPlayer(guild.id);
|
||||
const voiceChannel = await getVoiceChannelById(guild, data.userId);
|
||||
if (!player) {
|
||||
if (!voiceChannel) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "음성채널에 들어가서 이용해주세요." }));
|
||||
player = (await channelJoin(guild, voiceChannel.id)).player;
|
||||
}
|
||||
if (!player) return await this.pub.setex(resultKey, 60, JSON.stringify({ success: false, message: "세션을 찾을수 없습니다." }));
|
||||
await lavalinkManager.search(guild.id, data.playlistUrl, data.userId, player);
|
||||
// await this.pub.setex(resultKey, 60, JSON.stringify({ success: true, message: "플레이리스트 추가 완료" }));
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(`명령어 처리 중 에러: ${String(err)}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user