Page 1 of 3

Chat Filter example

PostPosted: 05 Jan 2012, 16:45
by dzienny
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) { }
   }
}

Re: Chat Filter example

PostPosted: 05 Jan 2012, 17:23
by Ultima
Sweet :)
Time to make a badword/caps filter 8-)

The only request i really wanted :mrgreen: (and a working hack-detection :mrgreen: )

Re: Chat Filter example

PostPosted: 01 Apr 2012, 00:07
by Lapito
Can anyone help me with the Chat Filter please?

Re: Chat Filter example

PostPosted: 02 Apr 2012, 16:16
by Ultima
I will post a working chat-filter when i am home.
It's not perfect but it works :lol:

Re: Chat Filter example

PostPosted: 03 Apr 2012, 03:10
by Lapito
Oh thanks :D

Re: Chat Filter example

PostPosted: 03 Apr 2012, 15:33
by Ultima
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)

Re: Chat Filter example

PostPosted: 03 Apr 2012, 16:15
by Lapito
Thank you so much. You are a programmer :D

Re: Chat Filter example

PostPosted: 04 Apr 2012, 08:48
by Ultima
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

Re: Chat Filter example

PostPosted: 08 Apr 2012, 18:59
by creativemagic
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?

Re: Chat Filter example

PostPosted: 08 Apr 2012, 19:08
by Ultima
That would work yes.

As for the "How to compile in C#", read this: viewtopic.php?f=21&t=984