Language & Char conversion to english keyboard

Viewed 156

So basically in short im making a bot on discord and there are a few words that I need to censor. No problem, except now users can simply use characters from keyboards that are not english, and bypass the censors. Is there a simple way I can take any string and convert its contents to english keyboard characters? Thanks in advance!

1 Answers

It seems like DiscordJS is running on NodeJS - so here's what we can do. Here is the example code posted on the website, but we can use it for your project.

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'swearword') {
    msg.reply('naughty!');
  }
});

client.login('token');

With this code in place, you can use an API like Google Translate API to take every word that is processed and pass it to it, and await a response.

Here is the sample provided by Google:

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';

// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;

// Instantiates a client
const translate = new Translate({projectId});

async function quickStart() {
  // The text to translate
  const text = 'Hello, world!';

  // The target language
  const target = 'ru';

  // Translates some text into Russian
  const [translation] = await translate.translate(text, target);
  console.log(`Text: ${text}`);
  console.log(`Translation: ${translation}`);
}

quickStart();

If you combine the translation process along with msg.content you should get a swearword in another language.

Here's an example (I havent tested this but play around with it): You will need Google API account / key etc. So please read their instructions on how to set it up.

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';

// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;

// Instantiates a client
const translate = new Translate({projectId});

var translation = "";

client.on('message', msg => {
      // Translate msg.content
    
      // The target language (i think english is en, you need to check)
      const target = 'en';
    
      // Translates some text into English (i think)
      translation = await translate.translate(msg.content, target);
      
      if (translation === 'swearword') {
        msg.reply('naughty!');
      }
});

client.login('token');
Related