Heres my code
const {getAudioUrl} = require("google-tts-api")
const langlist = ["en", "id"]
let language = args[0]
if(langlist.find(c => c === language)) {
if(!language) return message.reply("Please enter the language")
if(!args[1]) return message.reply("Please enter the text")
const text = args.slice(1).join(" ")
if(text.length > 200) return message.reply('You cant input a text with over 200 characters')
const voiceChannel = message.member.voice.channel
if(!voiceChannel) return message.reply(" Please enter a voice channel")
const audioURL = await getAudioUrl(text, {
lang: language,
slow: false,
host: 'https://translate.google.com',
timeout: '150000'
})
try {
voiceChannel.join().then(connection => {
const dispatcher = connection.play(audioURL)
dispatcher.on('finish', () => {
voiceChannel.leave()
})
})
}
} else { return message.reply(`That language is not supported, the current supported language : [${langlist.join(", ")}]`); }
catch(err){
console.log(err)
message.channel.send("There is an error")}
}
}
I'm searching on internet but their problem if the try don't have catch, but i already have one.
Your catch statement is outside the if block that has the try block. This is what I did to fix the error:
catch block closer to the try.} that were causing errors.Here's the code:
const {getAudioUrl} = require("google-tts-api")
const langlist = ["en", "id"]
let language = args[0]
if(langlist.find(c => c === language)) {
if(!language) return message.reply("Please enter the language")
if(!args[1]) return message.reply("Please enter the text")
const text = args.slice(1).join(" ")
if(text.length > 200) return message.reply('You cant input a text with over 200 characters')
const voiceChannel = message.member.voice.channel
if(!voiceChannel) return message.reply(" Please enter a voice channel")
const audioURL = await getAudioUrl(text, {
lang: language,
slow: false,
host: 'https://translate.google.com',
timeout: '150000'
})
try {
voiceChannel.join().then(connection => {
const dispatcher = connection.play(audioURL)
dispatcher.on('finish', () => {
voiceChannel.leave()
})
})
}
catch(err){
console.log(err)
message.channel.send("There is an error")
}
} else {
return message.reply(`That language is not supported, the current supported language : [${langlist.join(", ")}]`);
}
A tip to avoid this kind of error: indent properly!
If you had indented all of your code, it would have been so much easier to read and therefore finding the error would have been really quick (and you wouldn't even need to ask Stack Overflow).
"Programs must be written for people to read, and only incidentally for machines to execute." - Harold Abelson
If you make your code easier to read, debugging, rewriting, maintaining, understanding, etc. will become much easier - for you, and everyone else.