FemScoreboard/discord/commands/tts/tts.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-05-09 16:20:14 -07:00
import {
AttachmentBuilder,
ChatInputCommandInteraction,
SlashCommandBuilder
} from 'discord.js';
import 'dotenv/config';
import { logError, logInfo, logWarn } 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
};