Skip to content

Commit

Permalink
fix tts maybeeee
Browse files Browse the repository at this point in the history
  • Loading branch information
OOF2510 committed Dec 22, 2023
1 parent 71affa8 commit 60aba16
Showing 1 changed file with 51 additions and 33 deletions.
84 changes: 51 additions & 33 deletions cmds/misc/tts.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,77 @@
const { SlashCommandBuilder } = require("discord.js");
const { CommandInteraction, Client } = require("discord.js");
const { existsSync, mkdirSync } = require("fs");
const { promisify } = require("util");
const exec = promisify(require("child_process").exec);
const voice = require("@discordjs/voice");
const say = require("say");

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) {
const msg = interaction;
const message = interaction.options.getString("text");
const guild = interaction.guild;
let msg = interaction;
let message = interaction.options.getString("text");
let guild = interaction.guild;

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

await msg.deferReply({ ephemeral: true });
if (!existsSync("./temp")) {
mkdirSync("./temp");
}

const channelID = msg.member.voice.channelId;
const Channel = client.channels.cache.get(channelID);
let timeStamp = new Date();
let filename = "./temp/" + guild.name + `-` + timeStamp + ".wav";

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

const player = voice.createAudioPlayer();
await msg.deferReply({ ephemeral: true });

const resource = voice.createAudioResource(
`https://translate.google.com/translate_tts?tl=en&q=${encodeURIComponent(
message
)}`,
{
inlineVolume: true,
say.export(message, null, 1.0, filename, async (err) => {
if (err) {
console.error(err);
return msg.editReply("Error occurred while generating audio.");
}
);
player.play(resource);

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

player.on("error", (error) => {
console.error(
`Error: ${error.message} with resource ${error.resource.metadata.title}`
);
player.stop();
});
const player = voice.createAudioPlayer();
const connection = voice.joinVoiceChannel({
channelId: channelID,
guildId: Channel.guild.id,
adapterCreator: Channel.guild.voiceAdapterCreator,
});

// player.on(voice.AudioPlayerStatus.Idle, async () => {
// player.stop();
// connection.destroy();
// });
const resource = voice.createAudioResource(filename);
player.play(resource);

msg.editReply("I have spoken!");
connection.subscribe(player);

player.on("error", (error) => {
console.error(
`Error: ${error.message} with resource ${error.resource.metadata.title}`
);
player.stop();
});

player.on(voice.AudioPlayerStatus.Idle, async () => {
player.stop();
msg.editReply("I have spoken!");
});
});
},
};

0 comments on commit 60aba16

Please sign in to comment.