2026-06-09 17:34:18 +07:00
|
|
|
import {
|
|
|
|
|
COMMAND_GUILDS_LIST,
|
|
|
|
|
COMMAND_GUILDS_TEXT_CHANNELS,
|
|
|
|
|
COMMAND_VOICE_CHANNELS,
|
|
|
|
|
COMMAND_VOICE_CONNECT,
|
|
|
|
|
COMMAND_VOICE_DISCONNECT,
|
|
|
|
|
VOICE_STATUS_KEY,
|
|
|
|
|
} from "@bete/shared";
|
2026-06-02 21:06:42 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
2026-06-09 10:16:04 +07:00
|
|
|
import { getPool } from "../../shared/database/index.js";
|
|
|
|
|
import { publishCommand, readRedisStatus } from "../../shared/redis/index.js";
|
2026-06-02 00:32:38 +07:00
|
|
|
|
|
|
|
|
const logger = createChildLogger("voice.service");
|
2026-06-01 21:44:29 +07:00
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
export interface Guild {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
2026-06-02 00:32:38 +07:00
|
|
|
icon: string | null;
|
2026-06-01 21:44:29 +07:00
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
export interface Channel {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
type: "voice" | "text";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface VoiceStatus {
|
|
|
|
|
connected: boolean;
|
2026-06-02 00:32:38 +07:00
|
|
|
activeGuildId: string | null;
|
|
|
|
|
activeChannelId: string | null;
|
|
|
|
|
activeChannelName: string | null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
/**
|
2026-06-02 11:02:54 +07:00
|
|
|
* Get guilds — query from discord-gateway via Redis command for real names.
|
|
|
|
|
* Falls back to database (distinct guild_id from messages) if gateway unreachable.
|
2026-06-02 00:11:29 +07:00
|
|
|
*/
|
|
|
|
|
export async function getGuilds(): Promise<Guild[]> {
|
2026-06-09 17:34:18 +07:00
|
|
|
logger.info("getGuilds called");
|
|
|
|
|
const reply = await publishCommand<Guild[]>(COMMAND_GUILDS_LIST, {});
|
2026-06-09 10:16:04 +07:00
|
|
|
if (reply?.success && reply.data && reply.data.length > 0) return reply.data;
|
2026-06-02 11:02:54 +07:00
|
|
|
|
|
|
|
|
// Fallback: Postgres with synthetic names
|
|
|
|
|
logger.warn(
|
|
|
|
|
"discord-gateway unreachable, falling back to Postgres for guilds",
|
|
|
|
|
);
|
2026-06-02 00:32:38 +07:00
|
|
|
const pool = getPool();
|
|
|
|
|
const { rows } = await pool.query(
|
|
|
|
|
`SELECT DISTINCT guild_id FROM messages ORDER BY guild_id`,
|
2026-06-02 00:11:29 +07:00
|
|
|
);
|
|
|
|
|
|
2026-06-02 00:32:38 +07:00
|
|
|
return rows.map((row: Record<string, unknown>) => ({
|
2026-06-02 00:11:29 +07:00
|
|
|
id: String(row.guild_id ?? ""),
|
|
|
|
|
name: `Guild ${String(row.guild_id).slice(0, 8)}`,
|
2026-06-02 00:32:38 +07:00
|
|
|
icon: null,
|
2026-06-02 00:11:29 +07:00
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-02 11:02:54 +07:00
|
|
|
* Get text channels — query from discord-gateway via Redis command for real names.
|
|
|
|
|
* Falls back to database if gateway unreachable.
|
2026-06-02 00:11:29 +07:00
|
|
|
*/
|
|
|
|
|
export async function getTextChannels(guildId: string): Promise<Channel[]> {
|
2026-06-09 17:34:18 +07:00
|
|
|
logger.info({ guildId }, "getTextChannels called");
|
|
|
|
|
const reply = await publishCommand<Channel[]>(COMMAND_GUILDS_TEXT_CHANNELS, {
|
2026-06-02 11:02:54 +07:00
|
|
|
guildId,
|
|
|
|
|
});
|
2026-06-09 10:16:04 +07:00
|
|
|
if (reply?.success && reply.data && reply.data.length > 0) return reply.data;
|
2026-06-02 11:02:54 +07:00
|
|
|
|
|
|
|
|
// Fallback: Postgres with synthetic names
|
|
|
|
|
logger.warn(
|
|
|
|
|
{ guildId },
|
|
|
|
|
"discord-gateway unreachable, falling back to Postgres for text channels",
|
|
|
|
|
);
|
2026-06-02 00:32:38 +07:00
|
|
|
const pool = getPool();
|
|
|
|
|
const { rows } = await pool.query(
|
|
|
|
|
`SELECT DISTINCT channel_id FROM messages WHERE guild_id = $1 ORDER BY channel_id`,
|
|
|
|
|
[guildId],
|
2026-06-02 00:11:29 +07:00
|
|
|
);
|
|
|
|
|
|
2026-06-02 00:32:38 +07:00
|
|
|
return rows.map((row: Record<string, unknown>) => ({
|
2026-06-02 00:11:29 +07:00
|
|
|
id: String(row.channel_id ?? ""),
|
|
|
|
|
name: `Channel ${String(row.channel_id).slice(0, 8)}`,
|
|
|
|
|
type: "text" as const,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-02 00:32:38 +07:00
|
|
|
* Get voice channels — query from discord-gateway via Redis command.
|
2026-06-02 00:11:29 +07:00
|
|
|
*/
|
2026-06-02 00:32:38 +07:00
|
|
|
export async function getVoiceChannels(guildId: string): Promise<Channel[]> {
|
2026-06-09 17:34:18 +07:00
|
|
|
logger.info({ guildId }, "getVoiceChannels called");
|
|
|
|
|
const reply = await publishCommand<Channel[]>(COMMAND_VOICE_CHANNELS, {
|
|
|
|
|
guildId,
|
|
|
|
|
});
|
2026-06-02 21:06:42 +07:00
|
|
|
return reply?.success && reply.data ? reply.data : [];
|
2026-06-02 00:11:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-02 00:32:38 +07:00
|
|
|
* Get current voice connection status from Redis cache set by discord-gateway.
|
2026-06-02 00:11:29 +07:00
|
|
|
*/
|
2026-06-02 00:32:38 +07:00
|
|
|
export async function getVoiceStatus(): Promise<VoiceStatus> {
|
2026-06-09 17:34:18 +07:00
|
|
|
logger.debug("getVoiceStatus called");
|
|
|
|
|
const cached = await readRedisStatus(VOICE_STATUS_KEY);
|
2026-06-02 21:06:42 +07:00
|
|
|
if (cached) return cached as unknown as VoiceStatus;
|
2026-06-02 00:11:29 +07:00
|
|
|
return {
|
|
|
|
|
connected: false,
|
2026-06-02 00:32:38 +07:00
|
|
|
activeGuildId: null,
|
|
|
|
|
activeChannelId: null,
|
|
|
|
|
activeChannelName: null,
|
2026-06-02 00:11:29 +07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-02 00:32:38 +07:00
|
|
|
* Connect to a voice channel via Redis command to discord-gateway.
|
2026-06-02 00:11:29 +07:00
|
|
|
*/
|
|
|
|
|
export async function connectVoice(
|
2026-06-02 00:32:38 +07:00
|
|
|
guildId: string,
|
|
|
|
|
channelId: string,
|
2026-06-02 00:11:29 +07:00
|
|
|
): Promise<VoiceStatus> {
|
2026-06-09 17:34:18 +07:00
|
|
|
logger.info({ guildId, channelId }, "connectVoice called");
|
|
|
|
|
const reply = await publishCommand<VoiceStatus>(COMMAND_VOICE_CONNECT, {
|
2026-06-02 00:32:38 +07:00
|
|
|
guildId,
|
|
|
|
|
channelId,
|
|
|
|
|
});
|
2026-06-02 21:06:42 +07:00
|
|
|
if (reply?.success && reply.data) return reply.data;
|
2026-06-02 00:32:38 +07:00
|
|
|
|
|
|
|
|
// Fallback: read from Redis status key
|
2026-06-09 17:34:18 +07:00
|
|
|
const cached = await readRedisStatus(VOICE_STATUS_KEY);
|
2026-06-09 10:16:04 +07:00
|
|
|
return (
|
|
|
|
|
(cached as unknown as VoiceStatus) ?? {
|
|
|
|
|
connected: false,
|
|
|
|
|
activeGuildId: null,
|
|
|
|
|
activeChannelId: null,
|
|
|
|
|
activeChannelName: null,
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-06-02 00:11:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-02 00:32:38 +07:00
|
|
|
* Disconnect from voice via Redis command to discord-gateway.
|
2026-06-02 00:11:29 +07:00
|
|
|
*/
|
|
|
|
|
export async function disconnectVoice(): Promise<VoiceStatus> {
|
2026-06-09 17:34:18 +07:00
|
|
|
logger.info("disconnectVoice called");
|
|
|
|
|
const reply = await publishCommand<VoiceStatus>(COMMAND_VOICE_DISCONNECT, {});
|
2026-06-02 21:06:42 +07:00
|
|
|
if (reply?.success && reply.data) return reply.data;
|
2026-06-02 00:32:38 +07:00
|
|
|
|
2026-06-09 17:34:18 +07:00
|
|
|
const cached = await readRedisStatus(VOICE_STATUS_KEY);
|
2026-06-09 10:16:04 +07:00
|
|
|
return (
|
|
|
|
|
(cached as unknown as VoiceStatus) ?? {
|
|
|
|
|
connected: false,
|
|
|
|
|
activeGuildId: null,
|
|
|
|
|
activeChannelId: null,
|
|
|
|
|
activeChannelName: null,
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-06-02 00:11:29 +07:00
|
|
|
}
|