Jump to content

Photo

Insane Limits: Mute on Foreign Language - Google Translate API


  • Please log in to reply
26 replies to this topic

#1
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by TMiland*:

Mute on Foreign Language - Google Translate API

This limit will warn/warn/mute if it detects any other language in chat than English! See bottom of post if you want to allow more.

This limit is based on PC9's Chat bad word detection using Google service*

This limit requires: AdKats Advanced In Game Admin and Ban Enforcer and a Google API Key.

Set limit to evaluate OnAnyChat, set action to None

Set first_check to this Code:

Code:
if (player.LastChat.StartsWith("ID_CHAT_") || player.LastChat.StartsWith("/") || player.LastChat.StartsWith("!") || player.LastChat.StartsWith("@") || player.LastChat.StartsWith("_") || player.LastChat.StartsWith(":") || player.LastChat.StartsWith(";"))
    return false;
    String[] words = player.LastChat.Trim().Split(new Char[]{' ','\t',',','.',':','!','_',';','(',')',']','[','"'});
    StringBuilder edited = new StringBuilder();
    foreach (String w in words) {
		// Skip less than 5 characters
		if (w.Length < 5) {
			continue;
		}
        edited.Append(w);
        edited.Append(" ");
    }
    // URL Encode edited string
    String UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
    StringBuilder Result = new StringBuilder();
    String Input = edited.ToString();
    for (int x = 0; x < Input.Length; ++x)
    {
		if (UnreservedChars.IndexOf(Input[x]) != -1)
			Result.Append(Input[x]);
		else
			Result.Append("%").Append(String.Format("{0:X2}", (int)Input[x]));
	}
	String key = "YOUR GOOGLE API KEY";
    bool language = false;
    try {
        WebClient client = new WebClient();
        String json = client.DownloadString("https://www.googleapis.com/language/translate/v2/detect_key=" + key + "&q=" + Result.ToString());
        language = json.Contains("\"language\": \"en\",");
    } catch (Exception e) {
        plugin.ConsoleWrite("Language check failed! Error: " + e);
    }
	return !language;
Second check code:

Code:
String fancy_time = DateTime.Now.ToString("HH:mm:ss");
	String fancy_date = DateTime.Now.ToString("dd-MM-yyyy");
	double count = limit.Activations(player.Name);
	String msg = "none";
	if (count == 1) {
		msg = plugin.R("ATTENTION %k_n%! Write in ENGLISH in chat!");
		plugin.SendPlayerYell(player.Name, msg, 20);
		plugin.SendGlobalMessage(msg);
		plugin.PRoConChat("ADMIN > " + msg);
		plugin.ConsoleWrite("^b^1ADMIN >^0^n " + player.FullName + " tripped the Foreign Language filter! ");
		plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
	}
	if (count == 2) {
		msg = plugin.R("ATTENTION %k_n%! ENGLISH ONLY IN CHAT!");
		plugin.SendPlayerYell(player.Name, msg, 20);
		plugin.SendGlobalMessage(msg);
		plugin.PRoConChat("ADMIN > " + msg);
		plugin.ConsoleWrite("^b^1ADMIN >^0^n " + player.FullName + " tripped the Foreign Language filter! ");
		plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
	}
	else if (count == 3) {
		msg = plugin.R("/@mute " + player.Name + " not writing in ENGLISH in chat!");
		plugin.ServerCommand("admin.say", msg);
		plugin.SendPlayerYell(player.Name, msg, 20);
		plugin.SendGlobalMessage(msg);
		plugin.PRoConChat("ADMIN MUTE > " + msg);
		plugin.ConsoleWrite("^b^1ADMIN MUTE >^0^n " + player.FullName + " using Foreign Language in chat! ");
		plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
	}

	return false;
You need to change Code:
String key = "YOUR GOOGLE API KEY";
with your api key from google: https://console.deve...gle.com/project

If you want to allow more languages, add them to this line like this: Code:
language = json.Contains("\"language\": \"en\",") || json.Contains("\"language\": \"no\",") || json.Contains("\"language\": \"sv\",") || json.Contains("\"language\": \"da\",");
Improvements and fixes are more than welcome, as i have no idea what i am doing!

USE AT YOUR OWN RISK!
* Restored post. It could be that the author is no longer active.

#2
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by ColColonCleaner*:

Interesting. Maybe instead of muting them, you could just slay them every time they continue talking in the other language after the 2 warnings, and kick after X if necessary. This way people don't need AdKats. I like the promotion, but it's really not needed for this functionality.
* Restored post. It could be that the author is no longer active.

#3
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by TMiland*:

Interesting. Maybe instead of muting them, you could just slay them every time they continue talking in the other language after the 2 warnings, and kick after X if necessary. This way people don't need AdKats. I like the promotion, but it's really not needed for this functionality.

I am really found of the mute function in adkats, it's a way to show the bad players themselves out.

If they don't shut up, it's their own fault! :cool:

But of course it can be changed to whatever you'd like. :smile:

Edit:

I currently only run this in virtual, to collect chat and see what false positives it might be.
* Restored post. It could be that the author is no longer active.

#4
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by DarkZerO_AT*:

Was curious about this and tried it out
But whatever someone writes, it says "Player tripped the Foreign Language filter!"
Even if its english or german (which i added)

Running it on virtual currently cause i didnt wanted someone to get punished if it fails

I also wasnt sure what API key to use, so i activated the "Translate API" and created a "Public Server Key" but im not sure if thats correct even tho it seems to work but sometimes returns a "403 Forbidden" error
* Restored post. It could be that the author is no longer active.

#5
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by Tobsen03*:

hmm

Didnīt work ....

1:42:45 78] [Insane Limits] Thread(Threadpool worker): Language check failed! Error: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0
* Restored post. It could be that the author is no longer active.

#6
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by TMiland*:

hmm

Didnīt work ....

1:42:45 78] [Insane Limits] Thread(Threadpool worker): Language check failed! Error: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0

Post your limit? :smile:
* Restored post. It could be that the author is no longer active.

#7
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by Tobsen03*:

First Code :

if (player.LastChat.StartsWith("ID_CHAT_") || player.LastChat.StartsWith("/") || player.LastChat.StartsWith("!") || player.LastChat.StartsWith("@") || player.LastChat.StartsWith("_") || player.LastChat.StartsWith(":") || player.LastChat.StartsWith(";"))
return false;
String[] words = player.LastChat.Trim().Split(new Char[]{' ','\t',',','.',':','!','_',';','(',')',']','[','"'});
StringBuilder edited = new StringBuilder();
foreach (String w in words) {
// Skip less than 5 characters
if (w.Length < 5) {
continue;
}
edited.Append(w);
edited.Append(" ");
}
// URL Encode edited string
String UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789-_.~";
StringBuilder Result = new StringBuilder();
String Input = edited.ToString();
for (int x = 0; x < Input.Length; ++x)
{
if (UnreservedChars.IndexOf(Input[x]) != -1)
Result.Append(Input[x]);
else
Result.Append("%").Append(String.Format("{0:X2}", (int)Input[x]));
}
String key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
bool language = false;
try {
WebClient client = new WebClient();
String json = client.DownloadString("https://www.googleapis.com/language/translate/v2/detect_key=" + key + "&q=" + Result.ToString());
language = json.Contains("\"language\": \"en\",") || json.Contains("\"language\": \"de\",");
} catch (Exception e) {
plugin.ConsoleWrite("Language check failed! Error: " + e);
}
return !language;

Second Code:

String fancy_time = DateTime.Now.ToString("HH:mm:ss");
String fancy_date = DateTime.Now.ToString("dd-MM-yyyy");
double count = limit.Activations(player.Name);
String msg = "none";
if (count == 1) {
msg = plugin.R("ATTENTION %k_n%! Write in GERMAN OR ENGLISH in chat!");
plugin.SendPlayerYell(player.Name, msg, 20);
plugin.SendGlobalMessage(msg);
plugin.PRoConChat("ADMIN > " + msg);
plugin.ConsoleWrite("^b^1ADMIN >^0^n " + player.FullName + " tripped the Foreign Language filter! ");
plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
}
if (count == 2) {
msg = plugin.R("ATTENTION %k_n%! ENGLISH OR GERMAN ONLY IN CHAT!");
plugin.SendPlayerYell(player.Name, msg, 20);
plugin.SendGlobalMessage(msg);
plugin.PRoConChat("ADMIN > " + msg);
plugin.ConsoleWrite("^b^1ADMIN >^0^n " + player.FullName + " tripped the Foreign Language filter! ");
plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
}
else if (count == 3) {
msg = plugin.R("/@mute " + player.Name + " not writing in German or ENGLISH in chat!");
plugin.ServerCommand("admin.say", msg);
plugin.SendPlayerYell(player.Name, msg, 20);
plugin.SendGlobalMessage(msg);
plugin.PRoConChat("ADMIN MUTE > " + msg);
plugin.ConsoleWrite("^b^1ADMIN MUTE >^0^n " + player.FullName + " using Foreign Language in chat! ");
plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
}

return false;


I want to allow German and English......
* Restored post. It could be that the author is no longer active.

#8
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by TMiland*:

Try this as first check code:
Code:
if (player.LastChat.StartsWith("ID_CHAT_") || player.LastChat.StartsWith("/") || player.LastChat.StartsWith("!") || player.LastChat.StartsWith("@") || player.LastChat.StartsWith("_") || player.LastChat.StartsWith(":") || player.LastChat.StartsWith(";"))
    return false;
    String[] words = player.LastChat.Trim().Split(new Char[]{' ','\t',',','.',':','!','_',';','(',')',']','[','"'});
    StringBuilder edited = new StringBuilder();
    foreach (String w in words) {
		// Skip less than 5 characters
		if (w.Length < 5) {
			continue;
		}
        edited.Append(w);
        edited.Append(" ");
    }
    // URL Encode edited string
    String UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
    StringBuilder Result = new StringBuilder();
    String Input = edited.ToString();
    for (int x = 0; x < Input.Length; ++x)
    {
		if (UnreservedChars.IndexOf(Input[x]) != -1)
			Result.Append(Input[x]);
		else
			Result.Append("%").Append(String.Format("{0:X2}", (int)Input[x]));
	}
	String key = "AIzaSyDPvuB_LvXR6E-xxxxxxxxxxxxxxxxxxx";
    bool language = false;
    try {
        WebClient client = new WebClient();
        String json = client.DownloadString("https://www.googleapis.com/language/translate/v2/detect_key=" + key + "&q=" + Result.ToString());
        language = json.Contains("\"language\": \"en\",") || json.Contains("\"language\": \"de\",");
    } catch (Exception e) {
        plugin.ConsoleWrite("Language check failed! Error: " + e);
    }
	return !language;
Let me know when you have it copied, so we can remove you API key from public view. :tongue:

And please use CODE tags when posting your code. :ohmy:
* Restored post. It could be that the author is no longer active.

#9
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by Tobsen03*:

[20:35:06 27] [Insane Limits] Thread(Threadpool worker): ERROR: 1 error compiling Code
[20:35:06 27] [Insane Limits] Thread(Threadpool worker): ERROR: (CS0019, line: 55, column: 71): Operator `||' cannot be applied to operands of type `bool' and `string'
* Restored post. It could be that the author is no longer active.

#10
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by LCARSx64*:

[20:35:06 27] [Insane Limits] Thread(Threadpool worker): ERROR: 1 error compiling Code
[20:35:06 27] [Insane Limits] Thread(Threadpool worker): ERROR: (CS0019, line: 55, column: 71): Operator `||' cannot be applied to operands of type `bool' and `string'

Replace this line:
Code:
language = json.Contains("\"language\": \"en\",") || ("\"language\": \"de\",");
with:
Code:
language = json.Contains("\"language\": \"en\",") || json.Contains("\"language\": \"de\",");

* Restored post. It could be that the author is no longer active.

#11
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by TMiland*:

Replace this line:
Code:

language = json.Contains("\"language\": \"en\",") || ("\"language\": \"de\",");
with:
Code:
language = json.Contains("\"language\": \"en\",") || json.Contains("\"language\": \"de\",");

Ops! Sry about that, thanks LCARSx64 :biggrin:

I have edited the full code.
* Restored post. It could be that the author is no longer active.

#12
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by Tobsen03*:

[23:31:15 05] [Insane Limits] Thread(Threadpool worker): Language check failed! Error: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0
* Restored post. It could be that the author is no longer active.

#13
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by LCARSx64*:

Ops! Sry about that, thanks LCARSx64 :biggrin:

I have edited the full code.

No problem. :smile:
* Restored post. It could be that the author is no longer active.

#14
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by TMiland*:

[23:31:15 05] [Insane Limits] Thread(Threadpool worker): Language check failed! Error: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in :0
at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in :0

Make sure your API key is correctly set up. https://console.deve...gle.com/project

https://myrcon.net/r...2ampd1441978797

Attached Files:

* Restored post. It could be that the author is no longer active.

#15
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by snufduck*:

Getting this error.

[19:43:05 94] [Insane Limits] ADMIN MUTE > [DFS]SnufDuck using Foreign Language in chat!
[19:43:05 94] [Insane Limits] ERROR: unable to append data to file d:\localuser\g281427\Logs/InsaneLimits_foreign_language_api.log
[19:43:05 94] [Insane Limits] EXCEPTION: System.IO.DirectoryNotFoundException: Could not find a part of the path 'd:\localuser\g281427\Logs\InsaneLimits_foreign_la nguage_api.log'
* Restored post. It could be that the author is no longer active.

#16
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by TMiland*:

Getting this error.

[19:43:05 94] [Insane Limits] ADMIN MUTE > [DFS]SnufDuck using Foreign Language in chat!
[19:43:05 94] [Insane Limits] ERROR: unable to append data to file d:\localuser\g281427\Logs/InsaneLimits_foreign_language_api.log
[19:43:05 94] [Insane Limits] EXCEPTION: System.IO.DirectoryNotFoundException: Could not find a part of the path 'd:\localuser\g281427\Logs\InsaneLimits_foreign_la nguage_api.log'

Comment the logfile part out, you do not have the permissions to write to your layer.

Use this for second check:

Code:
String fancy_time = DateTime.Now.ToString("HH:mm:ss");
	String fancy_date = DateTime.Now.ToString("dd-MM-yyyy");
	double count = limit.Activations(player.Name);
	String msg = "none";
	if (count == 1) {
		msg = plugin.R("ATTENTION %k_n%! Write in ENGLISH in chat!");
		plugin.SendPlayerYell(player.Name, msg, 20);
		plugin.SendGlobalMessage(msg);
		plugin.PRoConChat("ADMIN > " + msg);
		plugin.ConsoleWrite("^b^1ADMIN >^0^n " + player.FullName + " tripped the Foreign Language filter! ");
		plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
	}
	if (count == 2) {
		msg = plugin.R("ATTENTION %k_n%! ENGLISH ONLY IN CHAT!");
		plugin.SendPlayerYell(player.Name, msg, 20);
		plugin.SendGlobalMessage(msg);
		plugin.PRoConChat("ADMIN > " + msg);
		plugin.ConsoleWrite("^b^1ADMIN >^0^n " + player.FullName + " tripped the Foreign Language filter! ");
		plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
	}
	else if (count == 3) {
		msg = plugin.R("/@mute " + player.Name + " not writing in ENGLISH in chat!");
		plugin.ServerCommand("admin.say", msg);
		plugin.SendPlayerYell(player.Name, msg, 20);
		plugin.SendGlobalMessage(msg);
		plugin.PRoConChat("ADMIN MUTE > " + msg);
		plugin.ConsoleWrite("^b^1ADMIN MUTE >^0^n " + player.FullName + " using Foreign Language in chat! ");
		//plugin.Log("Logs/InsaneLimits_foreign_language_api.log", plugin.R("[" + fancy_date + "][" + fancy_time + "] %p_n% said: [" + player.LastChat + "]"));
	}

	return false;

* Restored post. It could be that the author is no longer active.

#17
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by SmackDown*:

Hello,
]
I am trying to get this program to work but I keep getting errors. I want this to allow only English Chat, and I am currently running it in Virtual mode.

Whenever some sends text, regardless if its in English or not, this error code pops up:

[06:59:03] [Insane Limits] Language check failed! Error: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at PRoConEvents.LimitEvaluator4.FirstCheck(PlayerInfo Interface player, ServerInfoInterface server, PluginInterface plugin, TeamInfoInterface team1, TeamInfoInterface team2, TeamInfoInterface team3, TeamInfoInterface team4)
[06:59:03] [Insane Limits] WARNING: not yelling player-message to AlistorMcCoy, virtual_mode is on
[06:59:03] [Insane Limits] WARNING: not sending global-message, virtual_mode is on

Now I know absolutely nothing about Google API code. I have gone to the website that you posted a link too. I have set up my account and got an API account number which I put into the code. But I am still getting the above error.

I think I am setting up my API key incorrectly. Can someone give me some advice on how to set it up or help me figure out what is causing this error. My API google page looks like I set it up correctly based on what I am seeing from the above post but for the life of me I cant figure this out.

Any help would be appreciated!
* Restored post. It could be that the author is no longer active.

#18
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by ty_ger07*:

The 403 issue seems to be a permissions issue.

https://msdn.microso....v=vs.110).aspx
http://stackoverflow....-403-forbidden

This may help. Try this:

After
Code:
WebClient client = new WebClient();
Add
Code:
client.UseDefaultCredentials = true;

* Restored post. It could be that the author is no longer active.

#19
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by SmackDown*:

The 403 issue seems to be a permissions issue.

https://msdn.microso....v=vs.110).aspx
http://stackoverflow....-403-forbidden

This may help. Try this:

After
Code:

WebClient client = new WebClient();
Add
Code:
client.UseDefaultCredentials = true;

ty_ger07,

Thank you for taking the time to try to help me with my issue.

I have tried your suggestion and it does not work. I am getting the same errors as before.

I have no idea why this is not working.
* Restored post. It could be that the author is no longer active.

#20
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by ty_ger07*:

Bah! I know almost nothing about any of the C languages. I am sure another person can figure it out.
* Restored post. It could be that the author is no longer active.

#21
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by Mamba334*:

I noticed a bug and wanted to bring this to your attention ...

I have this installed and it is running in Virtual Mode right now.

Someone typed "lol" and it warned him. Just giving you a heads up on this.
* Restored post. It could be that the author is no longer active.

#22
ImportBot

ImportBot

    Advanced Member

  • Friendly Neighborhood Bot
  • 52428 posts
Originally Posted by ty_ger07*:

Bah! I know almost nothing about any of the C languages. I am sure another person can figure it out.

Maybe you can bring this to ColColonCleaner's attention and see if he can help you figure out the 403 error.
* Restored post. It could be that the author is no longer active.

#23
Jhinta

Jhinta

    Newbie

  • Active Members
  • Pip
  • 5 posts

this kinda stil works but it reacts on any words, how to fix it

if (player.LastChat.StartsWith("ID_CHAT_") || player.LastChat.StartsWith("/") || player.LastChat.StartsWith("!") || player.LastChat.StartsWith("@") || player.LastChat.StartsWith("_") || player.LastChat.StartsWith(":") || player.LastChat.StartsWith(";"))
    return false;
    String[] words = player.LastChat.Trim().Split(new Char[]{' ','\t',',','.',':','!','_',';','(',')',']','[','"'});
    StringBuilder edited = new StringBuilder();
    foreach (String w in words) {
// Skip less than 5 characters
if (w.Length < 5) {
continue;
}
        edited.Append(w);
        edited.Append(" ");
    }
    // URL Encode edited string
    String UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
    StringBuilder Result = new StringBuilder();
    String Input = edited.ToString();
    for (int x = 0; x < Input.Length; ++x)
    {
if (UnreservedChars.IndexOf(Input[x]) != -1)
Result.Append(Input[x]);
else
Result.Append("%").Append(String.Format("{0:X2}", (int)Input[x]));
}
String key = "xxxxxxxxxxxxxxxxxxxz94xxxxxlHzk";
    bool language = false;
    try {
        WebClient client = new WebClient();
        String json = client.DownloadString("https://translation.googleapis.com/language/translate/v2/detect?key=" + key + "&q=" + Result.ToString());
        language = json.Contains("\"language\": \"en\",");
    } catch (Exception e) {
        plugin.ConsoleWrite("Language check failed! Error: " + e);
    }


return !language; // for Actions


#24
Jhinta

Jhinta

    Newbie

  • Active Members
  • Pip
  • 5 posts

this works, but to make it more effective , some times is fals detect.

if (player.LastChat.StartsWith("ID_CHAT_") || player.LastChat.StartsWith("/") || player.LastChat.StartsWith("!") || player.LastChat.StartsWith("@") || player.LastChat.StartsWith("_") || player.LastChat.StartsWith(":") || player.LastChat.StartsWith(";"))
    return false;
    String[] words = player.LastChat.Trim().Split(new Char[]{' ','\t',',','.',':','!','_',';','(',')',']','[','"'});
    StringBuilder edited = new StringBuilder();
    foreach (String w in words) {
		// Skip less than 5 characters
		if (w.Length < 5) {
			continue;
		}
        edited.Append(w);
        edited.Append(" ");
    }
    // URL Encode edited string
    String UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
    StringBuilder Result = new StringBuilder();
    String Input = edited.ToString();
    for (int x = 0; x < Input.Length; ++x)
    {
		if (UnreservedChars.IndexOf(Input[x]) != -1)
			Result.Append(Input[x]);
		else
			Result.Append("%").Append(String.Format("{0:X2}", (int)Input[x]));
	}
	String key = "xxxxxxxxxxxxxxXOlHzk";
    bool language = false;
	try {
        WebClient client = new WebClient();
        String json = client.DownloadString("https://translation.googleapis.com/language/translate/v2/detect?key=" + key + "&q=" + Result.ToString());
        language = json.Contains("\"language\": \"en\",");
	} catch (Exception e) {
        plugin.ConsoleWrite("Language check failed! Error: " + e);
    }
	if (!language) {
		//FOR DEBUGGING plugin.ConsoleWrite("Language oke");
        return false;
    } else {
		//FOR DEBUGGING plugin.ConsoleWrite("Language bad");
		return true; // for Actions
	}

Edited by Jhinta, 03 December 2019 - 06:11 PM.


#25
Jhinta

Jhinta

    Newbie

  • Active Members
  • Pip
  • 5 posts

i  have found the problem, try opening the json link and keep refressing and look at where "en" is located -> no , or with , 

 

language = json.Contains("\"language\": \"en\"") || json.Contains("\"language\": \"und\""); // a good option?


Edited by Jhinta, 04 December 2019 - 06:18 PM.


#26
Jhinta

Jhinta

    Newbie

  • Active Members
  • Pip
  • 5 posts

also remove and add

 

// Skip less than 5 characters
//if (w.Length < 5) {
//continue;
//}
if (plugin.isInList(w, "good_words")) {
            continue;
        }

Edited by Jhinta, 05 December 2019 - 11:28 AM.


#27
Jhinta

Jhinta

    Newbie

  • Active Members
  • Pip
  • 5 posts
if (player.LastChat.StartsWith("ID_CHAT_") || player.LastChat.StartsWith("/") || player.LastChat.StartsWith("!") || player.LastChat.StartsWith("@") || player.LastChat.StartsWith("_") || player.LastChat.StartsWith(":") || player.LastChat.StartsWith(";"))
    return false;
    String[] words = player.LastChat.Trim().Split(new Char[]{' ','\t',',','.',':','!','_',';','(',')',']','[','"'});
    StringBuilder edited = new StringBuilder();
    foreach (String w in words) {
		//Skip less than 5 characters // needs fixing dont count 1 word but full line
		//if (w.Length < 3) {
			//continue;
		//}
		if (plugin.isInList(w, "good_words")) {
            continue;
        }
        edited.Append(w);
        edited.Append(" ");
    }
    // URL Encode edited string
    String UnreservedChars = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxWXYZ0123456789-_.~";
    StringBuilder Result = new StringBuilder();
    String Input = edited.ToString();
    for (int x = 0; x < Input.Length; ++x)
    {
		if (UnreservedChars.IndexOf(Input[x]) != -1)
			Result.Append(Input[x]);
		else
			Result.Append("%").Append(String.Format("{0:X2}", (int)Input[x]));
	}
	String key = "AIzaSyAuGyJ_VtlwE1rixQnvNz949Rq2zXOlHzk";
    bool language = false;
    try {
        WebClient client = new WebClient();
        String json = client.DownloadString("https://translation.googleapis.com/language/translate/v2/detect?key=" + key + "&q=" + Result.ToString());
        language = json.Contains("\"language\": \"pl\"")|| json.Contains("\"language\": \"ru\"")|| json.Contains("\"language\": \"pl\",")|| json.Contains("\"language\": \"ru\",") || json.Contains("\"language\": \"de\",") || json.Contains("\"language\": \"de\"");
		//plugin.ConsoleWrite(player.FullName + ":" + Result.ToString() + json); //enable for beter info
		
		if (language == false) {
			//plugin.ConsoleWrite("test false"); //enable for some debugging
			{language = false;}
		} else if (language == true){
			//plugin.ConsoleWrite("test true"); //enable for some debugging
			{language = true;}
		}
		} catch (Exception e) {
			plugin.ConsoleWrite("Language check failed! Error: " + e);
		}
		
	return language;

Fully working code , add lang witch you dont want (dont depend if eng true!) also every lang need to be added 2x see code div


Edited by Jhinta, 06 December 2019 - 05:53 PM.



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users