[PRIVMSG] user-type doesn't work

Hey Community,
I’m writing an IRC-Bot atm. and I’m at the point receiving PRIVMSG’s.
My problem is the following :

@color=#0D4200;display-name=TWITCH_UserNaME;emotes=25:0-4,12-16/1902:6-10;subscriber=0;turbo=1;user-type=global_mod :twitch_username!twitch_username@twitch_username.tmi.twitch.tv PRIVMSG #channel :Kappa Keepo Kappa

every time i recieve that user-type its Empty…

Anyone maybe know how to fix that ?

[ How i get the user-type ]

var userTypeMatch = Regex.Match(message, "(user-type=mod|global_mod|admin|staff)");
var userType = userTypeMatch.Value.Replace("user-type=", "").Trim();

Just try it with basic RegEx

message.match(new RegExp("user-type" + "(.*)" + ":")).trim;

Hey, thanks for the quick reply!
But this is also not working, in the message it self without Regex and/or splitting its always user-type= :

Oh, I misunderstood your point. If no user-type is given I would suppose it’s just a “normal” user as you see here:
Twitch v3 Chat

Yeah, but it’s also Empty if the sender is an Moderator^^ that is something i dont understand :smiley:

edit: I tested both regex and they both fail. The first because you are using | incorrectly, and the second because it is too greedy.

short-term fix for your immediate problem:
user-type=(mod|global_mod|admin|staff)?
or if you want to capture all, not just match:
(user-type=(?:mod|global_mod|admin|staff)?)

It is not recommended to use regex to parse IRC messages. Use a tokenizer instead. Twisted has been suggested here before: http://twistedmatrix.com/trac/browser/tags/releases/twisted-15.0.0/twisted/words/protocols/irc.py#L75

or you can make your own.

Its not because of the Regex but i wrote an Message Parser now :

    public static IrcMessage ParseIrcMessage(string rawMessage)
    {
        var prefix = "";
        var arguments = new Stack<string[]>();

        if (string.IsNullOrEmpty(rawMessage))
            throw new BadIrcMessageException("Message is empty.");

        var hasPrefix = rawMessage[0] == ':';
        if (hasPrefix)
            prefix = rawMessage.Substring(rawMessage.IndexOf(':'), rawMessage.IndexOf(' ', 1)).Replace(":", "").Trim();
        if(rawMessage.IndexOf(" :") != -1)
        {
            var trailing = rawMessage.Substring(0, rawMessage.IndexOf(" :"));
            var message = rawMessage.Substring(rawMessage.IndexOf(" :"));

            string[] argumentArray = new string[0];
            Array.Resize(ref argumentArray, trailing.Split().Length + 1);

            argumentArray.For((i) => {
                if ((i + 1) > trailing.Split().Count())
                    argumentArray[i] = message.Trim().Replace(":", "");
                else argumentArray[i] = trailing.Split()[i];
            });

            arguments.Push(argumentArray);
        } else {
            arguments.Push(rawMessage.Split());
        }

        var command = ((hasPrefix) ? arguments.Peek()[1] : arguments.Peek()[0]).Trim();
        return new IrcMessage(prefix, command, arguments.Pop(), hasPrefix);
    }

Now it work’s, thank you all for you’r help!

You can use @Sunspots method of splitting the “prefix”

This method does not break with changes to tags or their order.

Ah, thank you again, will implement that! nice community over here :sunny: :blush: :smile:

1 Like

So, i tryed to implement it in C#, so if anyone needs an IRC-Message-Parser Method for C# :
(Works totally fine now)

Message Parser

    public static IrcMessage ParseIrcMessage(string rawMessage)
    {
        var prefix = "";
        var arguments = new Stack<string[]>();
        var message = "";

        if (string.IsNullOrEmpty(rawMessage))
            throw new BadIrcMessageException("Message is empty.");

        var hasPrefix = rawMessage[0] == ':' && rawMessage[0] != '@';

        if (hasPrefix)
            prefix = rawMessage.Substring(rawMessage.IndexOf(':'), rawMessage.IndexOf(' ', 1)).Replace(":", "").Trim();
        if(rawMessage.IndexOf(" :") != -1)
        {
            string[] argumentArray = new string[0];
            string[] singleDatas = new string[0];
            if (!hasPrefix)
            {
                var dataConstants = rawMessage.Substring(rawMessage.IndexOf('@'), rawMessage.IndexOf(" :"));
                singleDatas = dataConstants.Split(';');

                Array.Resize(ref argumentArray, (argumentArray.Length + singleDatas.Length));

                argumentArray.For((i) => {
                    argumentArray[i] = singleDatas[i];
                });

                rawMessage = rawMessage.Remove(rawMessage.IndexOf('@'), rawMessage.IndexOf(" :")).Trim();

                var parsedMessage = ParseIrcMessage(rawMessage);
                parsedMessage.MessageData = argumentArray.ToList<string>();
                return parsedMessage;
            }

            var trailing = rawMessage.Substring(0, rawMessage.IndexOf(" :"));
            message = rawMessage.Substring(rawMessage.IndexOf(" :"));

            var trailingLength = trailing.Split().Length;
            var splittedTrailing = trailing.Split();

            Array.Resize(ref argumentArray, (argumentArray.Length + trailingLength) + 1);
            argumentArray.For((i) => {
                if ((i + 1) > trailingLength)
                    argumentArray[i] = message.Trim().Replace(":", "");
                else argumentArray[i] = splittedTrailing[i];
            }, trailingLength + 1);
            
            arguments.Push(argumentArray);
        } else {
            arguments.Push(rawMessage.Split());
        }
        var command = ((hasPrefix) ? arguments.Peek()[1] : arguments.Peek()[0]).Trim();

        return new IrcMessage(prefix, command, arguments.Pop(), hasPrefix);
    }

IrcMessage (Model-Class)

public class IrcMessage
{
    private string _prefix;
    private bool _hasPrefix;
    private string _command;
    private List<string> _messageData;
    private string[] _args;

    public IrcMessage(string prefix, string command, string[] args, bool hasPrefix)
    {
        _prefix = prefix;
        _command = command;
        _args = args;
    }

    public string Prefix
    {
        get { return _prefix; }
        set { _prefix = value; }
    }

    public string Command
    {
        get { return _command; }
        set { _command = value; }
    }

    public string[] Arguments
    {
        get { return _args; }
        set { _args = value; }
    }

    public bool HasPrefix
    {
        get { return _hasPrefix; }
        set { _hasPrefix = value; }
    }

    public List<string> MessageData
    {
        get { return _messageData; }
        set { _messageData = value; }
    }
}

Hope i’ll helped someone with that :slight_smile: :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.