FemScoreboard/discord/commands/tts/tts.ts
2024-05-11 05:15:45 +00:00

44 lines
1.2 KiB
TypeScript

import {
AttachmentBuilder,
ChatInputCommandInteraction,
SlashCommandBuilder
} from 'discord.js';
import 'dotenv/config';
import { logError } from '../../../logging';
import { requestTTSResponse } from '../../util';
const config = {
ttsSettings: {
pitch_change_oct: 1,
pitch_change_sem: 0
}
};
async function ttsCommand(interaction: ChatInputCommandInteraction)
{
const text = interaction.options.getString('text');
await interaction.reply(`generating audio for "${text}"...`);
try {
const audio = await requestTTSResponse(text);
const audioBuf = await audio.arrayBuffer();
const audioFile = new AttachmentBuilder(Buffer.from(audioBuf)).setName('mikuified.wav');
await interaction.editReply({
files: [audioFile]
});
} catch (err) {
await interaction.editReply(`Error: ${err}`);
logError(`Error while generating TTS: ${err}`);
}
}
export = {
data: new SlashCommandBuilder()
.setName('tts')
.setDescription('Read text in Miku\'s voice')
.addStringOption(
opt => opt.setName('text').setDescription('Text').setRequired(true)
),
execute: ttsCommand,
config: config
};