语言和字符转换为英文键盘

时间:2020-09-02 22:11:34

标签: javascript string discord discord.js bots

因此,简而言之,我基本上是在不和谐中制造机器人,我需要检查一些单词。没问题,除了现在,用户可以简单地使用非英语键盘上的字符并绕过检查器。有什么简单的方法可以获取任何字符串并将其内容转换为英文键盘字符?预先感谢!

1 个答案:

答案 0 :(得分:2)

DiscordJS似乎正在NodeJS上运行-这就是我们可以做的。 这是网站上发布的示例代码,但是我们可以将其用于您的项目。

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');

使用此代码后,您可以使用Google Translate API之类的API来处理每个要处理的单词并将其传递给它,然后等待响应。

这是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();

如果将翻译过程与msg.content结合使用,则应该用另一种语言来作为脏话。

这是一个例子(我还没有测试过,但可以尝试一下): 您将需要Google API帐户/密钥等。因此,请阅读他们的说明以进行设置。

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');