【问题标题】:Language & Char conversion to english keyboard语言和字符转换为英文键盘
【发布时间】:2020-12-22 03:12:36
【问题描述】:

所以基本上简而言之,我在 discord 上制作了一个机器人,并且有几句话我需要审查。没问题,除了现在用户可以简单地使用非英文键盘上的字符,绕过审查员。有没有一种简单的方法可以获取任何字符串并将其内容转换为英文键盘字符?提前致谢!

【问题讨论】:

    标签: javascript string discord discord.js bots


    【解决方案1】:

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

    【讨论】:

    • 我不太确定 Discord.js 的实现是如何工作的,但您需要通过 node.js 安装 Google Translate API 模块。如果我今天下班后有一些额外的时间,我会在需要时帮助你。
    • 我有点陌生和困惑,你能提供一个使用 discord.js 的示例吗?
    • 你去@HrittikChatterjee,玩得开心学习编程:) 这将是一次伟大的冒险。如果您有任何问题,请告诉我。
    • 问题是如果有人发送这样的消息:gyazo.com/abe2d68933ea77ad3f3a36ca3aed46ea 它不会被翻译成英文键盘
    • 正确,对于常规语言翻译,它可以工作。你有一个更大的问题要解决,超出了这个问题的范围。您可能需要使用 AI 来确定哪些是带有这些字符的脏话/哪些不是。在本网站上回答问题需要很长时间。研究机器学习 :) 某处可能有一个 NodeJS 机器学习库。
    猜你喜欢
    • 2011-01-22
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2013-01-03
    相关资源
    最近更新 更多