Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1.  
  2. const translate = require('google-translate-api');
  3.  
  4. if (command === "translate") {
  5. if (args[0]) {
  6. let from_language = "auto" // default languages
  7. let to_language = "en" // default languages
  8. let tobe_translated = message.content.slice(prefix.length + command.length + 1) // Getting the text
  9. if (args[0].startsWith("from:")) { // Checking if there is a from:language & to:language, this part is not optimized
  10. from_language = args[0].slice(5)
  11. tobe_translated = tobe_translated.slice(args[0].length + 1)
  12. if (args[1].startsWith("to:")) {
  13. to_language = args[1].slice(3)
  14. tobe_translated = tobe_translated.slice(args[1].length + 1) // cutting the from & to from the text
  15. }
  16. } else if (args[0].startsWith("to:")) { // Checking if there is a to:language & from:language, Yes I check 2 times :/
  17. to_language = args[0].slice(3)
  18. tobe_translated = tobe_translated.slice(args[0].length + 1)
  19. if (args[1].startsWith("from:")) {
  20. from_language = args[1].slice(5)
  21. tobe_translated = tobe_translated.slice(args[1].length + 1) // cutting the from & to from the text
  22. }
  23. }
  24. translate(tobe_translated, {
  25. from: from_language,
  26. to: to_language
  27. }).then(res => { // We translate the text
  28. from_language = res.from.language.iso
  29. if (res.from.text.value) tobe_translated = res.from.text.value
  30. final_text = res.text
  31. let translateembed = new Discord.RichEmbed()
  32. .setTitle("Translate") // Optionnal stuff
  33. .setColor(`0x3980b3`) // Optionnal stuff
  34. .setDescription("Bip Bip Boop\nThe internet magic is here") // Optionnal stuff
  35. .addField("`from: " + from_language + "`", "```" + tobe_translated + "```")
  36. .addField("`to: " + to_language + "`", "```" + final_text + "```")
  37. .setThumbnail("https://cdn.dribbble.com/users/1341307/screenshots/3641494/google_translate.gif") // Optionnal stuff
  38. message.channel.send(translateembed)
  39. }).catch(err => {
  40. message.channel.send(":x: Usage: `" + prefix + "translate [from:iso] [to:iso] <some text>` \nThe from: and to: are optional, you can check out <http://bit.ly/ISO_codesWiki> for the iso codes\nExample: ```" + prefix + "translate from:ro to:fr Salut, ce mai faci?```") // Yes, I used Romanian for my example. Do you have any problem?
  41. });
  42. } else {
  43. message.channel.send(":x: Usage: `" + prefix + "translate [from:iso] [to:iso] <some text>` \nThe from: and to: are optional, you can check out <http://bit.ly/ISO_codesWiki> for the iso codes\nExample: ```" + prefix + "translate from:ro to:fr Salut, ce mai faci?```")
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement