예제 #1
0
 /// <summary>
 ///     使用默认 <see cref="IMessageChainFormatter" /> 格式化消息链
 /// </summary>
 /// <param name="message">消息链</param>
 /// <returns>消息文本</returns>
 public static string Flatten(this MessageChain message)
 {
     return(_formatter.Format(message));
 }
        public override void WriteJson(JsonWriter writer, [AllowNull] MessageChain value, JsonSerializer serializer)
        {
            var text = formatter.Format(value);

            writer.WriteRawValue(text);
        }
예제 #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);
        }
예제 #4
0
        public bool Run(IApiClient client, GenericEventArgs eventArgs)
        {
            switch (eventArgs)
            {
            case GroupMessageEventArgs args:
                _logger.LogInformation("GroupMessageEventArgs received {0}:\n{1}=>{2}", args.User.DisplayName, args.Group.Name, _formatter.Format(args.Message));
                break;

            case FriendMessageEventArgs args:
                _logger.LogInformation("FriendMessageEventArgs received {0}:\n{1}", args.User.Nickname, args.Message.ToString());
                break;

            default:
                _logger.LogInformation("GenericEventArgs received at {0}", eventArgs.Time);
                break;
            }

            return(true);
        }