Discord: Auto-Translate

Translates messages in other languages to English.

// ==UserScript==
// @name         Discord: Auto-Translate
// @description  Translates messages in other languages to English.
// @version      1.1
// @author       Midnight
// @namespace    https://google.com
// @match        *://*/*
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
  "use strict";

  // Get the Discord client.
  var Discord = require("discord.js");

  // Create a new Discord client.
  var client = new Discord.Client();

  // When the client is ready, start the auto-translator.
  client.on("ready", async () => {
    // Create a new Google Translate client.
    var translate = new google.translate.Translate();

    // Start listening for messages.
    client.on("message", async (message) => {
      // If the message is not in English, translate it.
      if (message.content.split(" ").some(word => !/[a-zA-Z]+/.test(word))) {
        // Get the translation of the message.
        var translation = await translate.translate(message.content, {
          target: "en",
        });

        // Send the translation to the channel.
        message.channel.send(translation);
      }
    });
  });

})();