Lepstr
October 21, 2015, 3:04pm
1
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;
Lepstr
October 21, 2015, 3:33pm
3
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
Lepstr
October 21, 2015, 4:10pm
5
Yeah, but it’s also Empty if the sender is an Moderator^^ that is something i dont understand
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.
Lepstr
October 22, 2015, 12:11pm
7
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 is how I’m turning any tags into a simple map of strings (in Go, but it’s just an example of simple string splits).
This is after splitting out the tag part of the message, not including @ or trailing space.
func tagsToMap(tagString string) map[string]string {
result := make(map[string]string)
splitTags := strings.Split(tagString, ";")
for _, tag := range splitTags {
splitTag := strings.SplitN(tag, "=", 2)
result[splitTag[0]] = splitTag[1]
}
return res…
This method does not break with changes to tags or their order.
Lepstr
October 22, 2015, 12:45pm
9
1 Like
Lepstr
October 22, 2015, 3:29pm
10
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
system
Closed
November 21, 2015, 3:30pm
11
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.