twitch_bot/commands/translate.js

42 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-02-05 16:36:37 +01:00
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(
2023-02-06 15:02:34 +01:00
`https://simplytranslate.pussthecat.org/api/translate/?engine=google&to=${encodeURIComponent(
2023-02-05 16:36:37 +01:00
splitted[2]
2023-02-06 15:02:34 +01:00
)}&text=${encodeURIComponent(splitted.slice(3).join(" "))}`
2023-02-05 16:36:37 +01:00
)
.then((res) => res.json())
2023-02-11 13:48:05 +01:00
.catch((err) => console.error("Error with translating: " + err.message));
2023-02-05 16:36:37 +01:00
if (!data) {
await client.say(
msg.channelName,
`${msg.displayName}, invalid country code or text`
);
return;
}
2023-02-11 13:48:05 +01:00
await client.say(
msg.channelName,
`${msg.displayName}, Translation of ${splitted
.slice(3)
.join(" ")} to lang ${splitted[2]}: '${data["translated-text"]}'`
);
2023-02-05 16:36:37 +01:00
},
};
module.exports = { command };