redis onoff만들어서 쓸때만 .env에 on하기

This commit is contained in:
tkrmagid-desktop
2026-04-26 20:23:17 +09:00
parent 3864b5d082
commit 04044650d0
7 changed files with 28 additions and 15 deletions

View File

@@ -32,7 +32,7 @@ export class GuildPlayer {
) { ) {
this.player.setGlobalVolume(50); this.player.setGlobalVolume(50);
this.player.on("start", (_data: TrackStartEvent) => { this.player.on("start", (_data: TrackStartEvent) => {
Redis.publishState("player_update", { Redis?.publishState("player_update", {
guildId: this.guild.id, guildId: this.guild.id,
}); });
}); });
@@ -189,7 +189,7 @@ export class GuildPlayer {
if (!this.isPlaying) return; if (!this.isPlaying) return;
if (!this.nowTrack) return; if (!this.nowTrack) return;
this.player.seekTo(num); this.player.seekTo(num);
Redis.publishState("player_update", { guildId: this.guild.id }); Redis?.publishState("player_update", { guildId: this.guild.id });
} }
private async autoPlay() { private async autoPlay() {
@@ -244,8 +244,8 @@ export class GuildPlayer {
} }
public async setMsg(update: { player: boolean; queue: boolean; } = { player: true, queue: true }) { public async setMsg(update: { player: boolean; queue: boolean; } = { player: true, queue: true }) {
if (update.player) Redis.publishState("player_update", { guildId: this.guild.id }); if (update.player) Redis?.publishState("player_update", { guildId: this.guild.id });
if (update.queue) Redis.publishState("queue_update", { guildId: this.guild.id }); if (update.queue) Redis?.publishState("queue_update", { guildId: this.guild.id });
const { channel, msg, check } = await checkTextChannelAndMsg(this.guild, this.textChannel, this.msg); const { channel, msg, check } = await checkTextChannelAndMsg(this.guild, this.textChannel, this.msg);
if (!check) return; if (!check) return;
this.textChannel = channel; this.textChannel = channel;

View File

@@ -23,7 +23,12 @@ type SubAction =
"queue_set" | "queue_set" |
"queue_remove"; "queue_remove";
export class RedisClient { export const RedisClient = () => {
if (Config.redis.state) return new RedisClientClass();
return null;
}
class RedisClientClass {
public pub: Redis = new Redis({ host: Config.redis.host, port: Config.redis.port }); 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 }); public sub: Redis = new Redis({ host: Config.redis.host, port: Config.redis.port });

View File

@@ -14,9 +14,11 @@ export const clientReady = async () => {
reloadMsg(); reloadMsg();
const guildIds = client.guilds.cache.map(guild => guild.id); if (Redis) {
Redis.pub.set("bot-guilds", JSON.stringify(guildIds)); const guildIds = client.guilds.cache.map(guild => guild.id);
Logger.info(`[Redis Pub] bot-guilds 설정 완료: [${guildIds.join(",")}]`); Redis.pub.set("bot-guilds", JSON.stringify(guildIds));
Logger.info(`[Redis Pub] bot-guilds 설정 완료: [${guildIds.join(",")}]`);
}
if (!Config.dev) return; if (!Config.dev) return;
try { try {

View File

@@ -2,7 +2,9 @@ import { client, Redis } from "../index";
import { Logger } from "../utils/Logger"; import { Logger } from "../utils/Logger";
export const guildCreate = async () => { export const guildCreate = async () => {
const guildIds = client.guilds.cache.map(guild => guild.id); if (Redis) {
Redis.pub.set("bot-guilds", JSON.stringify(guildIds)); const guildIds = client.guilds.cache.map(guild => guild.id);
Logger.info(`[Redis Pub] bot-guilds 수정 완료: [${guildIds.join(",")}]`); Redis.pub.set("bot-guilds", JSON.stringify(guildIds));
Logger.info(`[Redis Pub] bot-guilds 수정 완료: [${guildIds.join(",")}]`);
}
} }

View File

@@ -2,7 +2,9 @@ import { client, Redis } from "../index";
import { Logger } from "../utils/Logger"; import { Logger } from "../utils/Logger";
export const guildDelete = async () => { export const guildDelete = async () => {
const guildIds = client.guilds.cache.map(guild => guild.id); if (Redis) {
Redis.pub.set("bot-guilds", JSON.stringify(guildIds)); const guildIds = client.guilds.cache.map(guild => guild.id);
Logger.info(`[Redis Pub] bot-guilds 수정 완료: [${guildIds.join(",")}]`); Redis.pub.set("bot-guilds", JSON.stringify(guildIds));
Logger.info(`[Redis Pub] bot-guilds 수정 완료: [${guildIds.join(",")}]`);
}
} }

View File

@@ -21,7 +21,7 @@ process.on('SIGTERM', () => {
export const client = new BotClient(); export const client = new BotClient();
export const lavalinkManager = new LavalinkManager(client); export const lavalinkManager = new LavalinkManager(client);
export const handler = new Handler(); export const handler = new Handler();
export const Redis = new RedisClient(); export const Redis = RedisClient();
for (const eventName of Object.keys(onEvents) as (keyof typeof onEvents)[]) { for (const eventName of Object.keys(onEvents) as (keyof typeof onEvents)[]) {
client.onEvent(eventName, onEvents[eventName]); client.onEvent(eventName, onEvents[eventName]);

View File

@@ -70,6 +70,7 @@ export const Config = {
}, },
_redis: { _redis: {
state: process.env.REDIS?.trim()?.toLocaleLowerCase() === "true",
host: process.env.REDIS_HOST?.trim(), host: process.env.REDIS_HOST?.trim(),
port: process.env.REDIS_PORT?.trim(), port: process.env.REDIS_PORT?.trim(),
}, },
@@ -79,6 +80,7 @@ export const Config = {
const port = Number(this._redis.port!); const port = Number(this._redis.port!);
if (isNaN(port)) throw new TypeError("REDIS_PORT must be a number"); if (isNaN(port)) throw new TypeError("REDIS_PORT must be a number");
return { return {
state: this._redis.state,
host: this._redis.host, host: this._redis.host,
port: port, port: port,
}; };