-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
}); | ||
}); | ||
}, | ||
}; |