TwitchMessage.cs – oder: Was genau wollen die von mir?

Dieser Beitrag wurde nur partiell von einer früheren Version der Website wiederhergestellt.

Wer einen Bot programmiert, der kommt unweigerlich nicht an einer gewissen Sache vorbei: Das lesen, analysieren und auswerten von Textnachrichten. Allen voran gilt dies, wenn die Textnachrichten ein Befehl für den Bot sind, dann muss dieser nämlich dazu in der Lage sein, den ankommenden Datenstrom richtig zu lesen.
// Group matches for RegexPattern
// Group1: badges (moderator/1,subscriber/12) - can be null
// Group2: color (#A300CC)
// Group3: display-name - can be null
// Group4: Twitch bug. Just for bugfixing.
// Group5: emotes-only=1 - can be null
// Group6: emotes - can be null
// Group7: id
// Group8: mod
// Group9: room-id
// Group10: ---
// Group11: sent-ts
// Group12: subscriber
// Group13: tmi-sent-ts
// Group14: turbo
// Group15: user-id
// Group16: user-type - can be null
// Group17: nickname
// Group18: nickname
// Group19: nickname
// Group20: #channel
// Group21: Message

using System;
using System.Drawing;
using System.Text.RegularExpressions;

namespace TwitchBitch
{
    public class TwitchMessage
    {
        public TwitchMessageAuthor Author;
        public string Message { get; }
        public string Channel { get; }
        public bool EmotesOnly { get; }
        public DateTime ReceivedTime { get; }
        //public static string RegexPattern { get; } = @"^@badges=(\w+\/\d)?(,(\w+\/\d))?;color=(#......)?;display-name=(\w+);emotes=(.*?);id=(.*?);mod=(\d);room-id=(.*?);(sent-ts(.*?);)?subscriber=(\d);tmi-sent-ts=(.*?);turbo=(\d);user-id=(.*?);user-type=(\w+)?\s:(\w+)!\17@\17\.tmi\.twitch\.tv\s(\w+)\s#(\w+)(?:\s:(.*))?";
        public static string RegexPattern { get; } = @"^@badges=(.*);color=(#......)?;display-name=(\w+)?(\\s)?;(emote-only=1;)?emotes=(.*);id=(.+);mod=(0|1);room-id=(\d+);(sent-ts=(\d+);)?subscriber=(0|1);tmi-sent-ts=(\d+);turbo=(0|1);user-id=(\d+);user-type=(\w+)? :(\w+)!(\w+)@(\w+)\.tmi\.twitch\.tv PRIVMSG (#\w+) :(.+)$";
        public static string ShortRegexPattern { get; } = @"^PRIVMSG #(.*) :(.*)$";
        public TwitchMessage(string Data)
        {
            if(Regex.IsMatch(Data,RegexPattern))
            {
                try
                {
                    Match match = SIMPLEPATTERN.Match(Data);
                    string[] badges = match.Groups[1]?.ToString().Split(new char[] { ',' });
                    Color color = ColorTranslator.FromHtml(match.Groups[2]?.ToString());
                    string displayname = (match.Groups[3] == null || String.IsNullOrWhiteSpace(match.Groups[3].ToString())) ? match.Groups[17].ToString() : match.Groups[3].ToString();
                    bool emotes_only = (match.Groups[5] == null) ? false : true;
                    string emotes = (match.Groups[6] == null) ? null : match.Groups[5].ToString();
                    string id = match.Groups[7].ToString();
                    bool isMod = (match.Groups[8].ToString() == "1") ? true : false;
                    long roomId = long.Parse(match.Groups[9].ToString());
                    bool isSubscriber = (match.Groups[12].ToString() == "1") ? true : false;
                    long tmi_sent_ts = long.Parse(match.Groups[13].ToString());
                    bool isTurbo = (match.Groups[14].ToString() == "1") ? true : false;
                    long userId = long.Parse(match.Groups[15].ToString());
                    TwitchMessageAuthor.usertype userType;
                    Enum.TryParse(match.Groups[16].ToString(),out userType);
                    string nickname = match.Groups[17].ToString();
                    string channel = match.Groups[20].ToString();
                    string message = match.Groups[21].ToString();
                    bool isChannelOwner = (nickname == channel.Substring(1, channel.Length - 1)) ? true : false;

                    Author = new TwitchMessageAuthor(displayname, nickname,color, id, isMod, isSubscriber,isChannelOwner, isTurbo, userType);
                    this.Message = message;
                    this.Channel = channel;
                    this.EmotesOnly = emotes_only;

                    ReceivedTime = DateTime.Now;
                }
                catch
                {
                    Author = new TwitchMessageAuthor("unkown", "unknown",Color.Black, "0", false, false, false, false, TwitchMessageAuthor.usertype.regular);
                    this.Message = "<RegEx-Error>";
                    this.Channel = "RegEx-Error";
                    ReceivedTime = DateTime.Now;
                }
            }
            else if(Regex.IsMatch(Data,ShortRegexPattern))
            {
                try
                {
                    Match match = SHORTSIMPLEPATTERN.Match(Data);
                    string username = PluginInfoDevice.ClientConfig.Username;
                    string displayname = PluginInfoDevice.ClientConfig.Username;
                    Color displaycolor = Color.Black;
                    string id = "0";
                    bool isMod = false;
                    bool isSubscriber = false;
                    bool isTurbo = false;
                    bool isChannelOwner = false;
                    string channel = "#" + match.Groups[1].ToString();
                    string message = match.Groups[2].ToString();

                    Author = new TwitchMessageAuthor(displayname, username, displaycolor, id, isMod, isSubscriber, isChannelOwner, isTurbo, TwitchMessageAuthor.usertype.regular);
                    this.Message = message;
                    this.Channel = channel;
                    ReceivedTime = DateTime.Now;
                }
                catch
                {
                    Author = new TwitchMessageAuthor(PluginInfoDevice.ClientConfig.Username, PluginInfoDevice.ClientConfig.Username, Color.Black, "0", false, false, false, false, TwitchMessageAuthor.usertype.regular);
                    this.Message = "<RegEx-Error>";
                    this.Channel = "RegEx-Error";
                    ReceivedTime = DateTime.Now;
                }
            }

        }

        public class TwitchMessageAuthor
        {
            public string DisplayName { get; }
            public string UserName { get; }
            public string Id { get; }
            public string[] Badges { get; }
            public bool IsMod { get; }
            public bool IsSubscriber { get; }
            public bool IsTurbo { get; }
            public bool IsChannelOwner { get; }
            public usertype Usertype { get; }
            public Color DisplayColor { get; }

            public TwitchMessageAuthor(string displayname, string username, Color displaycolor, string id, bool isMod, bool isSubscriber, bool isChannelOwner, bool isTurbo, usertype type)
            {
                this.DisplayName = displayname;
                this.UserName = username;
                this.DisplayColor = displaycolor;
                this.Id = id;
                this.IsMod = isMod;
                this.IsSubscriber = isSubscriber;
                this.IsTurbo = isTurbo;
                this.Usertype = type;
            }

            public enum usertype
            {
                mod,
                global_mod,
                admin,
                staff,
                regular
            }
        }
        private static Regex SIMPLEPATTERN = new Regex(RegexPattern);
        private static Regex SHORTSIMPLEPATTERN = new Regex(ShortRegexPattern);

    }
}

One comment on “TwitchMessage.cs – oder: Was genau wollen die von mir?

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert