add translate command

This commit is contained in:
Joonas 2023-02-05 17:36:37 +02:00
parent 3a217680f8
commit c59ce44a1b
1 changed files with 43 additions and 0 deletions

43
commands/translate.js Normal file
View File

@ -0,0 +1,43 @@
const command = {
name: "translate",
desc: "translate something",
restricted: false,
mod: false,
run: async (client, msg, splitted) => {
if (!splitted[2] || !splitted[3]) {
await client.say(
msg.channelName,
`${msg.displayName}, needed <country code> <text to be translated>`
);
return;
}
const data = await fetch(
`https://simplytranslate.org/api/translate/?engine=google&to=${
splitted[2]
}&text=${splitted.slice(3).join(" ")}`
)
.then((res) => res.json())
.catch((err) => console.log(err));
if (!data) {
await client.say(
msg.channelName,
`${msg.displayName}, invalid country code or text`
);
return;
}
await client
.say(
msg.channelName,
`${msg.displayName}, Translation of ${splitted
.slice(3)
.join(" ")} to lang ${splitted[2]}: '${data["translated-text"]}'`
)
.catch((err) => console.log(err));
},
};
module.exports = { command };