Update code to typescript
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
Events,
|
||||
GatewayDispatchEvents,
|
||||
type Client,
|
||||
type Guild,
|
||||
type VoiceBasedChannel,
|
||||
} from "discord.js";
|
||||
import type {
|
||||
DiscordGatewayAdapterCreator,
|
||||
DiscordGatewayAdapterLibraryMethods,
|
||||
} from "@discordjs/voice";
|
||||
|
||||
const adapters = new Map<string, DiscordGatewayAdapterLibraryMethods>();
|
||||
const trackedClients = new Set<Client>();
|
||||
const trackedShards = new Map<number, Set<string>>();
|
||||
|
||||
function trackClient(client: Client): void {
|
||||
if (trackedClients.has(client)) return;
|
||||
trackedClients.add(client);
|
||||
|
||||
client.ws.on(GatewayDispatchEvents.VoiceServerUpdate, (payload) => {
|
||||
adapters.get(payload.guild_id)?.onVoiceServerUpdate(payload);
|
||||
});
|
||||
|
||||
client.ws.on(GatewayDispatchEvents.VoiceStateUpdate, (payload) => {
|
||||
if (payload.guild_id && payload.session_id && payload.user_id === client.user?.id) {
|
||||
adapters.get(payload.guild_id)?.onVoiceStateUpdate(payload);
|
||||
}
|
||||
});
|
||||
|
||||
client.on(Events.ShardDisconnect, (_event, shardId) => {
|
||||
const guilds = trackedShards.get(shardId);
|
||||
if (guilds) {
|
||||
for (const guildID of guilds.values()) {
|
||||
adapters.get(guildID)?.destroy();
|
||||
}
|
||||
}
|
||||
trackedShards.delete(shardId);
|
||||
});
|
||||
}
|
||||
|
||||
function trackGuild(guild: Guild): void {
|
||||
let guilds = trackedShards.get(guild.shardId);
|
||||
if (!guilds) {
|
||||
guilds = new Set();
|
||||
trackedShards.set(guild.shardId, guilds);
|
||||
}
|
||||
guilds.add(guild.id);
|
||||
}
|
||||
|
||||
export function createAdapter(channel: VoiceBasedChannel): DiscordGatewayAdapterCreator {
|
||||
return (methods) => {
|
||||
adapters.set(channel.guild.id, methods);
|
||||
trackClient(channel.client);
|
||||
trackGuild(channel.guild);
|
||||
return {
|
||||
sendPayload(data) {
|
||||
channel.guild.shard.send(data);
|
||||
return true;
|
||||
},
|
||||
destroy() {
|
||||
return void adapters.delete(channel.guild.id);
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import {
|
||||
AudioPlayerStatus,
|
||||
createAudioResource,
|
||||
type AudioPlayer,
|
||||
type AudioResource,
|
||||
} from "@discordjs/voice";
|
||||
|
||||
export class AudioQueue {
|
||||
private queue: string[] = [];
|
||||
private current: AudioResource | undefined;
|
||||
|
||||
constructor(private readonly player: AudioPlayer) {
|
||||
this.player.on(AudioPlayerStatus.Idle, () => this.handleStop());
|
||||
}
|
||||
|
||||
add(filepath: string): void {
|
||||
this.queue.push(filepath);
|
||||
if (this.queue.length === 1) this.playNext();
|
||||
}
|
||||
|
||||
flush(): void {
|
||||
this.current?.volume?.setVolume(0);
|
||||
this.queue = [];
|
||||
this.playNext();
|
||||
}
|
||||
|
||||
private playNext(): void {
|
||||
const next = this.queue[0];
|
||||
if (next === undefined) {
|
||||
this.current = undefined;
|
||||
return;
|
||||
}
|
||||
const resource = createAudioResource(next, { inlineVolume: true });
|
||||
this.player.play(resource);
|
||||
this.current = resource;
|
||||
}
|
||||
|
||||
private handleStop(): void {
|
||||
this.queue.shift();
|
||||
this.playNext();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
createAudioPlayer,
|
||||
joinVoiceChannel,
|
||||
type AudioPlayer,
|
||||
type VoiceConnection,
|
||||
} from "@discordjs/voice";
|
||||
import type { Message, VoiceBasedChannel } from "discord.js";
|
||||
import { createAdapter } from "../adapter.js";
|
||||
import type { BaseEngine, VoiceParams } from "../tts/BaseEngine.js";
|
||||
import type { TTSRegistry } from "../tts/registry.js";
|
||||
import { AudioQueue } from "./AudioQueue.js";
|
||||
|
||||
export interface AudioServiceOptions {
|
||||
voiceTmpPath: string;
|
||||
tts: TTSRegistry;
|
||||
}
|
||||
|
||||
export class AudioService {
|
||||
readonly player: AudioPlayer = createAudioPlayer();
|
||||
readonly queue: AudioQueue = new AudioQueue(this.player);
|
||||
|
||||
private joinedChannels: VoiceBasedChannel[] = [];
|
||||
private connections = new Map<VoiceBasedChannel, VoiceConnection>();
|
||||
|
||||
constructor(private readonly opts: AudioServiceOptions) {}
|
||||
|
||||
getActiveVoiceChannel(): VoiceBasedChannel | undefined {
|
||||
return this.joinedChannels[0];
|
||||
}
|
||||
|
||||
isInVoiceChannel(channel: VoiceBasedChannel): boolean {
|
||||
return this.joinedChannels.includes(channel);
|
||||
}
|
||||
|
||||
getConnectionForVoiceChannel(channel: VoiceBasedChannel): VoiceConnection | undefined {
|
||||
return this.connections.get(channel);
|
||||
}
|
||||
|
||||
async joinChannel(channel: VoiceBasedChannel): Promise<void> {
|
||||
if (this.isInVoiceChannel(channel)) return;
|
||||
const connection = joinVoiceChannel({
|
||||
channelId: channel.id,
|
||||
guildId: channel.guild.id,
|
||||
adapterCreator: createAdapter(channel),
|
||||
});
|
||||
connection.subscribe(this.player);
|
||||
this.joinedChannels.push(channel);
|
||||
this.connections.set(channel, connection);
|
||||
}
|
||||
|
||||
async leaveChannel(channel: VoiceBasedChannel): Promise<void> {
|
||||
if (!this.isInVoiceChannel(channel)) return;
|
||||
const conn = this.connections.get(channel);
|
||||
this.joinedChannels = this.joinedChannels.filter((c) => c !== channel);
|
||||
conn?.disconnect();
|
||||
this.connections.delete(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an audio file for `text` using the given engine+voice, caching by
|
||||
* SHA1(voice + text). Returns the path of the generated file.
|
||||
*/
|
||||
async generateVoice(
|
||||
text: string,
|
||||
engine: BaseEngine,
|
||||
voice: string,
|
||||
params: VoiceParams = {},
|
||||
): Promise<string> {
|
||||
const hash = createHash("sha1").update(voice + text).digest("hex");
|
||||
const filepath = join(this.opts.voiceTmpPath, `${hash}.${engine.fileExtension}`);
|
||||
if (!existsSync(filepath)) {
|
||||
await engine.getSpeechFile(text, filepath, voice, params);
|
||||
}
|
||||
return filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synthesizes `message` with the given engine/voice (defaults to the
|
||||
* configured announcement engine/voice) and queues it for playback.
|
||||
*/
|
||||
async speak(
|
||||
_channel: VoiceBasedChannel,
|
||||
message: string,
|
||||
engine: BaseEngine = this.opts.tts.announcement,
|
||||
voice: string = this.opts.tts.announcementVoice,
|
||||
params: VoiceParams = {},
|
||||
): Promise<void> {
|
||||
const filepath = await this.generateVoice(message, engine, voice, params);
|
||||
this.queue.add(filepath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the author is in voice, speaks the response and adds a sysmsg ping;
|
||||
* otherwise replies to the message.
|
||||
*/
|
||||
export function respond(
|
||||
audio: AudioService,
|
||||
sysmsgPath: string,
|
||||
message: Message,
|
||||
text: string,
|
||||
voiceText?: string,
|
||||
): void {
|
||||
const displayName = message.member?.displayName ?? message.author.username;
|
||||
const toSend = `${displayName}, ${voiceText ?? text}`;
|
||||
const voiceChannel = message.member?.voice.channel;
|
||||
if (voiceChannel) {
|
||||
audio.queue.add(sysmsgPath);
|
||||
void audio.speak(voiceChannel, toSend);
|
||||
} else {
|
||||
void message.reply(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { Message } from "discord.js";
|
||||
|
||||
export type CommandHandler = (args: string[], message: Message) => void | Promise<void>;
|
||||
|
||||
export class CommandRegistry {
|
||||
private handlers = new Map<string, CommandHandler>();
|
||||
|
||||
constructor(private readonly prefix: string) {}
|
||||
|
||||
register(name: string, handler: CommandHandler): void {
|
||||
this.handlers.set(name, handler);
|
||||
}
|
||||
|
||||
async handleMessage(message: Message): Promise<void> {
|
||||
if (!message.content.startsWith(this.prefix)) return;
|
||||
const args = message.content.split(" ");
|
||||
const head = args[0];
|
||||
if (!head) return;
|
||||
const command = head.slice(this.prefix.length);
|
||||
const handler = this.handlers.get(command);
|
||||
if (!handler) return;
|
||||
try {
|
||||
await handler(args, message);
|
||||
} catch (err) {
|
||||
console.error(`Command "${command}" failed:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import "dotenv/config";
|
||||
import { z } from "zod";
|
||||
|
||||
const schema = z.object({
|
||||
TOKEN: z.string().min(1),
|
||||
GUILD: z.string().min(1),
|
||||
CHANNEL: z.string().min(1),
|
||||
STRING_SET: z.string().default("en"),
|
||||
VOICE_TMP_PATH: z.string().min(1),
|
||||
DB_FILE: z.string().min(1),
|
||||
PREFIX: z.string().min(1).default("+"),
|
||||
ANNOUNCEMENT_ENGINE: z.string().min(1),
|
||||
ANNOUNCEMENT_VOICE: z.string().min(1),
|
||||
TTS_CHANNEL: z.string().optional(),
|
||||
|
||||
// Optional per-provider credentials
|
||||
AZURE_API_KEY: z.string().optional(),
|
||||
AZURE_REGION: z.string().optional(),
|
||||
AZURE_LIST_ENDPOINT: z.string().optional(),
|
||||
XI_API_KEY: z.string().optional(),
|
||||
watsonURL: z.string().optional(),
|
||||
watsonAPIKey: z.string().optional(),
|
||||
GOOGLE_APPLICATION_CREDENTIALS: z.string().optional(),
|
||||
OPENAI_API_KEY: z.string().optional(),
|
||||
OPENAI_CHAT_MODEL: z.string().default("gpt-4o"),
|
||||
UNREAL_API_KEY: z.string().optional(),
|
||||
|
||||
// Module-specific
|
||||
MANGLE_LANGS: z.string().optional(),
|
||||
QDB_URL: z.string().optional(),
|
||||
QDB_USER: z.string().optional(),
|
||||
QDB_PASS: z.string().optional(),
|
||||
});
|
||||
|
||||
export type Config = z.infer<typeof schema>;
|
||||
|
||||
export const config: Config = (() => {
|
||||
const parsed = schema.safeParse(process.env);
|
||||
if (!parsed.success) {
|
||||
console.error("Invalid configuration:");
|
||||
for (const issue of parsed.error.issues) {
|
||||
console.error(` ${issue.path.join(".")}: ${issue.message}`);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
return parsed.data;
|
||||
})();
|
||||
@@ -0,0 +1,24 @@
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import type { Client } from "discord.js";
|
||||
import type { Config } from "./config.js";
|
||||
import type { AudioService } from "./audio/AudioService.js";
|
||||
import type { CommandRegistry } from "./commands/CommandRegistry.js";
|
||||
import type { TTSRegistry } from "./tts/registry.js";
|
||||
import type { AppDatabase } from "./db/db.js";
|
||||
import type { TFn, Strings } from "./i18n/strings.js";
|
||||
|
||||
export interface BotContext {
|
||||
client: Client;
|
||||
config: Config;
|
||||
audio: AudioService;
|
||||
commands: CommandRegistry;
|
||||
tts: TTSRegistry;
|
||||
db: AppDatabase;
|
||||
strings: Strings;
|
||||
t: TFn;
|
||||
/** Absolute path to the repo root (used for sysmsg.wav, sysstart.wav lookup). */
|
||||
rootDir: string;
|
||||
}
|
||||
|
||||
export const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
@@ -0,0 +1,8 @@
|
||||
import sqlite3 from "sqlite3";
|
||||
import { open, type Database } from "sqlite";
|
||||
|
||||
export type AppDatabase = Database<sqlite3.Database, sqlite3.Statement>;
|
||||
|
||||
export async function openDatabase(filename: string): Promise<AppDatabase> {
|
||||
return open({ filename, driver: sqlite3.Database });
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
export interface TTSPreferencesRow {
|
||||
user_id: string;
|
||||
engine: string;
|
||||
voice: string;
|
||||
}
|
||||
|
||||
export interface WBWStoryRow {
|
||||
story_id: number;
|
||||
story_text: string;
|
||||
}
|
||||
|
||||
export interface BotStateRow {
|
||||
key: string;
|
||||
value: string;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import printf from "printf";
|
||||
|
||||
export type StringKey =
|
||||
| "WELCOME"
|
||||
| "USER_JOINED"
|
||||
| "USER_LEFT"
|
||||
| "SYSTEM_VOICE_CHANGED"
|
||||
| "USER_VOICE_CHANGED"
|
||||
| "INVALID_ENGINE"
|
||||
| "INVALID_VOICE"
|
||||
| "TOO_MANY_ARGUMENTS"
|
||||
| "CURRENT_STORY"
|
||||
| "NO_STORY"
|
||||
| "WBW_REPLACED"
|
||||
| "WBW_TOO_DIFFERENT"
|
||||
| "WBW_NEW_WORD"
|
||||
| "WBW_RESET"
|
||||
| "WBW_INVALID_ID";
|
||||
|
||||
export type Strings = Record<StringKey, string>;
|
||||
|
||||
const stringsDir = resolve(dirname(fileURLToPath(import.meta.url)), "../../strings");
|
||||
|
||||
function loadStringSet(name: string): Partial<Strings> {
|
||||
const filepath = resolve(stringsDir, `${name}.json`);
|
||||
return JSON.parse(readFileSync(filepath, "utf8")) as Partial<Strings>;
|
||||
}
|
||||
|
||||
export function loadStrings(stringSet: string): Strings {
|
||||
const fallback = loadStringSet("en") as Strings;
|
||||
if (stringSet === "en") return fallback;
|
||||
const localized = loadStringSet(stringSet);
|
||||
return { ...fallback, ...localized };
|
||||
}
|
||||
|
||||
export function makeT(strings: Strings) {
|
||||
return (key: StringKey, ...args: unknown[]): string => printf(strings[key], ...args);
|
||||
}
|
||||
|
||||
export type TFn = ReturnType<typeof makeT>;
|
||||
@@ -0,0 +1,57 @@
|
||||
import { Client, Events, GatewayIntentBits } from "discord.js";
|
||||
import { config } from "./config.js";
|
||||
|
||||
process.on("unhandledRejection", (reason) => {
|
||||
console.error("Unhandled promise rejection:", reason);
|
||||
});
|
||||
process.on("uncaughtException", (err) => {
|
||||
console.error("Uncaught exception:", err);
|
||||
});
|
||||
import { openDatabase } from "./db/db.js";
|
||||
import { loadStrings, makeT } from "./i18n/strings.js";
|
||||
import { TTSRegistry } from "./tts/registry.js";
|
||||
import { AudioService } from "./audio/AudioService.js";
|
||||
import { CommandRegistry } from "./commands/CommandRegistry.js";
|
||||
import { registerModules } from "./modules/registry.js";
|
||||
import { repoRoot, type BotContext } from "./context.js";
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
GatewayIntentBits.GuildMembers,
|
||||
GatewayIntentBits.GuildMessageReactions,
|
||||
GatewayIntentBits.GuildMessages,
|
||||
GatewayIntentBits.GuildPresences,
|
||||
GatewayIntentBits.GuildVoiceStates,
|
||||
GatewayIntentBits.Guilds,
|
||||
GatewayIntentBits.MessageContent,
|
||||
],
|
||||
});
|
||||
|
||||
client.on(Events.Error, (err) => console.error("Discord client error:", err));
|
||||
client.on(Events.Warn, (msg) => console.warn("Discord client warning:", msg));
|
||||
|
||||
const db = await openDatabase(config.DB_FILE);
|
||||
const tts = new TTSRegistry(config.ANNOUNCEMENT_ENGINE, config.ANNOUNCEMENT_VOICE);
|
||||
const audio = new AudioService({ voiceTmpPath: config.VOICE_TMP_PATH, tts });
|
||||
const commands = new CommandRegistry(config.PREFIX);
|
||||
const strings = loadStrings(config.STRING_SET);
|
||||
|
||||
const ctx: BotContext = {
|
||||
client,
|
||||
config,
|
||||
audio,
|
||||
commands,
|
||||
tts,
|
||||
db,
|
||||
strings,
|
||||
t: makeT(strings),
|
||||
rootDir: repoRoot,
|
||||
};
|
||||
|
||||
await registerModules(ctx);
|
||||
|
||||
client.on(Events.MessageCreate, (message) => {
|
||||
void commands.handleMessage(message);
|
||||
});
|
||||
|
||||
await client.login(config.TOKEN);
|
||||
@@ -0,0 +1,26 @@
|
||||
import { join } from "node:path";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
export const announcer: Module = ({ client, audio, tts, t, rootDir }) => {
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
|
||||
client.on("voiceStateUpdate", async (oldState, newState) => {
|
||||
if (newState.member?.user.bot) return;
|
||||
if (oldState.channel && newState.channel) return;
|
||||
const channel = oldState.channel ?? newState.channel;
|
||||
if (!channel) return;
|
||||
|
||||
if (channel.members.size < 2) {
|
||||
audio.queue.flush();
|
||||
await audio.leaveChannel(channel);
|
||||
}
|
||||
await audio.joinChannel(channel);
|
||||
|
||||
const joined = !oldState.channel;
|
||||
const username = newState.member?.displayName ?? oldState.member?.displayName ?? "someone";
|
||||
const str = joined ? t("USER_JOINED", username) : t("USER_LEFT", username);
|
||||
const filepath = await audio.generateVoice(str, tts.announcement, tts.announcementVoice);
|
||||
audio.queue.add(sysmsg);
|
||||
audio.queue.add(filepath);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,78 @@
|
||||
import { readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { respond } from "../audio/AudioService.js";
|
||||
import type { TTSPreferencesRow } from "../db/schema.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
export const canttalk: Module = ({ client, audio, commands, tts, db, t, config, rootDir }) => {
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
|
||||
client.on("messageCreate", async (message) => {
|
||||
if (message.author.bot) return;
|
||||
if (message.content.startsWith(config.PREFIX)) return;
|
||||
if (!config.TTS_CHANNEL || message.channel.id !== config.TTS_CHANNEL) return;
|
||||
const voiceChannel = message.member?.voice.channel;
|
||||
if (!voiceChannel) return;
|
||||
|
||||
let userRow = await db.get<TTSPreferencesRow>(
|
||||
"select * from TTSPreferences where user_id=?",
|
||||
message.author.id,
|
||||
);
|
||||
if (!userRow) {
|
||||
await db.run("insert into TTSPreferences (user_id,engine,voice) values (?,?,?)", [
|
||||
message.author.id,
|
||||
tts.announcement.shortName,
|
||||
tts.announcementVoice,
|
||||
]);
|
||||
userRow = await db.get<TTSPreferencesRow>(
|
||||
"select * from TTSPreferences where user_id=?",
|
||||
message.author.id,
|
||||
);
|
||||
}
|
||||
if (!userRow) return;
|
||||
|
||||
const engine = tts.get(userRow.engine);
|
||||
if (engine) {
|
||||
await audio.speak(voiceChannel, message.content, engine, userRow.voice);
|
||||
}
|
||||
});
|
||||
|
||||
commands.register("myvoice", async (args, message) => {
|
||||
if (args.length > 3) {
|
||||
return respond(audio, sysmsg, message, t("TOO_MANY_ARGUMENTS"));
|
||||
}
|
||||
const engineName = args[1];
|
||||
const voiceArg = args[2];
|
||||
if (!engineName || !voiceArg) {
|
||||
return respond(audio, sysmsg, message, t("TOO_MANY_ARGUMENTS"));
|
||||
}
|
||||
const engine = tts.get(engineName);
|
||||
if (!engine) {
|
||||
return respond(audio, sysmsg, message, t("INVALID_ENGINE", engineName));
|
||||
}
|
||||
const userVoice = voiceArg.toLowerCase();
|
||||
let chosenVoice: string;
|
||||
if (engine.validateVoice(userVoice)) {
|
||||
chosenVoice = userVoice;
|
||||
respond(audio, sysmsg, message, t("USER_VOICE_CHANGED", chosenVoice, engine.longName));
|
||||
} else {
|
||||
chosenVoice = engine.getDefaultVoice();
|
||||
respond(audio, sysmsg, message, t("INVALID_VOICE", chosenVoice, engine.longName));
|
||||
}
|
||||
await db.run("update TTSPreferences set engine=?, voice=? where user_id=?", [
|
||||
engineName,
|
||||
chosenVoice,
|
||||
message.author.id,
|
||||
]);
|
||||
});
|
||||
|
||||
commands.register("random", async (_args, _message) => {
|
||||
const tmpPath = config.VOICE_TMP_PATH;
|
||||
const files = readdirSync(tmpPath);
|
||||
if (files.length === 0) return;
|
||||
const rnd = files[Math.floor(Math.random() * files.length)];
|
||||
if (!rnd) return;
|
||||
audio.queue.add(sysmsg);
|
||||
audio.queue.add(join(tmpPath, rnd));
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
import { join } from "node:path";
|
||||
import OpenAI from "openai";
|
||||
import { respond } from "../audio/AudioService.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
export const chatgptModule: Module = ({ audio, commands, config, rootDir }) => {
|
||||
if (!config.OPENAI_API_KEY) {
|
||||
console.warn("chatgpt module: OPENAI_API_KEY not set — `chat` command disabled");
|
||||
return;
|
||||
}
|
||||
const client = new OpenAI({ apiKey: config.OPENAI_API_KEY });
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
const model = config.OPENAI_CHAT_MODEL;
|
||||
|
||||
commands.register("chat", async (_args, message) => {
|
||||
const prompt = message.content.slice(config.PREFIX.length + "chat".length).trim();
|
||||
if (!prompt) return;
|
||||
const completion = await client.chat.completions.create({
|
||||
model,
|
||||
messages: [{ role: "user", content: prompt }],
|
||||
});
|
||||
const text = completion.choices[0]?.message.content?.trim() ?? "";
|
||||
if (text) respond(audio, sysmsg, message, text);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
import { join } from "node:path";
|
||||
import { respond } from "../audio/AudioService.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
/**
|
||||
* Calls Google Translate's unofficial endpoint. No SDK — the maintained
|
||||
* `node-google-translate-skidz` wrapper was abandoned, so we hit the same
|
||||
* single_translate endpoint directly with a fetch.
|
||||
*/
|
||||
async function translate(text: string, fromLang: string, toLang: string): Promise<string> {
|
||||
const url = new URL("https://translate.googleapis.com/translate_a/single");
|
||||
url.searchParams.set("client", "gtx");
|
||||
url.searchParams.set("sl", fromLang);
|
||||
url.searchParams.set("tl", toLang);
|
||||
url.searchParams.set("dt", "t");
|
||||
url.searchParams.set("q", text);
|
||||
const res = await fetch(url.toString());
|
||||
if (!res.ok) throw new Error(`translate ${fromLang}->${toLang} failed: ${res.status}`);
|
||||
const json = (await res.json()) as unknown[];
|
||||
const segments = json[0];
|
||||
if (!Array.isArray(segments)) throw new Error("translate: unexpected response shape");
|
||||
return segments
|
||||
.map((seg) => (Array.isArray(seg) && typeof seg[0] === "string" ? seg[0] : ""))
|
||||
.join("");
|
||||
}
|
||||
|
||||
export const mangle: Module = ({ audio, commands, config, rootDir }) => {
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
const langs = (config.MANGLE_LANGS ?? "en,de,ja,ru,en").split(",").map((l) => l.trim());
|
||||
|
||||
commands.register("mangle", async (_args, message) => {
|
||||
let str = message.content.slice(config.PREFIX.length + "mangle".length).trim();
|
||||
if (!str) return;
|
||||
for (let i = 0; i < langs.length - 1; i++) {
|
||||
const from = langs[i];
|
||||
const to = langs[i + 1];
|
||||
if (!from || !to) break;
|
||||
str = await translate(str, from, to);
|
||||
}
|
||||
respond(audio, sysmsg, message, str);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
import { join } from "node:path";
|
||||
import { respond } from "../audio/AudioService.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
interface Quote {
|
||||
author: string;
|
||||
medium: string;
|
||||
quote: string;
|
||||
}
|
||||
|
||||
export const quotedb: Module = ({ audio, commands, config, rootDir }) => {
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
|
||||
commands.register("randomquote", async (_args, message) => {
|
||||
if (!config.QDB_URL || !config.QDB_USER || !config.QDB_PASS) {
|
||||
console.warn("quotedb: QDB_URL/QDB_USER/QDB_PASS not configured");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const auth = Buffer.from(`${config.QDB_USER}:${config.QDB_PASS}`).toString("base64");
|
||||
const res = await fetch(config.QDB_URL, {
|
||||
headers: { Authorization: `Basic ${auth}` },
|
||||
});
|
||||
const quotes = (await res.json()) as Quote[];
|
||||
if (quotes.length === 0) return;
|
||||
const quote = quotes[Math.floor(Math.random() * quotes.length)];
|
||||
if (!quote) return;
|
||||
respond(
|
||||
audio,
|
||||
sysmsg,
|
||||
message,
|
||||
`Here's your quote: ${quote.author}, on ${quote.medium}: ${quote.quote}`,
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("randomquote failed:", err);
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { BotContext } from "../context.js";
|
||||
import { announcer } from "./announcer.js";
|
||||
import { canttalk } from "./canttalk.js";
|
||||
import { chatgptModule } from "./chatgpt.js";
|
||||
import { mangle } from "./mangle.js";
|
||||
import { quotedb } from "./quotedb.js";
|
||||
import { summon } from "./summon.js";
|
||||
import { ttsSettings } from "./ttsSettings.js";
|
||||
import { welcomer } from "./welcomer.js";
|
||||
import { wordbyword } from "./wordbyword.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
const modules: Record<string, Module> = {
|
||||
announcer,
|
||||
canttalk,
|
||||
chatgpt: chatgptModule,
|
||||
mangle,
|
||||
quotedb,
|
||||
summon,
|
||||
ttsSettings,
|
||||
welcomer,
|
||||
wordbyword,
|
||||
};
|
||||
|
||||
export async function registerModules(ctx: BotContext): Promise<void> {
|
||||
for (const [name, mod] of Object.entries(modules)) {
|
||||
console.log(`Loading module: ${name}`);
|
||||
await mod(ctx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { join } from "node:path";
|
||||
import { respond } from "../audio/AudioService.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
export const summon: Module = ({ audio, commands, rootDir }) => {
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
|
||||
commands.register("summon", async (_args, message) => {
|
||||
const channel = message.member?.voice.channel;
|
||||
if (!channel) return;
|
||||
await audio.joinChannel(channel);
|
||||
respond(audio, sysmsg, message, "Hi!");
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import { join } from "node:path";
|
||||
import { respond } from "../audio/AudioService.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
export const ttsSettings: Module = ({ audio, commands, tts, t, rootDir }) => {
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
|
||||
commands.register("announcevoice", (args, message) => {
|
||||
if (args.length > 3) {
|
||||
respond(audio, sysmsg, message, t("TOO_MANY_ARGUMENTS"));
|
||||
return;
|
||||
}
|
||||
const engineName = args[1];
|
||||
const voiceArg = args[2];
|
||||
if (!engineName || !voiceArg) {
|
||||
respond(audio, sysmsg, message, t("TOO_MANY_ARGUMENTS"));
|
||||
return;
|
||||
}
|
||||
const engine = tts.get(engineName);
|
||||
if (!engine) {
|
||||
respond(audio, sysmsg, message, t("INVALID_ENGINE", engineName));
|
||||
return;
|
||||
}
|
||||
tts.announcement = engine;
|
||||
if (engine.validateVoice(voiceArg)) {
|
||||
tts.announcementVoice = voiceArg;
|
||||
respond(audio, sysmsg, message, t("SYSTEM_VOICE_CHANGED", voiceArg, engine.longName));
|
||||
} else {
|
||||
tts.announcementVoice = engine.getDefaultVoice();
|
||||
respond(
|
||||
audio,
|
||||
sysmsg,
|
||||
message,
|
||||
t("INVALID_VOICE", tts.announcementVoice, engine.longName),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
commands.register("flush", () => audio.queue.flush());
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
import type { BotContext } from "../context.js";
|
||||
|
||||
export type Module = (ctx: BotContext) => void | Promise<void>;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { join } from "node:path";
|
||||
import { Events, type VoiceBasedChannel } from "discord.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
export const welcomer: Module = ({ client, audio, config, strings, rootDir }) => {
|
||||
const sysstart = join(rootDir, "sysstart.wav");
|
||||
|
||||
client.once(Events.ClientReady, async () => {
|
||||
console.log("Bot initialized and listening");
|
||||
await client.guilds.fetch(config.GUILD);
|
||||
const channel = await client.channels.fetch(config.CHANNEL);
|
||||
if (!channel?.isVoiceBased()) {
|
||||
console.warn(`Channel ${config.CHANNEL} is not a voice channel; skipping welcome`);
|
||||
return;
|
||||
}
|
||||
const voiceChannel = channel as VoiceBasedChannel;
|
||||
await audio.joinChannel(voiceChannel);
|
||||
audio.queue.add(sysstart);
|
||||
await audio.speak(voiceChannel, strings.WELCOME);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
import { join } from "node:path";
|
||||
import levenshtein from "fast-levenshtein";
|
||||
import { respond } from "../audio/AudioService.js";
|
||||
|
||||
const isStringInt = (s: string): boolean => /^-?\d+$/.test(s);
|
||||
|
||||
import type { BotStateRow, WBWStoryRow } from "../db/schema.js";
|
||||
import type { Module } from "./types.js";
|
||||
|
||||
export const wordbyword: Module = ({ audio, commands, db, t, rootDir }) => {
|
||||
const sysmsg = join(rootDir, "sysmsg.wav");
|
||||
let currentWBW = "";
|
||||
|
||||
commands.register("wbw", async (args, message) => {
|
||||
if (args.length === 1) {
|
||||
return respond(
|
||||
audio,
|
||||
sysmsg,
|
||||
message,
|
||||
currentWBW ? t("CURRENT_STORY", currentWBW) : t("NO_STORY"),
|
||||
);
|
||||
}
|
||||
if (args.length > 2) {
|
||||
return respond(audio, sysmsg, message, t("TOO_MANY_ARGUMENTS"));
|
||||
}
|
||||
const arg1 = args[1];
|
||||
if (arg1 === undefined) return;
|
||||
|
||||
if (isStringInt(arg1)) {
|
||||
const story = await db.get<WBWStoryRow>(
|
||||
"select * from WBWStories where story_id=?",
|
||||
parseInt(arg1, 10),
|
||||
);
|
||||
if (!story) return respond(audio, sysmsg, message, t("WBW_INVALID_ID"));
|
||||
return respond(audio, sysmsg, message, story.story_text);
|
||||
}
|
||||
|
||||
const lastUser = await db.get<BotStateRow>(
|
||||
"select value from BotState where key='last_wbw'",
|
||||
);
|
||||
if (lastUser && message.author.id === lastUser.value && currentWBW !== "") {
|
||||
const lastWord =
|
||||
currentWBW.indexOf(" ") === currentWBW.lastIndexOf(" ")
|
||||
? currentWBW.trim()
|
||||
: currentWBW.slice(currentWBW.slice(0, -1).lastIndexOf(" ") + 1).trim();
|
||||
if (levenshtein.get(arg1, lastWord) <= 3) {
|
||||
currentWBW = currentWBW.replace(
|
||||
new RegExp(`${escapeRegExp(lastWord)}([^${escapeRegExp(lastWord)}]*)$`),
|
||||
`${arg1}$1 `,
|
||||
);
|
||||
respond(audio, sysmsg, message, t("WBW_REPLACED", lastWord, arg1));
|
||||
} else {
|
||||
return respond(audio, sysmsg, message, t("WBW_TOO_DIFFERENT"));
|
||||
}
|
||||
} else {
|
||||
currentWBW += `${arg1} `;
|
||||
respond(audio, sysmsg, message, t("WBW_NEW_WORD"));
|
||||
const toSay =
|
||||
currentWBW.indexOf(".") === -1 ? currentWBW : currentWBW.slice(currentWBW.lastIndexOf(".") + 2);
|
||||
const voiceChannel = message.member?.voice.channel;
|
||||
if (voiceChannel) await audio.speak(voiceChannel, toSay);
|
||||
await db.run("update BotState set value=? where key='last_wbw'", [message.author.id]);
|
||||
}
|
||||
});
|
||||
|
||||
commands.register("newwbw", async (_args, message) => {
|
||||
await db.run("insert into WBWStories (story_text) values(?)", [currentWBW]);
|
||||
currentWBW = "";
|
||||
respond(audio, sysmsg, message, t("WBW_RESET"));
|
||||
});
|
||||
};
|
||||
|
||||
function escapeRegExp(s: string): string {
|
||||
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
|
||||
export type VoiceParams = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Common contract for every TTS provider. Subclasses override either
|
||||
* `getSpeech` (returning a fetch-like Response) or `getSpeechFile` (writing
|
||||
* directly to disk) — the default `getSpeechFile` pipes `getSpeech` to a file.
|
||||
*/
|
||||
export abstract class BaseEngine {
|
||||
/** Short ID used in env / commands (e.g. "azure"). */
|
||||
readonly shortName: string;
|
||||
/** Human-readable name shown in messages. */
|
||||
readonly longName: string;
|
||||
/** Output file extension without the dot (e.g. "mp3"). */
|
||||
readonly fileExtension: string;
|
||||
|
||||
protected voices: Record<string, string | { name: string; lang: string }> = {};
|
||||
|
||||
constructor(shortName: string, longName: string, fileExtension: string) {
|
||||
this.shortName = shortName;
|
||||
this.longName = longName;
|
||||
this.fileExtension = fileExtension;
|
||||
}
|
||||
|
||||
/** Maps a user-friendly voice name to the provider's internal identifier. */
|
||||
getInternalVoiceName(str: string): string {
|
||||
const v = this.voices[str];
|
||||
if (v == null) return str;
|
||||
return typeof v === "string" ? v : v.name;
|
||||
}
|
||||
|
||||
abstract getDefaultVoice(): string;
|
||||
|
||||
validateVoice(voice: string): boolean {
|
||||
if (Object.keys(this.voices).length === 0) return true;
|
||||
return this.voices[voice] != null;
|
||||
}
|
||||
|
||||
/** Default implementation: subclass should override either this or getSpeechFile. */
|
||||
async getSpeech(_text: string, _voice?: string, _params?: VoiceParams): Promise<Response> {
|
||||
throw new Error(`${this.shortName}: getSpeech not implemented`);
|
||||
}
|
||||
|
||||
async getSpeechFile(
|
||||
text: string,
|
||||
filepath: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
params: VoiceParams = {},
|
||||
): Promise<string> {
|
||||
const data = await this.getSpeech(text, voice, params);
|
||||
const buf = Buffer.from(await data.arrayBuffer());
|
||||
await writeFile(filepath, buf);
|
||||
return filepath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import * as sdk from "microsoft-cognitiveservices-speech-sdk";
|
||||
import { config } from "../config.js";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
interface AzureVoiceMeta {
|
||||
DisplayName: string;
|
||||
ShortName: string;
|
||||
Name: string;
|
||||
}
|
||||
|
||||
export class AzureEngine extends BaseEngine {
|
||||
protected override voices: Record<string, string> = {};
|
||||
|
||||
constructor() {
|
||||
super("azure", "Microsoft Azure TTS", "wav");
|
||||
void this.populateVoiceList();
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "aria";
|
||||
}
|
||||
|
||||
override getSpeechFile(
|
||||
text: string,
|
||||
filepath: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!config.AZURE_API_KEY || !config.AZURE_REGION) {
|
||||
reject(new Error("AZURE_API_KEY and AZURE_REGION must be set"));
|
||||
return;
|
||||
}
|
||||
const speechConfig = sdk.SpeechConfig.fromSubscription(
|
||||
config.AZURE_API_KEY,
|
||||
config.AZURE_REGION,
|
||||
);
|
||||
speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm;
|
||||
const internal = this.voices[voice] ?? this.voices[this.getDefaultVoice()];
|
||||
if (internal) speechConfig.speechSynthesisVoiceName = internal;
|
||||
const audioConfig = sdk.AudioConfig.fromAudioFileOutput(filepath);
|
||||
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
|
||||
synthesizer.speakTextAsync(
|
||||
text,
|
||||
(result) => {
|
||||
synthesizer.close();
|
||||
if (result) resolve(filepath);
|
||||
else reject(new Error("Azure TTS returned no result"));
|
||||
},
|
||||
(error: unknown) => {
|
||||
synthesizer.close();
|
||||
reject(error instanceof Error ? error : new Error(String(error)));
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private async populateVoiceList(): Promise<void> {
|
||||
if (!config.AZURE_LIST_ENDPOINT || !config.AZURE_API_KEY) return;
|
||||
try {
|
||||
const res = await fetch(config.AZURE_LIST_ENDPOINT, {
|
||||
headers: { "Ocp-Apim-Subscription-Key": config.AZURE_API_KEY },
|
||||
});
|
||||
const json = (await res.json()) as AzureVoiceMeta[];
|
||||
for (const voice of json) {
|
||||
const key = voice.DisplayName.toLowerCase();
|
||||
if (this.voices[key]) {
|
||||
if (voice.Name.includes("Neural")) this.voices[key] = voice.ShortName;
|
||||
} else {
|
||||
this.voices[key] = voice.ShortName;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Azure: failed to populate voice list:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { config } from "../config.js";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
interface ElevenVoice {
|
||||
name: string;
|
||||
voice_id: string;
|
||||
}
|
||||
|
||||
interface ElevenVoicesResponse {
|
||||
voices: ElevenVoice[];
|
||||
}
|
||||
|
||||
export class ElevenEngine extends BaseEngine {
|
||||
protected override voices: Record<string, string> = {};
|
||||
|
||||
constructor() {
|
||||
super("eleven", "Eleven Labs TTS", "mp3");
|
||||
void this.populateVoiceList();
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "guillem";
|
||||
}
|
||||
|
||||
private async populateVoiceList(): Promise<void> {
|
||||
if (!config.XI_API_KEY) return;
|
||||
try {
|
||||
const res = await fetch("https://api.elevenlabs.io/v1/voices", {
|
||||
method: "GET",
|
||||
headers: { "xi-api-key": config.XI_API_KEY },
|
||||
});
|
||||
const json = (await res.json()) as ElevenVoicesResponse;
|
||||
for (const v of json.voices) {
|
||||
this.voices[v.name.toLowerCase()] = v.voice_id;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Eleven: failed to populate voice list:", err);
|
||||
}
|
||||
}
|
||||
|
||||
override async getSpeech(
|
||||
text: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<Response> {
|
||||
if (!config.XI_API_KEY) {
|
||||
throw new Error("XI_API_KEY must be set");
|
||||
}
|
||||
const voiceId = this.getInternalVoiceName(voice);
|
||||
const url = `https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`;
|
||||
return fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"xi-api-key": config.XI_API_KEY,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model_id: "eleven_multilingual_v2",
|
||||
text,
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
export class EspeakEngine extends BaseEngine {
|
||||
constructor() {
|
||||
super("espeak", "ESpeak", "wav");
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "en";
|
||||
}
|
||||
|
||||
override validateVoice(_voice: string): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
override async getSpeechFile(
|
||||
text: string,
|
||||
filepath: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const proc = spawn("espeak", ["-v", voice, "-w", filepath, "--stdin"]);
|
||||
proc.on("error", reject);
|
||||
proc.on("close", (code) => {
|
||||
if (code === 0) resolve(filepath);
|
||||
else reject(new Error(`espeak exited with code ${code}`));
|
||||
});
|
||||
proc.stdin.end(text);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import textToSpeech from "@google-cloud/text-to-speech";
|
||||
import type { TextToSpeechClient } from "@google-cloud/text-to-speech";
|
||||
import { config } from "../config.js";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
interface GoogleVoiceMeta {
|
||||
name: string;
|
||||
lang: string;
|
||||
}
|
||||
|
||||
export class GoogleEngine extends BaseEngine {
|
||||
private client: TextToSpeechClient | undefined;
|
||||
protected override voices: Record<string, GoogleVoiceMeta> = {};
|
||||
|
||||
constructor() {
|
||||
super("google", "Google Cloud TTS", "wav");
|
||||
void this.populateVoiceList();
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "en-us-wavenet-a";
|
||||
}
|
||||
|
||||
private credentialsAvailable(): boolean {
|
||||
return (
|
||||
!!config.GOOGLE_APPLICATION_CREDENTIALS &&
|
||||
existsSync(config.GOOGLE_APPLICATION_CREDENTIALS)
|
||||
);
|
||||
}
|
||||
|
||||
private getClient(): TextToSpeechClient {
|
||||
if (!this.credentialsAvailable()) {
|
||||
throw new Error(
|
||||
"Google Cloud TTS unavailable: GOOGLE_APPLICATION_CREDENTIALS must point to a readable file",
|
||||
);
|
||||
}
|
||||
this.client ??= new textToSpeech.TextToSpeechClient();
|
||||
return this.client;
|
||||
}
|
||||
|
||||
private async populateVoiceList(): Promise<void> {
|
||||
if (!this.credentialsAvailable()) return;
|
||||
try {
|
||||
const [result] = await this.getClient().listVoices({});
|
||||
for (const voice of result.voices ?? []) {
|
||||
if (!voice.name || !voice.languageCodes?.[0]) continue;
|
||||
this.voices[voice.name.toLowerCase()] = {
|
||||
name: voice.name,
|
||||
lang: voice.languageCodes[0],
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Google Cloud TTS: failed to populate voice list:", err);
|
||||
}
|
||||
}
|
||||
|
||||
override async getSpeechFile(
|
||||
text: string,
|
||||
filepath: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<string> {
|
||||
const meta = this.voices[voice];
|
||||
if (!meta) throw new Error(`Google: unknown voice "${voice}"`);
|
||||
const [response] = await this.getClient().synthesizeSpeech({
|
||||
input: { text },
|
||||
voice: { name: meta.name, languageCode: meta.lang },
|
||||
audioConfig: { audioEncoding: "LINEAR16" },
|
||||
});
|
||||
if (!response.audioContent) {
|
||||
throw new Error("Google: synthesizeSpeech returned no audioContent");
|
||||
}
|
||||
await writeFile(filepath, response.audioContent);
|
||||
return filepath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { getAudioUrl } from "@sefinek/google-tts-api";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
export class GtranslateEngine extends BaseEngine {
|
||||
constructor() {
|
||||
super("gtranslate", "Google Translate TTS", "mp3");
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "en-us";
|
||||
}
|
||||
|
||||
override validateVoice(_voice: string): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
override async getSpeech(
|
||||
text: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<Response> {
|
||||
const url = getAudioUrl(text, { lang: voice });
|
||||
return fetch(url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { config } from "../config.js";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
const OPENAI_VOICES = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"] as const;
|
||||
|
||||
export class OpenAIEngine extends BaseEngine {
|
||||
protected override voices: Record<string, string> = Object.fromEntries(
|
||||
OPENAI_VOICES.map((v) => [v, v]),
|
||||
);
|
||||
|
||||
constructor() {
|
||||
super("openai", "OpenAI TTS", "mp3");
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "alloy";
|
||||
}
|
||||
|
||||
override async getSpeech(
|
||||
text: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<Response> {
|
||||
if (!config.OPENAI_API_KEY) {
|
||||
throw new Error("OPENAI_API_KEY must be set");
|
||||
}
|
||||
return fetch("https://api.openai.com/v1/audio/speech", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${config.OPENAI_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "tts-1-hd",
|
||||
input: text,
|
||||
voice,
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import type { BaseEngine } from "./BaseEngine.js";
|
||||
import { AzureEngine } from "./azure.js";
|
||||
import { ElevenEngine } from "./eleven.js";
|
||||
import { EspeakEngine } from "./espeak.js";
|
||||
import { GoogleEngine } from "./google.js";
|
||||
import { GtranslateEngine } from "./gtranslate.js";
|
||||
import { OpenAIEngine } from "./openai.js";
|
||||
import { SamEngine } from "./sam.js";
|
||||
import { UnrealEngine } from "./unreal.js";
|
||||
import { WatsonEngine } from "./watson.js";
|
||||
|
||||
export class TTSRegistry {
|
||||
private engines: Record<string, BaseEngine>;
|
||||
private _announcement: BaseEngine;
|
||||
private _announcementVoice: string;
|
||||
|
||||
constructor(initialAnnouncementEngineName: string, initialAnnouncementVoice: string) {
|
||||
this.engines = {
|
||||
azure: new AzureEngine(),
|
||||
eleven: new ElevenEngine(),
|
||||
espeak: new EspeakEngine(),
|
||||
google: new GoogleEngine(),
|
||||
gtranslate: new GtranslateEngine(),
|
||||
openai: new OpenAIEngine(),
|
||||
sam: new SamEngine(),
|
||||
unreal: new UnrealEngine(),
|
||||
watson: new WatsonEngine(),
|
||||
};
|
||||
for (const name of Object.keys(this.engines)) {
|
||||
console.log(`Loaded TTS engine: ${name}`);
|
||||
}
|
||||
const ann = this.engines[initialAnnouncementEngineName];
|
||||
if (!ann) {
|
||||
throw new Error(
|
||||
`ANNOUNCEMENT_ENGINE "${initialAnnouncementEngineName}" is not a registered TTS engine`,
|
||||
);
|
||||
}
|
||||
this._announcement = ann;
|
||||
this._announcementVoice = initialAnnouncementVoice;
|
||||
}
|
||||
|
||||
get(name: string): BaseEngine | undefined {
|
||||
return this.engines[name];
|
||||
}
|
||||
|
||||
has(name: string): boolean {
|
||||
return name in this.engines;
|
||||
}
|
||||
|
||||
list(): string[] {
|
||||
return Object.keys(this.engines);
|
||||
}
|
||||
|
||||
get announcement(): BaseEngine {
|
||||
return this._announcement;
|
||||
}
|
||||
|
||||
set announcement(engine: BaseEngine) {
|
||||
this._announcement = engine;
|
||||
}
|
||||
|
||||
get announcementVoice(): string {
|
||||
return this._announcementVoice;
|
||||
}
|
||||
|
||||
set announcementVoice(voice: string) {
|
||||
this._announcementVoice = voice;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import Sam from "sam-js";
|
||||
import pkg from "wavefile";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
const { WaveFile } = pkg;
|
||||
|
||||
export class SamEngine extends BaseEngine {
|
||||
constructor() {
|
||||
super("sam", "Software Automatic Mouth", "wav");
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "sam";
|
||||
}
|
||||
|
||||
override validateVoice(_voice: string): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
override async getSpeechFile(
|
||||
text: string,
|
||||
filepath: string,
|
||||
_voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<string> {
|
||||
const sam = new Sam();
|
||||
let phonetic = false;
|
||||
let input = text;
|
||||
if (input.startsWith("$")) {
|
||||
input = input.slice(1);
|
||||
phonetic = true;
|
||||
}
|
||||
const buf = sam.buf8(input, phonetic);
|
||||
if (!(buf instanceof Uint8Array)) throw new Error("SAM produced no audio");
|
||||
const wave = new WaveFile();
|
||||
wave.fromScratch(1, 22050, "8", buf);
|
||||
await writeFile(filepath, wave.toBuffer());
|
||||
return filepath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { config } from "../config.js";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
interface UnrealResponse {
|
||||
OutputUri: string;
|
||||
}
|
||||
|
||||
export class UnrealEngine extends BaseEngine {
|
||||
protected override voices: Record<string, string> = {
|
||||
scarlett: "Scarlett",
|
||||
liv: "Liv",
|
||||
dan: "Dan",
|
||||
will: "Will",
|
||||
amy: "Amy",
|
||||
};
|
||||
|
||||
constructor() {
|
||||
super("unreal", "Unreal Speech TTS", "mp3");
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "liv";
|
||||
}
|
||||
|
||||
override async getSpeechFile(
|
||||
text: string,
|
||||
filepath: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<string> {
|
||||
if (!config.UNREAL_API_KEY) throw new Error("UNREAL_API_KEY must be set");
|
||||
const res = await fetch("https://api.v6.unrealspeech.com/speech", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${config.UNREAL_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
Bitrate: "320k",
|
||||
Temperature: 0.1,
|
||||
VoiceId: this.getInternalVoiceName(voice),
|
||||
Text: text,
|
||||
AudioFormat: "mp3",
|
||||
}),
|
||||
});
|
||||
const json = (await res.json()) as UnrealResponse;
|
||||
const audio = await fetch(json.OutputUri);
|
||||
await writeFile(filepath, Buffer.from(await audio.arrayBuffer()));
|
||||
return filepath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { config } from "../config.js";
|
||||
import { BaseEngine, type VoiceParams } from "./BaseEngine.js";
|
||||
|
||||
interface WatsonVoice {
|
||||
name: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
interface WatsonVoicesResponse {
|
||||
voices: WatsonVoice[];
|
||||
}
|
||||
|
||||
export class WatsonEngine extends BaseEngine {
|
||||
protected override voices: Record<string, string> = {};
|
||||
|
||||
constructor() {
|
||||
super("watson", "IBM Watson TTS", "ogg");
|
||||
void this.populateVoiceList();
|
||||
}
|
||||
|
||||
override getDefaultVoice(): string {
|
||||
return "michael";
|
||||
}
|
||||
|
||||
private authString(): string {
|
||||
if (!config.watsonAPIKey) throw new Error("watsonAPIKey must be set");
|
||||
const b64 = Buffer.from(`apikey:${config.watsonAPIKey}`).toString("base64");
|
||||
return `Basic ${b64}`;
|
||||
}
|
||||
|
||||
private async populateVoiceList(): Promise<void> {
|
||||
if (!config.watsonURL || !config.watsonAPIKey) return;
|
||||
try {
|
||||
const res = await fetch(`${config.watsonURL}/v1/voices`, {
|
||||
method: "GET",
|
||||
headers: { Authorization: this.authString() },
|
||||
});
|
||||
const json = (await res.json()) as WatsonVoicesResponse;
|
||||
for (const v of json.voices) {
|
||||
const key = v.description.substring(0, v.description.indexOf(":")).toLowerCase();
|
||||
this.voices[key] = v.name;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Watson: failed to populate voice list:", err);
|
||||
}
|
||||
}
|
||||
|
||||
override async getSpeech(
|
||||
text: string,
|
||||
voice: string = this.getDefaultVoice(),
|
||||
_params: VoiceParams = {},
|
||||
): Promise<Response> {
|
||||
if (!config.watsonURL) throw new Error("watsonURL must be set");
|
||||
const url = `${config.watsonURL}/v1/synthesize?voice=${this.getInternalVoiceName(voice)}`;
|
||||
return fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: this.authString(),
|
||||
},
|
||||
body: JSON.stringify({ text }),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user