Skip to content

Commit

Permalink
rework tts
Browse files Browse the repository at this point in the history
  • Loading branch information
OOF2510 committed Dec 22, 2023
1 parent d485668 commit aaffccf
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions cmds/misc/tts.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,43 @@
const { SlashCommandBuilder } = require("discord.js");
const { CommandInteraction, Client } = require("discord.js"),
Sequelize = require("sequelize");
const { existsSync, mkdirSync } = require("fs");
const { promisify } = require("util");
const exec = promisify(require("child_process").exec);
const { CommandInteraction, Client } = require("discord.js");
const voice = require("@discordjs/voice");

module.exports = {
data: new SlashCommandBuilder()
.setName("tts")
.setDescription("speaks given message in vc")
.setDescription("Speaks given message in vc")
.addStringOption((option) =>
option.setName("text").setDescription("Text to say").setRequired(true)
),
/**
* Executes the command
* @param {CommandInteraction} interaction
* @param {Client} client
* @param {*} config
* @param {Sequelize} db
* @param {Array} allowed
*/
async execute(interaction, client, config, db, allowed) {
let msg = interaction;
let message = interaction.options.getString("text");
let guild = interaction.guild;
const msg = interaction;
const message = interaction.options.getString("text");
const guild = interaction.guild;

if (!msg.member.voice.channel)
return msg.reply("You must be in a voice channel to do that!");

if (!existsSync("./temp")) {
mkdirSync("./temp");
}

let timeStamp = new Date();
let filename = "./temp/" + guild.name + `-` + timeStamp + ".mp3";

message = message.replaceAll("'", "");

await msg.deferReply({ ephemeral: true });

await exec(`gtts-cli '${message}' --output "${filename}"`);

const channelID = msg.member.voice.channelId;
const Channel = client.channels.cache.get(channelID);

const player = voice.createAudioPlayer();

const connection = voice.joinVoiceChannel({
channelId: channelID,
guildId: Channel.guild.id,
adapterCreator: Channel.guild.voiceAdapterCreator,
});

const resource = voice.createAudioResource(filename);
const player = voice.createAudioPlayer();

const resource = voice.createAudioResource(
`https://translate.google.com/translate_tts?tl=en&q=${encodeURIComponent(
message
)}`,
{
inlineVolume: true,
}
);
player.play(resource);

connection.subscribe(player);
Expand All @@ -66,6 +51,7 @@ module.exports = {

player.on(voice.AudioPlayerStatus.Idle, async () => {
player.stop();
connection.destroy();
});

msg.editReply("I have spoken!");
Expand Down

0 comments on commit aaffccf

Please sign in to comment.