Compare commits
70 Commits
efa52dffbc
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 20e162dc32 | |||
| 09e10e4113 | |||
| cbb5a9a76a | |||
| 9025831f3d | |||
| 06926e5601 | |||
| 85c35021b5 | |||
| c44f92f777 | |||
| 1927728b60 | |||
| 99b06b574b | |||
| 7c3a5f6b56 | |||
| 27a6807340 | |||
| 2fe0551dee | |||
| 91a4c6e40d | |||
| 69ee765889 | |||
| d972e6598e | |||
| 2f4e944df4 | |||
| 9571e32e61 | |||
| b246afdc7f | |||
| 123ed75b60 | |||
| 51ebb6c92d | |||
| 8e7a71164d | |||
| 224d1339e9 | |||
| d9c623ac5c | |||
| 68e622d318 | |||
| fea589dc2c | |||
| feabc732cf | |||
| bfc749a034 | |||
| 11539d149b | |||
| 7cbb5f3a9f | |||
| f218a2cef9 | |||
| 0cda7dd110 | |||
| ce98f13efd | |||
| 697cfd1de1 | |||
| f282a77411 | |||
| 049897fb07 | |||
| 17df430122 | |||
| 042fde30c4 | |||
| c7ff5d3659 | |||
| 893511ee11 | |||
| 5bc4cd02ec | |||
| 30966ec81a | |||
| f7558913ee | |||
| 6d21c3deca | |||
| 14194d07ff | |||
| b3109d643d | |||
| 00e02b9f97 | |||
| fd75f692d5 | |||
| e1363de9df | |||
| 0fc38828be | |||
| c849c8ee11 | |||
| 426c97e654 | |||
| 60b66027a3 | |||
| fda4bd91aa | |||
| 7b4dfb0dce | |||
| 8efdf0bc5b | |||
| 449c4efbb7 | |||
| 4932bd18d3 | |||
| 726fd914e4 | |||
| 753405c504 | |||
| 294e256feb | |||
| c005bc0e54 | |||
| 8e8d5dc479 | |||
| dfb58318af | |||
| 4abc2ff594 | |||
| 5877644ed9 | |||
| 71da6e841d | |||
| c1d355993e | |||
| a401fdab15 | |||
| c5e6395b89 | |||
| 0c394bdcbe |
+3
-1
@@ -2,4 +2,6 @@ node_modules/*
|
||||
dist/*
|
||||
|
||||
db.sqlite
|
||||
.env
|
||||
.env
|
||||
|
||||
docker-compose.yml
|
||||
@@ -6,5 +6,5 @@
|
||||
another discord bot, but this one runs a lil bit better, maybe, hopefully
|
||||
</p>
|
||||
|
||||
<p>yeah idk look at this seal tho</p>
|
||||
<p>idk look at cute this seal tho</p>
|
||||
<img src="https://i.pinimg.com/474x/16/eb/b9/16ebb902c9425b0d5a6251bbab048387.jpg"/>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
services:
|
||||
bot:
|
||||
build: .
|
||||
container_name: luma
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
DISCORD_ID: ${DISCORD_ID}
|
||||
DISCORD_TOKEN: ${DISCORD_TOKEN}
|
||||
DISCORD_OWNER_ID: ${DISCORD_OWNER_ID}
|
||||
TTS_TIKTOK_SESSIONID: ${TTS_TIKTOK_SESSIONID}
|
||||
TTS_ELEVENLABS_REFRESHTOKEN: ${TTS_ELEVENLABS_REFRESHTOKEN}
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- ./db.sqlite:/app/db.sqlite
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
# build
|
||||
FROM node:24-alpine AS builder
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache python3 make g++ gcc
|
||||
COPY package*.json ./
|
||||
RUN npm ci
|
||||
COPY . .
|
||||
RUN npm run build
|
||||
|
||||
# prod
|
||||
FROM node:24-alpine
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache python3 make g++ ffmpeg
|
||||
COPY package*.json ./
|
||||
RUN npm ci --only=production # only prod deps
|
||||
COPY --from=builder /app/dist ./dist
|
||||
|
||||
CMD ["npm", "start"]
|
||||
Generated
+1270
-358
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -34,13 +34,13 @@
|
||||
"typescript-eslint": "^8.52.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-polly": "^3.968.0",
|
||||
"@discordjs/opus": "^0.10.0",
|
||||
"@discordjs/voice": "^0.19.0",
|
||||
"@snazzah/davey": "^0.1.9",
|
||||
"colorts": "^0.1.63",
|
||||
"discord.js": "^14.25.1",
|
||||
"dotenv": "^17.2.3",
|
||||
"edge-tts-universal": "^1.3.3",
|
||||
"node-audio-mixer": "^2.1.0",
|
||||
"prettier": "^3.7.4",
|
||||
"prism-media": "^1.3.5",
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
console.log('scanning localstorage')
|
||||
const keys = Object.keys(localStorage).filter(k => k.startsWith("firebase:authUser"));
|
||||
|
||||
if (keys.length > 0) {
|
||||
const data = JSON.parse(localStorage.getItem(keys[0]));
|
||||
console.log("found in localstorage:", data.stsTokenManager);
|
||||
} else {
|
||||
console.error("no session found");
|
||||
}
|
||||
+4
-1
@@ -255,7 +255,10 @@ export class CommandManager {
|
||||
|
||||
if (command.requiresAdmin) {
|
||||
const member = interaction.member as GuildMember;
|
||||
if (!member.permissions.has(PermissionFlagsBits.Administrator)) {
|
||||
if (
|
||||
!member.permissions.has(PermissionFlagsBits.Administrator) &&
|
||||
member.id != config.owner_id
|
||||
) {
|
||||
await interaction.reply({
|
||||
content:
|
||||
"You don't have the permissions required to execute this command.",
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { CommandCategoryInfo } from '../../commands';
|
||||
|
||||
const info: CommandCategoryInfo = {
|
||||
name: 'Bot',
|
||||
description: 'Bot management commands'
|
||||
};
|
||||
|
||||
export default info;
|
||||
@@ -0,0 +1,100 @@
|
||||
import {
|
||||
ChatInputCommandInteraction,
|
||||
MessageCreateOptions,
|
||||
MessageFlags,
|
||||
SlashCommandBuilder,
|
||||
TextChannel
|
||||
} from 'discord.js';
|
||||
import { Command } from '../../commands';
|
||||
|
||||
const builder = new SlashCommandBuilder()
|
||||
.setName('bot-mimic')
|
||||
.setDescription('Makes the bot send a message')
|
||||
.addStringOption((opt) =>
|
||||
opt
|
||||
.setName('content')
|
||||
.setDescription('The text content of the message')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addAttachmentOption((opt) =>
|
||||
opt
|
||||
.setName('attachment')
|
||||
.setDescription('An attachment for the message')
|
||||
.setRequired(false)
|
||||
)
|
||||
.addStringOption((opt) =>
|
||||
opt
|
||||
.setName('reply')
|
||||
.setDescription('The message ID that the bot should reply to')
|
||||
.setRequired(false)
|
||||
);
|
||||
|
||||
const command: Command = {
|
||||
name: 'bot-mimic',
|
||||
builder: builder,
|
||||
ownerOnly: true,
|
||||
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
|
||||
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
|
||||
|
||||
if (!interaction.channel?.isTextBased()) {
|
||||
await interaction.editReply(
|
||||
'This command can only be used in a text channel.'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!interaction.channel.isSendable()) {
|
||||
await interaction.editReply('Channel is not sendable');
|
||||
return;
|
||||
}
|
||||
|
||||
const content = interaction.options.getString('content');
|
||||
const attachment = interaction.options.getAttachment('attachment');
|
||||
const replyId = interaction.options.getString('reply');
|
||||
|
||||
if (!content && !attachment) {
|
||||
await interaction.editReply(
|
||||
'Unable to send empty message. Specify content or attachment, or both.'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const channel = interaction.channel as TextChannel;
|
||||
const message: MessageCreateOptions = {};
|
||||
|
||||
if (content) {
|
||||
message.content = content;
|
||||
}
|
||||
|
||||
if (replyId) {
|
||||
try {
|
||||
const replyMessage = await channel.messages.fetch(replyId);
|
||||
message.reply = {
|
||||
messageReference: replyMessage.id
|
||||
};
|
||||
} catch {
|
||||
await interaction.editReply('Invalid message ID for reply.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (attachment) {
|
||||
message.files = [
|
||||
{
|
||||
attachment: attachment.proxyURL,
|
||||
name: attachment.name
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
await channel.send(message);
|
||||
await interaction.editReply('Message sent successfully.');
|
||||
} catch (error) {
|
||||
console.error('Failed to send message:', error);
|
||||
await interaction.editReply('Failed to send message.');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default command;
|
||||
@@ -1,32 +1,37 @@
|
||||
import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../../commands";
|
||||
import { Bot } from "../../bot";
|
||||
import {
|
||||
ChatInputCommandInteraction,
|
||||
EmbedBuilder,
|
||||
SlashCommandBuilder
|
||||
} from 'discord.js';
|
||||
import { Command } from '../../commands';
|
||||
import { Bot } from '../../bot';
|
||||
|
||||
const builder = new SlashCommandBuilder()
|
||||
.setName('commands')
|
||||
.setDescription('Shows a list of all the commands.');
|
||||
.setName('commands')
|
||||
.setDescription('Shows a list of all the commands.');
|
||||
|
||||
const cmd: Command = {
|
||||
name: builder.name,
|
||||
builder: builder,
|
||||
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
|
||||
const responseEmbed = new EmbedBuilder()
|
||||
.setColor("Blurple")
|
||||
.setTitle("Command List");
|
||||
name: builder.name,
|
||||
builder: builder,
|
||||
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
|
||||
const responseEmbed = new EmbedBuilder()
|
||||
.setColor('Blurple')
|
||||
.setTitle('Command List');
|
||||
|
||||
const bot = Bot.get;
|
||||
bot.getCategories().forEach(({ info, commands }) => {
|
||||
const fieldBody = commands
|
||||
.filter(({ builder }) => builder)
|
||||
.map(({ builder }) => `• **${builder?.name}** - ${builder?.description}`)
|
||||
.join("\n");
|
||||
const bot = Bot.get;
|
||||
bot.getCategories().forEach(({ info, commands }) => {
|
||||
const fieldBody = commands
|
||||
.filter(({ builder }) => builder)
|
||||
.map(
|
||||
({ builder }) => `• **${builder?.name}** - ${builder?.description}`
|
||||
)
|
||||
.join('\n');
|
||||
|
||||
responseEmbed.addFields({ name: info.name, value: fieldBody });
|
||||
});
|
||||
responseEmbed.addFields({ name: info.name, value: fieldBody });
|
||||
});
|
||||
|
||||
await interaction.reply({ embeds: [responseEmbed] });
|
||||
|
||||
}
|
||||
await interaction.reply({ embeds: [responseEmbed] });
|
||||
}
|
||||
};
|
||||
|
||||
export default cmd;
|
||||
export default cmd;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../../commands";
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js';
|
||||
import { Command } from '../../commands';
|
||||
|
||||
const builder = new SlashCommandBuilder()
|
||||
.setName('ping')
|
||||
.setDescription('Pong.');
|
||||
.setName('ping')
|
||||
.setDescription('Pong.');
|
||||
|
||||
const cmd: Command = {
|
||||
name: builder.name,
|
||||
builder: builder,
|
||||
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
|
||||
interaction.reply('pong!');
|
||||
}
|
||||
name: builder.name,
|
||||
builder: builder,
|
||||
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
|
||||
interaction.reply('pong!');
|
||||
}
|
||||
};
|
||||
|
||||
export default cmd;
|
||||
export default cmd;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js';
|
||||
import { Command } from '../../commands';
|
||||
import { getVoiceConnection, VoiceConnectionStatus } from '@discordjs/voice';
|
||||
import { AudioStreamManager } from '../../modules/audiostreams';
|
||||
import { AudioStreamManager } from '../../modules/audioStreams';
|
||||
|
||||
const builder = new SlashCommandBuilder()
|
||||
.setName('tts-clear')
|
||||
@@ -24,6 +24,7 @@ const cmd: Command = {
|
||||
|
||||
const queue = stream.getQueue('TTS');
|
||||
queue.clear();
|
||||
queue.flush();
|
||||
|
||||
interaction.reply('Queue cleared.');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import {
|
||||
AutocompleteInteraction,
|
||||
ChatInputCommandInteraction,
|
||||
SlashCommandBuilder
|
||||
} from 'discord.js';
|
||||
import { Command } from '../../commands';
|
||||
import { TTSManager } from '../../modules/tts';
|
||||
import { ElevenLabsTTS } from '../../modules/tts-modes/elevenlabs';
|
||||
|
||||
const builder = new SlashCommandBuilder()
|
||||
.setName('elevenlabs-settings')
|
||||
.setDescription('Configures ElevenLabs generation')
|
||||
.addNumberOption((opt) =>
|
||||
opt
|
||||
.setName('stability')
|
||||
.setDescription('Determines whether to be stable or more variable')
|
||||
.setMaxValue(1)
|
||||
.setMinValue(0)
|
||||
)
|
||||
.addNumberOption((opt) =>
|
||||
opt
|
||||
.setName('similarity-boost')
|
||||
.setDescription('Boosts clarity and target voice similarity')
|
||||
.setMaxValue(1.0)
|
||||
.setMinValue(0)
|
||||
)
|
||||
.addNumberOption((opt) =>
|
||||
opt
|
||||
.setName('style')
|
||||
.setDescription('How much should the style be exaggerated')
|
||||
.setMaxValue(1.0)
|
||||
.setMinValue(0)
|
||||
)
|
||||
.addNumberOption((opt) =>
|
||||
opt
|
||||
.setName('speed')
|
||||
.setDescription('The speed at which the text should be read')
|
||||
.setMaxValue(1.2)
|
||||
.setMinValue(0.7)
|
||||
)
|
||||
.addBooleanOption((opt) =>
|
||||
opt
|
||||
.setName('speaker-boost')
|
||||
.setDescription('Should speaker boost be enabled?')
|
||||
)
|
||||
.addStringOption((opt) =>
|
||||
opt
|
||||
.setName('model')
|
||||
.setDescription('Which generation model to use')
|
||||
.setAutocomplete(true)
|
||||
);
|
||||
|
||||
const cmd: Command = {
|
||||
name: builder.name,
|
||||
builder: builder,
|
||||
ownerOnly: true,
|
||||
|
||||
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
|
||||
const mod = TTSManager.get.getModule('ElevenLabs') as
|
||||
| ElevenLabsTTS
|
||||
| undefined;
|
||||
if (!mod) return;
|
||||
|
||||
const stability =
|
||||
interaction.options.getNumber('stability') ||
|
||||
ElevenLabsTTS.DEFAULT_SETTINGS.stability;
|
||||
|
||||
const similarityBoost =
|
||||
interaction.options.getNumber('similarity-boost') ||
|
||||
ElevenLabsTTS.DEFAULT_SETTINGS.similarity_boost;
|
||||
const style =
|
||||
interaction.options.getNumber('style') ||
|
||||
ElevenLabsTTS.DEFAULT_SETTINGS.style;
|
||||
const speed =
|
||||
interaction.options.getNumber('speed') ||
|
||||
ElevenLabsTTS.DEFAULT_SETTINGS.speed;
|
||||
const speakerBoost =
|
||||
interaction.options.getBoolean('speaker-boost') ||
|
||||
ElevenLabsTTS.DEFAULT_SETTINGS.user_speaker_boost;
|
||||
|
||||
mod.setSettings({
|
||||
stability: stability,
|
||||
style: style,
|
||||
speed: speed,
|
||||
user_speaker_boost: speakerBoost,
|
||||
similarity_boost: similarityBoost
|
||||
});
|
||||
|
||||
const model = interaction.options.getString('model');
|
||||
if (model) mod.setModel(model);
|
||||
|
||||
interaction.reply('ElevenLabs settings applied');
|
||||
},
|
||||
|
||||
autocomplete: async (interaction: AutocompleteInteraction): Promise<void> => {
|
||||
const focused = interaction.options.getFocused(true);
|
||||
if (focused.name != 'model') return;
|
||||
|
||||
const mod = TTSManager.get.getModule('ElevenLabs') as
|
||||
| ElevenLabsTTS
|
||||
| undefined;
|
||||
if (!mod) return;
|
||||
|
||||
const models = await mod.getModels();
|
||||
|
||||
const filtered: string[] = models
|
||||
.filter((model) =>
|
||||
model.toLowerCase().startsWith(focused.value.toLowerCase())
|
||||
)
|
||||
.slice(0, 25);
|
||||
|
||||
await interaction.respond(
|
||||
filtered.map((choice) => ({ name: choice, value: choice }))
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default cmd;
|
||||
@@ -3,13 +3,14 @@ import { Command } from '../../commands';
|
||||
import { Logger } from '../../utils/log';
|
||||
import { getVoiceConnection, VoiceConnectionStatus } from '@discordjs/voice';
|
||||
import { TTSManager } from '../../modules/tts';
|
||||
import { AudioStreamManager } from '../../modules/audiostreams';
|
||||
import { AudioStreamManager } from '../../modules/audioStreams';
|
||||
import { Readable } from 'stream';
|
||||
import { DataTypes } from 'sequelize';
|
||||
import { config } from '../../utils/config';
|
||||
import { DatabaseManager } from '../../modules/db';
|
||||
|
||||
const URL_REGEX = /(?:https?|ftp):\/\/[\n\S]+/g;
|
||||
const DISCORD_REGEX = /<(?::\w+:|@!*&*|#)[0-9]+>/g; // from: https://www.reddit.com/r/discordapp/comments/iibxms/if_anyone_needs_regex_to_match_an_emote_mention/
|
||||
|
||||
class TTSListener implements Command {
|
||||
private log: Logger;
|
||||
@@ -67,16 +68,30 @@ class TTSListener implements Command {
|
||||
if (!voices) return;
|
||||
if (!voices.includes(voiceName)) return;
|
||||
|
||||
const msgFiltered = msg.content.replace(URL_REGEX, 'a link');
|
||||
let msgFiltered = msg.content.replace(URL_REGEX, 'a link');
|
||||
msgFiltered = msgFiltered.replace(DISCORD_REGEX, '');
|
||||
|
||||
if (msgFiltered.length === 0) return;
|
||||
|
||||
const audio = await ttsModule.generate(voiceName, msgFiltered);
|
||||
if (!audio) {
|
||||
this.log.error("TTS generation didn't return anything");
|
||||
return;
|
||||
}
|
||||
|
||||
if (audio?.data) {
|
||||
if (audio.data) {
|
||||
const stream =
|
||||
AudioStreamManager.get.getOrCreateStream(voiceConnection);
|
||||
const queue = stream.getQueue('TTS');
|
||||
queue.enqueue(Readable.from(audio.data));
|
||||
}
|
||||
|
||||
if (audio.error) {
|
||||
this.log.error(
|
||||
'Error occurred while generating message: (%s)',
|
||||
audio.error
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
this.log.error('Error occurred while processing TTS message (%s)', err);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const builder = new SlashCommandBuilder()
|
||||
const cmd: Command = {
|
||||
name: builder.name,
|
||||
builder: builder,
|
||||
|
||||
requiresAdmin: true,
|
||||
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
|
||||
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ const cmd: Command = {
|
||||
const modeName = interaction.options.getString('mode', true);
|
||||
const selectedMode = TTSManager.get
|
||||
.getModules()
|
||||
.filter(async (mod) => await mod.canBeUsed())
|
||||
.find((mode) => mode.name === modeName);
|
||||
|
||||
if (!selectedMode) {
|
||||
@@ -38,6 +39,8 @@ const cmd: Command = {
|
||||
}
|
||||
|
||||
await userData.set('tts_mode', modeName);
|
||||
if (selectedMode.defaultVoice)
|
||||
await userData.set('tts_voice', selectedMode.defaultVoice);
|
||||
await userData.save();
|
||||
|
||||
interaction.editReply(`TTS mode has been set to: ${modeName}.`);
|
||||
@@ -50,12 +53,13 @@ const cmd: Command = {
|
||||
const modes = TTSManager.get.getModules();
|
||||
|
||||
const filtered: string[] = modes
|
||||
.filter((mode) => {
|
||||
return mode.name
|
||||
? mode.name.toLowerCase().startsWith(focused.value.toLowerCase())
|
||||
.filter((mod) => mod.canBeUsed())
|
||||
.filter((mod) => {
|
||||
return mod.name
|
||||
? mod.name.toLowerCase().startsWith(focused.value.toLowerCase())
|
||||
: undefined;
|
||||
})
|
||||
.map((mode) => mode.name)
|
||||
.map((mod) => mod.name)
|
||||
.slice(0, 25);
|
||||
|
||||
await interaction.respond(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
ChatInputCommandInteraction,
|
||||
GuildMember,
|
||||
PermissionsBitField,
|
||||
SlashCommandBuilder
|
||||
} from 'discord.js';
|
||||
import { Command } from '../../commands';
|
||||
@@ -25,7 +26,7 @@ const cmd: Command = {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!member.voice.channelId) {
|
||||
if (!member.voice.channel || !member.voice.channelId) {
|
||||
interaction.reply('You are not currently on a voice channel');
|
||||
return;
|
||||
}
|
||||
@@ -40,6 +41,32 @@ const cmd: Command = {
|
||||
return;
|
||||
}
|
||||
|
||||
const voiceChannel = member.voice.channel;
|
||||
if (
|
||||
voiceChannel.userLimit != 0 &&
|
||||
voiceChannel.members.size >= voiceChannel.userLimit
|
||||
) {
|
||||
interaction.reply('Channel is full');
|
||||
return;
|
||||
}
|
||||
|
||||
const perms = voiceChannel.permissionsFor(me);
|
||||
|
||||
if (!perms.has(PermissionsBitField.Flags.ViewChannel)) {
|
||||
interaction.reply("I don't have permissions to see that channel");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!perms.has(PermissionsBitField.Flags.Connect)) {
|
||||
interaction.reply("I don't have the permissions to join that channel");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!perms.has(PermissionsBitField.Flags.Speak)) {
|
||||
interaction.reply("I don't have permissions to speak on that channel");
|
||||
return;
|
||||
}
|
||||
|
||||
const voiceOptions: JoinVoiceChannelOptions & CreateVoiceConnectionOptions =
|
||||
{
|
||||
channelId: member.voice.channelId,
|
||||
|
||||
+88
-46
@@ -1,19 +1,24 @@
|
||||
import {
|
||||
AudioPlayer,
|
||||
AudioPlayerStatus,
|
||||
createAudioPlayer,
|
||||
createAudioResource,
|
||||
StreamType,
|
||||
VoiceConnection
|
||||
VoiceConnection,
|
||||
VoiceConnectionStatus
|
||||
} from '@discordjs/voice';
|
||||
import { AudioMixer } from 'node-audio-mixer';
|
||||
import { PassThrough, Readable } from 'stream';
|
||||
|
||||
import prism from 'prism-media';
|
||||
|
||||
const DURATION_EXTRA_MS = 1000;
|
||||
|
||||
export class StreamQueue {
|
||||
private queue: Readable[] = [];
|
||||
private isPlaying = false;
|
||||
private mixer: MixedStream;
|
||||
private currentStop: (() => void) | null = null;
|
||||
|
||||
constructor(mixer: MixedStream) {
|
||||
this.mixer = mixer;
|
||||
@@ -32,7 +37,10 @@ export class StreamQueue {
|
||||
|
||||
try {
|
||||
if (nextStream) {
|
||||
await this.mixer.playStream(nextStream);
|
||||
const { completion, stop } = this.mixer.playStream(nextStream);
|
||||
this.currentStop = stop;
|
||||
await completion;
|
||||
this.currentStop = null;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Queue error:', e);
|
||||
@@ -44,14 +52,21 @@ export class StreamQueue {
|
||||
|
||||
public clear() {
|
||||
this.queue = [];
|
||||
if (this.currentStop) {
|
||||
this.currentStop();
|
||||
this.currentStop = null;
|
||||
}
|
||||
}
|
||||
|
||||
public flush() {
|
||||
this.mixer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
export class MixedStream {
|
||||
public readonly player: AudioPlayer;
|
||||
private mixer: AudioMixer;
|
||||
private output: PassThrough;
|
||||
private silenceInterval: NodeJS.Timeout;
|
||||
private output: PassThrough | undefined;
|
||||
|
||||
private queues: Map<string, StreamQueue> = new Map();
|
||||
|
||||
@@ -63,33 +78,7 @@ export class MixedStream {
|
||||
bitDepth: 16,
|
||||
sampleRate: 48000,
|
||||
autoClose: false,
|
||||
generateSilence: false // does not work :<
|
||||
});
|
||||
|
||||
const silenceInput = this.mixer.createAudioInput({
|
||||
channels: 2,
|
||||
sampleRate: 48000,
|
||||
bitDepth: 16,
|
||||
volume: 100
|
||||
});
|
||||
|
||||
const chunk = Buffer.alloc(3840);
|
||||
this.silenceInterval = setInterval(() => {
|
||||
if (silenceInput.writable && silenceInput.writableLength < 3840 * 10) {
|
||||
silenceInput.write(chunk);
|
||||
}
|
||||
}, 20);
|
||||
|
||||
this.output = new PassThrough({ highWaterMark: 1024 * 16 });
|
||||
this.mixer.pipe(this.output);
|
||||
|
||||
const resource = createAudioResource(this.output, {
|
||||
inputType: StreamType.Raw
|
||||
});
|
||||
|
||||
this.player.play(resource);
|
||||
this.player.on('error', (error) => {
|
||||
console.error('Error: ', error.message);
|
||||
generateSilence: false
|
||||
});
|
||||
}
|
||||
|
||||
@@ -102,8 +91,16 @@ export class MixedStream {
|
||||
return queue;
|
||||
}
|
||||
|
||||
public playStream(source: Readable): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
public playStream(source: Readable): {
|
||||
completion: Promise<void>;
|
||||
stop: () => void;
|
||||
} {
|
||||
let stopCallback: () => void = () => {};
|
||||
const completion = new Promise<void>((resolve) => {
|
||||
if (this.player.state.status === AudioPlayerStatus.Idle) {
|
||||
this.setupPipeline();
|
||||
}
|
||||
|
||||
const mixerInput = this.mixer.createAudioInput({
|
||||
channels: 2,
|
||||
sampleRate: 48000,
|
||||
@@ -131,32 +128,67 @@ export class MixedStream {
|
||||
totalBytes += chunk.length;
|
||||
});
|
||||
|
||||
let resolved = false;
|
||||
const cleanup = () => {
|
||||
if (resolved) return;
|
||||
resolved = true;
|
||||
|
||||
source.unpipe(transcoder);
|
||||
source.destroy();
|
||||
|
||||
transcoder.unpipe(mixerInput);
|
||||
transcoder.destroy();
|
||||
|
||||
this.mixer.removeAudioinput(mixerInput);
|
||||
mixerInput.destroy();
|
||||
|
||||
resolve();
|
||||
};
|
||||
|
||||
stopCallback = cleanup;
|
||||
|
||||
transcoder.on('end', () => {
|
||||
const durationMs = (totalBytes / 192000) * 1000;
|
||||
const durationMs = (totalBytes / 192000) * 1000 + DURATION_EXTRA_MS;
|
||||
|
||||
setTimeout(() => {
|
||||
source.unpipe(transcoder);
|
||||
transcoder.unpipe(mixerInput);
|
||||
this.mixer.removeAudioinput(mixerInput);
|
||||
transcoder.destroy();
|
||||
resolve();
|
||||
cleanup();
|
||||
}, durationMs);
|
||||
});
|
||||
|
||||
transcoder.on('error', () => {
|
||||
this.mixer.removeAudioinput(mixerInput);
|
||||
resolve();
|
||||
transcoder.on('error', (err) => {
|
||||
console.error('Transcoder error:', err);
|
||||
cleanup();
|
||||
});
|
||||
|
||||
source.pipe(transcoder).pipe(mixerInput);
|
||||
});
|
||||
|
||||
return { completion, stop: stopCallback };
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
this.player.stop();
|
||||
this.output.destroy();
|
||||
if (this.output) this.output.destroy();
|
||||
this.mixer.destroy();
|
||||
clearInterval(this.silenceInterval);
|
||||
}
|
||||
|
||||
public flush(): void {
|
||||
this.player.stop();
|
||||
this.setupPipeline();
|
||||
}
|
||||
|
||||
private setupPipeline(): void {
|
||||
if (this.output) {
|
||||
this.mixer.unpipe(this.output);
|
||||
this.output.destroy();
|
||||
}
|
||||
this.output = new PassThrough({ highWaterMark: 1024 * 256 });
|
||||
this.mixer.pipe(this.output);
|
||||
|
||||
const resource = createAudioResource(this.output, {
|
||||
inputType: StreamType.Raw
|
||||
});
|
||||
this.player.play(resource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,6 +202,16 @@ export class AudioStreamManager {
|
||||
stream = new MixedStream();
|
||||
this.streams.set(conn, stream);
|
||||
conn.subscribe(stream.player);
|
||||
|
||||
conn.on('stateChange', (_, newState) => {
|
||||
if (
|
||||
newState.status === VoiceConnectionStatus.Disconnected ||
|
||||
newState.status === VoiceConnectionStatus.Destroyed
|
||||
) {
|
||||
this.destroyStream(conn);
|
||||
}
|
||||
});
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
@@ -182,8 +224,8 @@ export class AudioStreamManager {
|
||||
}
|
||||
|
||||
/*
|
||||
singleton logic
|
||||
*/
|
||||
singleton logic
|
||||
*/
|
||||
static #instance: AudioStreamManager | null = null;
|
||||
|
||||
public static get get(): AudioStreamManager {
|
||||
|
||||
+239
-14
@@ -1,36 +1,261 @@
|
||||
import { createHash, randomBytes } from 'crypto';
|
||||
import { TTSModule, TTSResponse } from '../tts';
|
||||
|
||||
import { VoicesManager, Communicate } from 'edge-tts-universal';
|
||||
import * as https from 'https';
|
||||
|
||||
import { WebSocket } from 'ws';
|
||||
import { Logger } from '../../utils/log';
|
||||
|
||||
const CLIENT_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';
|
||||
const AZURE_ENDPOINT = 'speech.platform.bing.com';
|
||||
|
||||
const READALOUD_PATH = `/consumer/speech/synthesize/readaloud`;
|
||||
const WEBSOCKET_URL = `wss://${AZURE_ENDPOINT}${READALOUD_PATH}/edge/v1?TrustedClientToken=${CLIENT_TOKEN}`;
|
||||
const VOICES_PATH = `${READALOUD_PATH}/voices/list?TrustedClientToken=${CLIENT_TOKEN}`;
|
||||
|
||||
const CHROME_VERSION = '138.0.7204.157';
|
||||
const SEC_VERSION = `1-${CHROME_VERSION}`;
|
||||
|
||||
const USER_AGENT = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${CHROME_VERSION.split('.')[0]}.0.0.0 Safari/537.36 Edg/${CHROME_VERSION.split('.')[0]}.0.0.0`;
|
||||
const WIN_EPOCH = 11644473600;
|
||||
|
||||
const WS_RECONNECT_DELAY = 2000;
|
||||
const MAX_RECONNECT_ATTEMPTS = 5;
|
||||
|
||||
interface PendingRequest {
|
||||
resolve: (value: TTSResponse) => void;
|
||||
reject: (reason: Error) => void;
|
||||
audioBuff: Buffer[];
|
||||
}
|
||||
|
||||
interface VoiceInfo {
|
||||
// Name: string;
|
||||
ShortName: string,
|
||||
// Gender: string,
|
||||
// Locale: string,
|
||||
}
|
||||
|
||||
class AzureTTS implements TTSModule {
|
||||
private voices: Array<string> | undefined = undefined;
|
||||
|
||||
public name: string = 'Azure';
|
||||
public defaultVoice: string = 'en-US-AvaNeural';
|
||||
|
||||
private ready: boolean = false;
|
||||
private readyPromise: Promise<void> | null = null;
|
||||
private readyResolve: (() => void) | null = null;
|
||||
|
||||
private ws: WebSocket | undefined = undefined;
|
||||
private reconnectAttempts: number = 0;
|
||||
private reconnectTimer: NodeJS.Timeout | null = null;
|
||||
private isReconnecting: boolean = false;
|
||||
|
||||
private log: Logger;
|
||||
// Map keyed by X-RequestId
|
||||
private pendingRequests: Map<string, PendingRequest> = new Map();
|
||||
constructor() {
|
||||
this.log = new Logger('Azure TTS');
|
||||
this.initializeConnection();
|
||||
}
|
||||
|
||||
async getVoices(): Promise<Array<string> | undefined> {
|
||||
if (!this.voices) {
|
||||
const voiceMgr = await VoicesManager.create();
|
||||
const voiceQuery = await voiceMgr.find({});
|
||||
if (this.voices) return this.voices;
|
||||
|
||||
this.voices = voiceQuery.map((voice) => voice.ShortName);
|
||||
}
|
||||
const options: https.RequestOptions = {
|
||||
hostname: AZURE_ENDPOINT,
|
||||
path: `${VOICES_PATH}&Sec-MS-GEC=${this.genSecToken()}&Sec-MS-GEC-Version=${SEC_VERSION}`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Pragma: 'no-cache',
|
||||
'Cache-Control': 'no-cache',
|
||||
'User-Agent': USER_AGENT,
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'Accept-Language': 'en-US,en;q=0.9',
|
||||
Authority: 'speech.platform.bing.com',
|
||||
'Sec-CH-UA': `" Not;A Brand";v="99", "Microsoft Edge";v="${CHROME_VERSION.split('.')[0]}", "Chromium";v="${CHROME_VERSION.split('.')[0]}"`,
|
||||
'Sec-CH-UA-Mobile': '?0',
|
||||
Accept: '*/*',
|
||||
'Sec-Fetch-Site': 'none',
|
||||
'Sec-Fetch-Mode': 'cors',
|
||||
'Sec-Fetch-Dest': 'empty'
|
||||
}
|
||||
};
|
||||
|
||||
return this.voices;
|
||||
return new Promise((resolve) => {
|
||||
const req = https.request(options, (res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
const body = Buffer.concat(chunks).toString();
|
||||
this.voices = JSON.parse(body).map((v: VoiceInfo) => v.ShortName);
|
||||
resolve(this.voices);
|
||||
});
|
||||
req.on('error', (err) => {
|
||||
throw err;
|
||||
});
|
||||
res.on('aborted', () => {
|
||||
throw new Error('Response aborted');
|
||||
});
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async generate(voice: string, text: string): Promise<TTSResponse> {
|
||||
const comm = new Communicate(text, {
|
||||
voice: voice
|
||||
await this.readyPromise;
|
||||
if (!this.ready || !this.ws) return { error: 'Not initialized' };
|
||||
|
||||
const reqId = randomBytes(16).toString('hex');
|
||||
const lang = voice.split('-').slice(0, 2).join('-');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.pendingRequests.set(reqId, { resolve, reject, audioBuff: [] });
|
||||
|
||||
const headers = `X-RequestId:${reqId}\r\nContent-Type:application/ssml+xml\r\nPath:ssml\r\n\r\n`;
|
||||
const ssml = `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="${lang}"><voice name="${voice}"><prosody rate="default" pitch="default">${this.escapeXml(text)}</prosody></voice></speak>`;
|
||||
|
||||
this.ws?.send(headers + ssml, (err) => {
|
||||
if (err) {
|
||||
this.pendingRequests.delete(reqId);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
canBeUsed(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
private initializeConnection(): void {
|
||||
this.ready = false;
|
||||
this.readyPromise = new Promise((resolve) => {
|
||||
this.readyResolve = resolve;
|
||||
this.connect();
|
||||
});
|
||||
}
|
||||
|
||||
private connect(): void {
|
||||
const url = `${WEBSOCKET_URL}&Sec-MS-GEC=${this.genSecToken()}&Sec-MS-GEC-Version=${SEC_VERSION}`;
|
||||
|
||||
this.ws = new WebSocket(url, {
|
||||
host: 'speech.platform.bing.com',
|
||||
origin: 'chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold',
|
||||
headers: {
|
||||
Pragma: 'no-cache',
|
||||
'User-Agent': USER_AGENT
|
||||
}
|
||||
});
|
||||
|
||||
const buffers: Buffer[] = [];
|
||||
for await (const chunk of comm.stream()) {
|
||||
if (chunk.type === 'audio' && chunk.data) {
|
||||
buffers.push(chunk.data);
|
||||
this.ws.on('open', () => {
|
||||
// this.log.verbose('WebSocket open');
|
||||
this.reconnectAttempts = 0;
|
||||
this.isReconnecting = false;
|
||||
|
||||
const config = `Content-Type:application/json; charset=utf-8\r\nPath:speech.config\r\n\r\n
|
||||
{
|
||||
"context": {
|
||||
"synthesis": {
|
||||
"audio": {
|
||||
"metadataoptions": { "sentenceBoundaryEnabled": "false", "wordBoundaryEnabled": "true" },
|
||||
"outputFormat": "audio-24khz-48kbitrate-mono-mp3"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
this.ws?.send(config.trim());
|
||||
this.ready = true;
|
||||
this.readyResolve?.();
|
||||
});
|
||||
|
||||
this.ws.on('message', (data: Buffer, isBinary: boolean) => {
|
||||
this.handleIncomingMessage(data, isBinary);
|
||||
});
|
||||
|
||||
this.ws.on('close', (/*code, reason*/) => {
|
||||
this.ready = false;
|
||||
// this.log.verbose(`WS Closed: ${code}`);
|
||||
this.rejectAllPending(new Error('Connection closed'));
|
||||
this.scheduleReconnect();
|
||||
});
|
||||
|
||||
this.ws.on('error', (err) => {
|
||||
this.log.error('WS Error:', err);
|
||||
});
|
||||
}
|
||||
|
||||
private scheduleReconnect() {
|
||||
if (this.reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) return;
|
||||
|
||||
const delay = WS_RECONNECT_DELAY * Math.pow(2, this.reconnectAttempts++);
|
||||
setTimeout(() => this.connect(), delay);
|
||||
}
|
||||
|
||||
private handleIncomingMessage(data: Buffer, isBinary: boolean) {
|
||||
const message = data.toString();
|
||||
const reqId = message.match(/X-RequestId:(.*?)\r\n/)?.[1];
|
||||
if (!reqId) return;
|
||||
|
||||
const request = this.pendingRequests.get(reqId);
|
||||
if (!request) return;
|
||||
|
||||
if (isBinary) {
|
||||
const separator = 'Path:audio\r\n';
|
||||
const index = data.indexOf(separator);
|
||||
if (index !== -1) {
|
||||
request.audioBuff.push(data.subarray(index + separator.length));
|
||||
}
|
||||
} else {
|
||||
if (message.includes('Path:turn.end')) {
|
||||
request.resolve({ data: Buffer.concat(request.audioBuff) });
|
||||
this.pendingRequests.delete(reqId);
|
||||
} else if (
|
||||
message.includes('Path:turn.error') ||
|
||||
message.includes('Path:error')
|
||||
) {
|
||||
request.reject(new Error('Azure synthesis error'));
|
||||
this.pendingRequests.delete(reqId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { data: Buffer.concat(buffers) };
|
||||
private rejectAllPending(err: Error) {
|
||||
for (const [id, req] of this.pendingRequests) {
|
||||
req.reject(err);
|
||||
this.pendingRequests.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
private genSecToken(): string {
|
||||
const ticks =
|
||||
BigInt(Math.floor(Date.now() / 1000 + Number(WIN_EPOCH))) * 10000000n;
|
||||
const roundedTicks = ticks - (ticks % 3000000000n);
|
||||
|
||||
const strToHash = `${roundedTicks}${CLIENT_TOKEN}`;
|
||||
|
||||
const hash = createHash('sha256');
|
||||
hash.update(strToHash, 'ascii');
|
||||
|
||||
return hash.digest('hex').toUpperCase();
|
||||
}
|
||||
|
||||
private escapeXml(unsafe: string): string {
|
||||
return unsafe.replace(/[<>&"']/g, (c) => {
|
||||
switch (c) {
|
||||
case '<':
|
||||
return '<';
|
||||
case '>':
|
||||
return '>';
|
||||
case '&':
|
||||
return '&';
|
||||
case '"':
|
||||
return '"';
|
||||
case "'":
|
||||
return ''';
|
||||
default:
|
||||
return c;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,276 @@
|
||||
import { config } from '../../utils/config';
|
||||
import { TTSModule, TTSResponse } from '../tts';
|
||||
|
||||
import * as https from 'https';
|
||||
|
||||
const ELEVENLABS_API_ENDPOINT = 'api.elevenlabs.io';
|
||||
|
||||
const FIREBASE_API_KEY = 'AIzaSyBSsRE_1Os04-bxpd5JTLIniy3UK4OqKys';
|
||||
const FIREBASE_URL = `https://securetoken.googleapis.com/v1/token?key=${FIREBASE_API_KEY}`;
|
||||
|
||||
/*
|
||||
TO-DO: Implement previous text
|
||||
*/
|
||||
|
||||
interface ElevenLabsVoice {
|
||||
voice_id: string;
|
||||
name: string;
|
||||
// ...
|
||||
}
|
||||
|
||||
interface ElevenLabsModel {
|
||||
model_id: string;
|
||||
name: string;
|
||||
// ...
|
||||
}
|
||||
|
||||
interface ElevenLabsVoicesRes {
|
||||
voices?: Array<ElevenLabsVoice>;
|
||||
}
|
||||
|
||||
interface ElevenLabsVoiceSettings {
|
||||
stability: number;
|
||||
similarity_boost: number;
|
||||
style: number;
|
||||
speed: number;
|
||||
user_speaker_boost: boolean;
|
||||
}
|
||||
|
||||
interface ElevenLabsStreamRequest {
|
||||
text: string;
|
||||
model_id: string;
|
||||
voice_settings: ElevenLabsVoiceSettings;
|
||||
}
|
||||
|
||||
interface FirebaseSession {
|
||||
idToken: string;
|
||||
refreshToken: string;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
export class ElevenLabsTTS implements TTSModule {
|
||||
private voices: Array<ElevenLabsVoice> | undefined = undefined;
|
||||
private models: Array<ElevenLabsModel> | undefined = undefined;
|
||||
|
||||
public name: string = 'ElevenLabs';
|
||||
|
||||
public settings: ElevenLabsVoiceSettings;
|
||||
public modelId: string;
|
||||
|
||||
private session: FirebaseSession | undefined = undefined;
|
||||
|
||||
private initializationPromise: Promise<void> | undefined = undefined;
|
||||
|
||||
public static readonly DEFAULT_SETTINGS: ElevenLabsVoiceSettings = {
|
||||
stability: 0.0,
|
||||
similarity_boost: 0.5,
|
||||
style: 1.0,
|
||||
speed: 1.0,
|
||||
user_speaker_boost: true
|
||||
};
|
||||
|
||||
constructor() {
|
||||
this.settings = ElevenLabsTTS.DEFAULT_SETTINGS;
|
||||
this.modelId = 'eleven_v3';
|
||||
|
||||
if (this.canBeUsed()) this.initializationPromise = this.init();
|
||||
|
||||
this.setSettings = this.setSettings.bind(this);
|
||||
this.setModel = this.setModel.bind(this);
|
||||
this.getModels = this.getModels.bind(this);
|
||||
}
|
||||
|
||||
private async init(): Promise<void> {
|
||||
await this.ensureSession();
|
||||
await Promise.all([this.fetchVoices(), this.fetchModels()]);
|
||||
}
|
||||
|
||||
/*
|
||||
TTSModule methods
|
||||
*/
|
||||
async getVoices(): Promise<Array<string> | undefined> {
|
||||
if (this.voices) return this.voices.map((voice) => voice.name);
|
||||
}
|
||||
|
||||
async generate(voice: string, text: string): Promise<TTSResponse> {
|
||||
await this.initializationPromise;
|
||||
await this.ensureSession();
|
||||
|
||||
if (!this.voices) return { error: 'no voices' };
|
||||
if (!this.session) return { error: 'no session' };
|
||||
|
||||
const voiceData = this.voices.find((entry) => entry.name === voice);
|
||||
if (!voiceData) return { error: 'Invalid voice' };
|
||||
|
||||
const options: https.RequestOptions = {
|
||||
hostname: ELEVENLABS_API_ENDPOINT,
|
||||
path: `/v1/text-to-speech/${voiceData.voice_id}/stream`,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
origin: 'https://elevenlabs.io',
|
||||
'user-agent':
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
|
||||
'Sec-Ch-Ua': '"Not)A;Brand";v="8", "Chromium";v="138"',
|
||||
'Sec-Ch-Ua-Mobile': '?0',
|
||||
'Sec-Ch-Ua-Platform': '"Windows"',
|
||||
'Sec-Fetch-Site': 'same-site',
|
||||
'Sec-Fetch-Mode': 'cors',
|
||||
'Sec-Fetch-Dest': 'empty',
|
||||
host: 'api.elevenlabs.io',
|
||||
Authorization: `Bearer ${this.session.idToken}`
|
||||
}
|
||||
};
|
||||
|
||||
const body: ElevenLabsStreamRequest = {
|
||||
text: text,
|
||||
model_id: this.modelId,
|
||||
voice_settings: this.settings
|
||||
};
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const req = https.request(options, (res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
resolve({
|
||||
data: Buffer.concat(chunks)
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (error) => resolve({ error: error.message }));
|
||||
|
||||
req.write(JSON.stringify(body));
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
canBeUsed(): boolean {
|
||||
return config.tts_elevenlabs_refreshtoken != undefined;
|
||||
}
|
||||
|
||||
/*
|
||||
ElevenLabs specific methods
|
||||
*/
|
||||
public setSettings(settings: Partial<ElevenLabsVoiceSettings>) {
|
||||
this.settings = { ...this.settings, ...settings };
|
||||
}
|
||||
|
||||
public setModel(name: string) {
|
||||
if (!this.models) return;
|
||||
|
||||
const model = this.models.find((mod) => mod.name == name);
|
||||
if (!model) return;
|
||||
|
||||
this.modelId = model.model_id;
|
||||
}
|
||||
|
||||
public getModels(): Array<string> {
|
||||
if (!this.models) return [];
|
||||
return this.models.map((mod) => mod.name);
|
||||
}
|
||||
|
||||
private async fetchVoices(): Promise<void> {
|
||||
if (!this.session) return;
|
||||
|
||||
const opt: https.RequestOptions = {
|
||||
hostname: ELEVENLABS_API_ENDPOINT,
|
||||
path: '/v2/voices',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
Authorization: `Bearer ${this.session.idToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const req = https.get(opt, (res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
const voicesJSON = Buffer.concat(chunks).toString('utf-8');
|
||||
const voicesParsed = JSON.parse(voicesJSON) as ElevenLabsVoicesRes;
|
||||
if (!voicesParsed.voices) {
|
||||
console.error('ElevenLabs voice fetch responded:', voicesJSON);
|
||||
throw new Error('Failed to get ElevenLabs voices');
|
||||
}
|
||||
this.voices = voicesParsed.voices;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (err) => {
|
||||
console.error('Failed to get ElevenLabs voices:', err);
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async fetchModels(): Promise<void> {
|
||||
if (!this.session) return;
|
||||
|
||||
const opt: https.RequestOptions = {
|
||||
hostname: ELEVENLABS_API_ENDPOINT,
|
||||
path: '/v1/models',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
Authorization: `Bearer ${this.session.idToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const req = https.get(opt, (res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
const modelsJSON = Buffer.concat(chunks).toString('utf-8');
|
||||
const modelsParsed = JSON.parse(modelsJSON) as Array<ElevenLabsModel>;
|
||||
this.models = modelsParsed;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (err) => {
|
||||
console.error('Failed to get ElevenLabs models:', err);
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async ensureSession(): Promise<void> {
|
||||
if (this.session && Date.now() < this.session.expiresAt - 300000) return;
|
||||
|
||||
const refreshToken =
|
||||
this.session?.refreshToken || config.tts_elevenlabs_refreshtoken;
|
||||
if (!refreshToken) throw new Error('No refresh token available');
|
||||
|
||||
const response = await fetch(FIREBASE_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
Referer: 'https://elevenlabs.io/',
|
||||
Origin: 'https://elevenlabs.io'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: refreshToken
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok)
|
||||
throw new Error(`Auth Refresh Failed: ${await response.text()}`);
|
||||
const data = await response.json();
|
||||
this.session = {
|
||||
idToken: data.id_token,
|
||||
refreshToken: data.refresh_token,
|
||||
expiresAt: Date.now() + parseInt(data.expires_in) * 1000
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default new ElevenLabsTTS();
|
||||
@@ -9,9 +9,13 @@ const USER_AGENT =
|
||||
|
||||
const ttsGoogle: TTSModule = {
|
||||
name: 'Google',
|
||||
getVoices: async (): Promise<string[]> => GOOGLE_TTS_VOICES.voices,
|
||||
defaultVoice: 'en',
|
||||
|
||||
generate: async (voice: string, text: string): Promise<TTSResponse> => {
|
||||
async getVoices(): Promise<string[]> {
|
||||
return GOOGLE_TTS_VOICES.voices;
|
||||
},
|
||||
|
||||
async generate(voice: string, text: string): Promise<TTSResponse> {
|
||||
const query = new URLSearchParams({
|
||||
ie: 'UTF-8',
|
||||
q: text,
|
||||
@@ -41,6 +45,10 @@ const ttsGoogle: TTSModule = {
|
||||
resolve({ error: 'timed out' });
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
canBeUsed(): boolean {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@ const ttsNone: TTSModule = {
|
||||
getVoices: async (): Promise<Array<string>> => [],
|
||||
generate: async (): Promise<TTSResponse> => {
|
||||
return { data: Buffer.from([]) };
|
||||
},
|
||||
canBeUsed: (): boolean => {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import {
|
||||
PollyClient,
|
||||
DescribeVoicesCommand,
|
||||
Voice,
|
||||
SynthesizeSpeechCommand,
|
||||
Engine
|
||||
} from '@aws-sdk/client-polly';
|
||||
import { TTSModule, TTSResponse } from '../tts';
|
||||
import { config } from '../../utils/config';
|
||||
|
||||
const ENGINE_PRIORITY: Engine[] = [
|
||||
'generative',
|
||||
'neural',
|
||||
'standard',
|
||||
'long-form'
|
||||
];
|
||||
|
||||
class PollyTTS implements TTSModule {
|
||||
private client: PollyClient | undefined = undefined;
|
||||
private voices: Array<Voice> | undefined = undefined;
|
||||
|
||||
public name: string = 'AWS Polly';
|
||||
|
||||
constructor() {
|
||||
if (!config.aws_access_id || !config.aws_access_key) return;
|
||||
|
||||
this.client = new PollyClient({
|
||||
credentials: {
|
||||
accessKeyId: config.aws_access_id,
|
||||
secretAccessKey: config.aws_access_key
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async getVoices(): Promise<Array<string> | undefined> {
|
||||
if (!this.client) return [];
|
||||
|
||||
if (!this.voices) {
|
||||
const cmd = new DescribeVoicesCommand({});
|
||||
|
||||
try {
|
||||
const res = await this.client.send(cmd);
|
||||
if (res.Voices) this.voices = res.Voices;
|
||||
} catch (err) {
|
||||
console.error('AWS Polly getVoices error:', err);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.voices)
|
||||
return this.voices.map((voice) => `${voice.LanguageCode} ${voice.Id}`);
|
||||
}
|
||||
|
||||
async generate(voice: string, text: string): Promise<TTSResponse> {
|
||||
if (!this.client || !this.voices) return { data: Buffer.from([]) };
|
||||
|
||||
voice = voice.split(' ').slice(1).join(' ');
|
||||
const voiceData = this.voices.find((voiceDesc) => voiceDesc.Name == voice);
|
||||
if (!voiceData) return {};
|
||||
|
||||
const bestEngine = this.getBestEngine(voiceData);
|
||||
if (!bestEngine) return {};
|
||||
|
||||
const cmd = new SynthesizeSpeechCommand({
|
||||
Engine: bestEngine,
|
||||
LanguageCode: voiceData.LanguageCode,
|
||||
OutputFormat: 'mp3',
|
||||
Text: text,
|
||||
VoiceId: voiceData.Id
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await this.client.send(cmd);
|
||||
if (!res.AudioStream) return {};
|
||||
|
||||
const buffer = Buffer.from(await res.AudioStream.transformToByteArray());
|
||||
|
||||
return { data: buffer };
|
||||
} catch (err) {
|
||||
console.error('AWS Polly gen error:', err);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
canBeUsed(): boolean {
|
||||
if (!config.aws_access_id || !config.aws_access_key) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private getBestEngine(voice: Voice): Engine | null {
|
||||
if (!voice.SupportedEngines || voice.SupportedEngines.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const supportedSet = new Set(voice.SupportedEngines);
|
||||
return ENGINE_PRIORITY.find((engine) => supportedSet.has(engine)) || null;
|
||||
}
|
||||
}
|
||||
|
||||
export default new PollyTTS();
|
||||
@@ -0,0 +1,115 @@
|
||||
import { config } from '../../utils/config';
|
||||
import { TTSModule, TTSResponse } from '../tts';
|
||||
|
||||
import * as https from 'https';
|
||||
import * as zlib from 'zlib';
|
||||
|
||||
import TIKTOK_TTS_VOICES from './tiktok_voices.json';
|
||||
const TIKTOK_API_ENDPOINT = 'api16-normal-v6.tiktokv.com';
|
||||
|
||||
class TikTokTTS implements TTSModule {
|
||||
public name: string = 'TikTok';
|
||||
public defaultVoice: string = 'en_us_001';
|
||||
|
||||
async getVoices(): Promise<Array<string> | undefined> {
|
||||
return TIKTOK_TTS_VOICES.voices;
|
||||
}
|
||||
|
||||
async generate(voice: string, text: string): Promise<TTSResponse> {
|
||||
const reqText = encodeURIComponent(text);
|
||||
const path = `/media/api/text/speech/invoke/?text_speaker=${voice}&req_text=${reqText}&speaker_map_type=0&aid=1233`;
|
||||
|
||||
const options: https.RequestOptions = {
|
||||
hostname: TIKTOK_API_ENDPOINT,
|
||||
path: path,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'User-Agent':
|
||||
'com.zhiliaoapp.musically/2022600030 (Linux; U; Android 7.1.2; es_ES; SM-G988N; Build/NRD90M;tt-ok/3.12.13.1)',
|
||||
Cookie: `sessionid=${config.tts_tiktok_sessionid}`,
|
||||
'Accept-Encoding': 'gzip,deflate,compress',
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const req = https.request(options, (res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
|
||||
const encoding = res.headers['content-encoding'];
|
||||
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const buffer = Buffer.concat(chunks);
|
||||
|
||||
const decompressBuffer = (buf: Buffer): Promise<Buffer> => {
|
||||
return new Promise((decompressResolve, decompressReject) => {
|
||||
if (encoding === 'gzip' || encoding === 'deflate') {
|
||||
zlib.unzip(buf, (err: Error | null, decompressed: Buffer) => {
|
||||
if (err) decompressReject(err);
|
||||
else decompressResolve(decompressed);
|
||||
});
|
||||
} else {
|
||||
decompressResolve(buf);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
decompressBuffer(buffer)
|
||||
.then((decompressed) => {
|
||||
const result = JSON.parse(decompressed.toString());
|
||||
const statusCode = result?.status_code;
|
||||
|
||||
if (statusCode !== 0) {
|
||||
const errorMsg = this.handleStatusError(statusCode);
|
||||
return resolve({ error: errorMsg });
|
||||
}
|
||||
|
||||
const voiceStr = result?.data?.v_str;
|
||||
if (!voiceStr) {
|
||||
return resolve({ error: 'No audio data received' });
|
||||
}
|
||||
|
||||
resolve({ data: Buffer.from(voiceStr, 'base64') });
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve({ error: `Decompression/Parse error: ${err.message}` });
|
||||
});
|
||||
} catch (err) {
|
||||
resolve({ error: `Parse error: ${err}` });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (err) => resolve({ error: err.message }));
|
||||
req.on('timeout', () => {
|
||||
req.destroy();
|
||||
resolve({ error: 'timed out' });
|
||||
});
|
||||
|
||||
req.write('');
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
canBeUsed(): boolean {
|
||||
return config.tts_tiktok_sessionid != undefined;
|
||||
}
|
||||
|
||||
handleStatusError(code: number): string {
|
||||
switch (code) {
|
||||
case 1:
|
||||
return 'Session ID may be invalid or expired';
|
||||
case 2:
|
||||
return 'Text is too long';
|
||||
case 4:
|
||||
return 'Invalid voice';
|
||||
case 5:
|
||||
return 'No session id.';
|
||||
}
|
||||
return `Unknown error code: ${code}`;
|
||||
}
|
||||
}
|
||||
|
||||
export default new TikTokTTS();
|
||||
@@ -0,0 +1,99 @@
|
||||
{
|
||||
"voices": [
|
||||
"en_us_ghostface",
|
||||
"en_us_chewbacca",
|
||||
"en_us_c3po",
|
||||
"en_us_stitch",
|
||||
"en_us_stormtrooper",
|
||||
"en_us_rocket",
|
||||
"en_female_madam_leota",
|
||||
"en_male_ghosthost",
|
||||
"en_male_pirate",
|
||||
"en_au_001",
|
||||
"en_au_002",
|
||||
"en_uk_001",
|
||||
"en_uk_003",
|
||||
"en_us_001",
|
||||
"en_us_002",
|
||||
"en_us_006",
|
||||
"en_us_007",
|
||||
"en_us_009",
|
||||
"en_us_010",
|
||||
"en_male_jomboy",
|
||||
"en_male_cody",
|
||||
"en_female_samc",
|
||||
"en_female_makeup",
|
||||
"en_female_richgirl",
|
||||
"en_male_grinch",
|
||||
"en_male_deadpool",
|
||||
"en_male_jarvis",
|
||||
"en_male_ashmagic",
|
||||
"en_male_olantekkers",
|
||||
"en_male_ukneighbor",
|
||||
"en_male_ukbutler",
|
||||
"en_female_shenna",
|
||||
"en_female_pansino",
|
||||
"en_male_trevor",
|
||||
"en_female_betty",
|
||||
"en_male_cupid",
|
||||
"en_female_grandma",
|
||||
"en_male_m2_xhxs_m03_christmas",
|
||||
"en_male_santa_narration",
|
||||
"en_male_sing_deep_jingle",
|
||||
"en_male_santa_effect",
|
||||
"en_female_ht_f08_newyear",
|
||||
"en_male_wizard",
|
||||
"en_female_ht_f08_halloween",
|
||||
"fr_001",
|
||||
"fr_002",
|
||||
"de_001",
|
||||
"de_002",
|
||||
"es_002",
|
||||
"es_mx_002",
|
||||
"br_001",
|
||||
"br_003",
|
||||
"br_004",
|
||||
"br_005",
|
||||
"bp_female_ivete",
|
||||
"bp_female_ludmilla",
|
||||
"pt_female_lhays",
|
||||
"pt_female_laizza",
|
||||
"pt_male_bueno",
|
||||
"id_001",
|
||||
"jp_001",
|
||||
"jp_003",
|
||||
"jp_005",
|
||||
"jp_006",
|
||||
"kr_002",
|
||||
"kr_003",
|
||||
"kr_004",
|
||||
"jp_female_fujicochan",
|
||||
"jp_female_hasegawariona",
|
||||
"jp_male_keiichinakano",
|
||||
"jp_female_oomaeaika",
|
||||
"jp_male_yujinchigusa",
|
||||
"jp_female_shirou",
|
||||
"jp_male_tamawakazuki",
|
||||
"jp_female_kaorishoji",
|
||||
"jp_female_yagishaki",
|
||||
"jp_male_hikakin",
|
||||
"jp_female_rei",
|
||||
"jp_male_shuichiro",
|
||||
"jp_male_matsudake",
|
||||
"jp_female_machikoriiita",
|
||||
"jp_male_matsuo",
|
||||
"jp_male_osada",
|
||||
"en_female_f08_salut_damour",
|
||||
"en_male_m03_lobby",
|
||||
"en_female_f08_warmy_breeze",
|
||||
"en_male_m03_sunshine_soon",
|
||||
"en_female_ht_f08_glorious",
|
||||
"en_male_sing_funny_it_goes_up",
|
||||
"en_male_m2_xhxs_m03_silly",
|
||||
"en_female_ht_f08_wonderful_world",
|
||||
"en_male_sing_funny_thanksgiving",
|
||||
"en_male_narration",
|
||||
"en_male_funny",
|
||||
"en_female_emotional"
|
||||
]
|
||||
}
|
||||
+10
-4
@@ -11,8 +11,10 @@ export interface TTSResponse {
|
||||
|
||||
export interface TTSModule {
|
||||
name: string;
|
||||
defaultVoice?: string;
|
||||
getVoices: () => Promise<Array<string> | undefined>;
|
||||
generate: (voice: string, text: string) => Promise<TTSResponse>;
|
||||
canBeUsed: () => boolean;
|
||||
}
|
||||
|
||||
export class TTSManager {
|
||||
@@ -47,13 +49,17 @@ export class TTSManager {
|
||||
if (!isModule(filePath)) return;
|
||||
|
||||
const modRaw = await import(`file://${filePath}`);
|
||||
|
||||
if (!modRaw || !modRaw.default) {
|
||||
this.log.warning('Invalid module format in %s', filePath);
|
||||
if (!modRaw) {
|
||||
this.log.warning('Mod import failed for %s', filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
const mod = modRaw.default as TTSModule;
|
||||
const mod = modRaw.default?.default || modRaw.default || modRaw;
|
||||
|
||||
if (!mod.name || typeof mod.generate !== 'function') {
|
||||
this.log.warning('Invalid module format in %s', filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
this.log.verbose(`Loaded TTS mode: ${mod.name}`);
|
||||
this.modules.push(mod);
|
||||
|
||||
+10
-5
@@ -6,10 +6,13 @@ export interface Config {
|
||||
tts_default_mode: string | undefined;
|
||||
tts_default_voice: string | undefined;
|
||||
|
||||
tts_azure_key: string | undefined;
|
||||
tts_elevenlabs_key: string | undefined;
|
||||
tts_elevenlabs_refreshtoken: string | undefined;
|
||||
tts_tiktok_sessionid: string | undefined;
|
||||
|
||||
steam_webapi_key: string | undefined;
|
||||
|
||||
aws_access_id: string | undefined;
|
||||
aws_access_key: string | undefined;
|
||||
}
|
||||
|
||||
function loadConfig(): Config {
|
||||
@@ -26,9 +29,11 @@ function loadConfig(): Config {
|
||||
owner_id: process.env.DISCORD_OWNER_ID,
|
||||
tts_default_mode: process.env.DEFAULT_TTS_MODE,
|
||||
tts_default_voice: process.env.DEFAULT_TTS_VOICE,
|
||||
tts_azure_key: process.env.TTS_AZURE_KEY,
|
||||
tts_elevenlabs_key: process.env.TTS_ELEVENLABS_KEY,
|
||||
steam_webapi_key: process.env.STEAM_WEBAPI_KEY
|
||||
tts_elevenlabs_refreshtoken: process.env.TTS_ELEVENLABS_REFRESHTOKEN,
|
||||
steam_webapi_key: process.env.STEAM_WEBAPI_KEY,
|
||||
aws_access_id: process.env.AWS_ACCESS_ID,
|
||||
aws_access_key: process.env.AWS_ACCESS_KEY,
|
||||
tts_tiktok_sessionid: process.env.TTS_TIKTOK_SESSIONID
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user