Files
luma/src/commands/tts/setmode.ts
T

70 lines
1.8 KiB
TypeScript

import {
AutocompleteInteraction,
ChatInputCommandInteraction,
MessageFlags,
SlashCommandBuilder
} from 'discord.js';
import { Command } from '../../commands';
import { DatabaseManager } from '../../modules/db';
import { TTSManager } from '../../modules/tts';
const builder = new SlashCommandBuilder()
.setName('tts-mode')
.setDescription('Selects a mode to use for TTS')
.addStringOption((opt) =>
opt
.setName('mode')
.setDescription('Which TTS provider to use')
.setAutocomplete(true)
.setRequired(true)
);
const cmd: Command = {
name: builder.name,
builder: builder,
execute: async (interaction: ChatInputCommandInteraction): Promise<void> => {
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
const userData = await DatabaseManager.get.getUser(interaction.user.id);
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) {
await interaction.editReply(`Unknown mode (${modeName})`);
return;
}
await userData.set('tts_mode', modeName);
await userData.save();
interaction.editReply(`TTS mode has been set to: ${modeName}.`);
},
autocomplete: async (interaction: AutocompleteInteraction): Promise<void> => {
const focused = interaction.options.getFocused(true);
if (focused.name != 'mode') return;
const modes = TTSManager.get.getModules();
const filtered: string[] = modes
.filter((mod) => mod.canBeUsed())
.filter((mod) => {
return mod.name
? mod.name.toLowerCase().startsWith(focused.value.toLowerCase())
: undefined;
})
.map((mod) => mod.name)
.slice(0, 25);
await interaction.respond(
filtered.map((choice) => ({ name: choice, value: choice }))
);
}
};
export default cmd;