Ejemplo n.º 1
0
        public void Ctor_GenerateNames()
        {
            // A & A
            ExtractAttribute attr = new ExtractAttribute("!at [hyper.at({who})]");

            // A
            Assert.IsTrue(attr.Names.SequenceEqual(new string[] { "who" }));
        }
Ejemplo n.º 2
0
        public void Regex_Match()
        {
            // A & A
            ExtractAttribute attr  = new ExtractAttribute("!ban {ban}");
            Match            match = attr.Pattern.Match("!ban me!");

            // A
            Assert.IsTrue(match.Success);
        }
Ejemplo n.º 3
0
        public void HandleOne(ActionEntry entry, MessageContext context)
        {
            if (entry.State is int errorCount && errorCount >= 3)
            {
                if (errorCount == 3)
                {
                    _logger.LogWarning("An Action has met its error limit and has been disabled: " + entry);
                }

                return;
            }

            #region Extract Check

            ExtractAttribute extract = entry.Action.GetCustomAttribute <ExtractAttribute>();
            Dictionary <string, MessageChain> dict = new Dictionary <string, MessageChain>();

            if (extract != null)
            {
                Match match = extract.Pattern.Match(_formatter.Format(context.Message.AsReadable()));
                if (match.Success)
                {
                    string[] names = extract.Names.ToArray();
                    for (int i = 1; i < match.Groups.Count; i++)
                    {
                        dict.Add(names[i - 1], _parser.Parse(match.Groups[i].Value));
                    }
                }
                else
                {
                    return;
                }
            }

            #endregion Extract Check

            #region Filter Check

            object[] filterBys      = entry.Action.GetCustomAttributes(typeof(FilterByAttribute), false);
            string   failureMessage = null;
            bool     pass           = filterBys.All(x =>
            {
                FilterByAttribute filter = (FilterByAttribute)x;
                if (!filter.Filter.Check(context))
                {
                    failureMessage = filter.FailureMessage;
                    return(false);
                }
                return(true);
            });
            if (!pass)
            {
                if (failureMessage != null)
                {
                    MessageChain chain = new MessageChain(new MessageComponent[] { new Plain(entry.Action.Name + ": " + failureMessage) });
                    context.ReplyAsync(chain).Wait();
                }
                return;
            }

            #endregion Filter Check
            InvokeOne(entry, context, dict);
        }