Chat Filter example

Chat Filter example

Postby dzienny » 05 Jan 2012, 16:45

Here's a simple code that adds '#' sign in the begging of every text message. Its purpose is to show how to use the new event feature. Now you can create a bad word, spam or any other kind of text filter.

Code: Select all
using System;

namespace MCDzienny
{
   public class CmdChatFilter : Command
   {
      // Don't change it.
      public override string name { get { return ""; } }
      public override string shortcut { get { return ""; } }
      public override string type { get { return ""; } }
      public override bool museumUsable { get { return false; } }
      public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }

      // Init is executed when the code is loaded.
      public override void Init()
      {
         // You tell the server to send arguments to FilterChat method when PlayerChatEvent occures.
         Player.PlayerChatEvent += FilterChat;
      }

      // Here's the place where you want to write your filter code.
      // You get Player object that describes the player who tries to send a message,
      // string message is the message that is about to be send,
      // bool stopIt is false by default, and if you set it to true the message will not be send.
      private void FilterChat(Player p, ref string message, ref bool stopIt)
      {
         // You may, but you don't have to copy 'message' value to local variable.
         // You may as well perform changes on 'message'.
         string changedMessage = message;
                   
         // Hash sign is added to the beginning of the message.
         changedMessage = "#" + changedMessage;
         
         // 'message' value is now changed to 'changedMessage' value.
         message = changedMessage;
      }

      // Don't change it.
      public override void Help(Player p) { }
      public override void Use(Player p, string message) { }
   }
}
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Chat Filter example

Postby Ultima » 05 Jan 2012, 17:23

Sweet :)
Time to make a badword/caps filter 8-)

The only request i really wanted :mrgreen: (and a working hack-detection :mrgreen: )
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: Chat Filter example

Postby Lapito » 01 Apr 2012, 00:07

Can anyone help me with the Chat Filter please?
Lapito
 
Posts: 24
Joined: 08 Feb 2011, 00:17

Re: Chat Filter example

Postby Ultima » 02 Apr 2012, 16:16

I will post a working chat-filter when i am home.
It's not perfect but it works :lol:
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: Chat Filter example

Postby Lapito » 03 Apr 2012, 03:10

Oh thanks :D
Lapito
 
Posts: 24
Joined: 08 Feb 2011, 00:17

Re: Chat Filter example

Postby Ultima » 03 Apr 2012, 15:33

Code: Select all
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using MCDzienny;

namespace MCDzienny
{
    public class CmdChatFilter : Command
    {
        public override string name { get { return "cf"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return ""; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
        private static string Caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static int maxcaps = 5;

        public override void Init()
        {
            Player.PlayerChatEvent += FilterChat;
        }

        private void FilterChat(Player p, ref string message, ref bool stopIt)
        {

            //Check for caps
            int caps = 0;
            string changedMessage = message;
            foreach (char c in changedMessage)
            {
                foreach (char s in Caps)
                {
                    if (c == s)
                    {
                        caps++;
                    }
                }
            }

            string msg;
            if (caps > maxcaps)
            {
                msg = changedMessage.ToLower();
            }
            else
            {
                msg = changedMessage;
            }

            //Check for bad words
            string[] strBadWords = new string[] {
                            "duck",
                            "poo",
            };
            foreach (string badWord in strBadWords)
            {
                msg = msg.Replace(badWord, "***");
            }         
            message = msg;
        }


        public override void Help(Player p)
        { }
        public override void Use(Player p, string message)
        { }
    }
}


The words duck and poo will be replaced by ***.
So if u write duck it will become ***
If u write ducks it will become ***s

Also, if a sentece has more then 5 capitical letters it will change everyone to lowercase.
U can change this in the top at maxcaps.

It's not perfect but it works.
U can't compile this in MCDzienny, u need windows c# studio. (its free)
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: Chat Filter example

Postby Lapito » 03 Apr 2012, 16:15

Thank you so much. You are a programmer :D
Lapito
 
Posts: 24
Joined: 08 Feb 2011, 00:17

Re: Chat Filter example

Postby Ultima » 04 Apr 2012, 08:48

C# is not my specialty unlike java/php/mysql :mrgreen:
I could make it better/nicer with functions but i made this in 5min and never looked back on it :P
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Re: Chat Filter example

Postby creativemagic » 08 Apr 2012, 18:59

U can't compile this in MCDzienny, u need windows c# studio. (its free)

I got C# studio, and copied and pasted the filter into a Class Library, How do I compile/convert it to a .dll file?
Also, If you wanted to add more words to the filter, would you just have to edit this part?
Code: Select all
   //Check for bad words
                string[] strBadWords = new string[] {
                                "duck",
                                "poo",
                };

The edited part would be like this:
Code: Select all
  //Check for bad words
                string[] strBadWords = new string[] {
                                "duck",
                                "poo",
                                "lag",
                                "LAG",
                                "dog",
                                "hamster",
                };

Would that work?
Owner of...
[Exclusive] Lava Survival
User avatar
creativemagic
 
Posts: 214
Joined: 12 Nov 2011, 20:26

Re: Chat Filter example

Postby Ultima » 08 Apr 2012, 19:08

That would work yes.

As for the "How to compile in C#", read this: viewtopic.php?f=21&t=984
User avatar
Ultima
 
Posts: 953
Joined: 19 Aug 2011, 23:45

Next

Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 1 guest

cron