Example #1
0
 /// <summary>
 /// 检查参数数组长度
 /// </summary>
 /// <param name="args">指令数组</param>
 /// <param name="len">至少需要的参数个数</param>
 /// <param name="QQgroup">(可选,不给的话就不发送错误信息)\n报错信息要发送到的QQ群对象</param>
 /// <param name="fromQQid">(可选,但QQgroup给了的话本参数必填)\n要通知的人的QQ Id</param>
 /// <returns>Illegal不符合 Legitimate符合 Extra超出</returns>
 public static LenType CheckForLength(string[] args, int len, Group QQgroup = null, long fromQQid = 0)
 {
     if (args.Length >= len + 1)
     {
         if (args.Length == len + 1)
         {
             return(LenType.Legitimate);
         }
         else
         {
             return(LenType.Extra);
         }
     }
     else
     {
         QQgroup?.SendGroupMessage(CQApi.CQCode_At(fromQQid), " 命令参数不全,请补充。");
         return(LenType.Illegal);
     }
 }
        /// <summary>
        /// 主处理函数,负责主要流程即注册所有消息处理函数->缓存->发送处理函数返回的回复
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public bool handle(CQGroupMessageEventArgs e)
        {
            if (!IsInit)
            {
                init();
            }
            _GroupID = e.FromGroup;
            if (BlockList.Contains(_GroupID))
            {
                return(false);                                //处于屏蔽列表不处理任何信息
            }
            if (!GroupState.GroupState.ContainsKey(_GroupID)) //若缓存中不存在此群
            {
                GroupState.AddNewGroupToList(_GroupID);       //将群加入缓存
                Base_SQLHelper.SQLHelperData _new = new Base_SQLHelper.SQLHelperData();
                _new.Connection   = new SQLiteConnection("Data Source=data/db/" + _GroupID.ToString() + ".db");
                _new.command      = new SQLiteCommand();
                SQLPool[_GroupID] = _new;
                if (GroupConfig.HasConfig(_GroupID))//若存在对应ConfigINI文件则读入相关固化配置
                {
                    GroupState.GroupState[_GroupID].AllowRepeat = GroupConfig.AllowRepeat(_GroupID);
                    GroupState.GroupState[_GroupID].AllowReply  = GroupConfig.AllowReply(_GroupID);
                    GroupState.GroupState[_GroupID].AllowR18    = GroupConfig.AllowR18(_GroupID);
                    GroupState.GroupState[_GroupID].Admin       = GroupConfig.GetAllAdmin(_GroupID);
                    if (GroupConfig.Allowdd(_GroupID))
                    {
                        dd.Add(e);
                    }

                    if (!GroupConfig.IsListen(_GroupID))//ConfigINI中记录为BlackList则加入屏蔽列表并退出
                    {
                        BlockList.Add(_GroupID);
                        return(false);
                    }
                }
                else
                {
                    GroupConfig.NewGroupConfig(_GroupID);//不存在ConfigINI文件则新建
                }
                //return false;//缓存中未命中的情况下第一次不会发任何信息
            }
            if (!GroupState.GroupState[_GroupID].AllowReply)
            {
                return(false);                                            //不允许回复直接退出
            }
            //正式命令处理部分
            Msg = e.Message.Text.Trim();
            if (rand.Next(100) > 95)
            {//随机无意义表情包
                _GroupID.SendGroupMessage(CQApi.CQCode_Image("sm/" + new Base_FileHelper().RandomGetImg("data/image/sm")));
                return(true);
            }
            if (Msg.Length < 21)
            {//无AT的短语句匹配注册命令
                //匹配短语
                //首先尝试获取称呼,若存在称呼则删去后再匹配
                if (SQL.UserExists(SQLPool[_GroupID], e.FromQQ) && Msg.Contains(SQL.GetCalled(SQLPool[_GroupID], e.FromQQ)))
                {
                    //TODO...
                }
                if (SimpleContainer.IsRegistered <IMsgHandler>(Msg))
                {
                    Reply = SimpleContainer.Resolve <IMsgHandler>(Msg).Handler(e, SQLPool[_GroupID]);
                    if (Reply != "")
                    {
                        _GroupID.SendGroupMessage(Reply);
                        return(true);
                    }
                }
            }
            else //被AT后的处理
            {
                if (Msg.Substring(0, 21) == @"[CQ:at,qq=3178223002]" && Msg.Length >= 22)
                {
                    String Order = Msg.Substring(22).Trim();
                    //_GroupID.SendGroupMessage(Order.Substring(0, Order.IndexOf("#") - 1));
                    if (Order.Contains("#") && Order.IndexOf("#") > 0)
                    {
                        Order = Order.Substring(0, Order.IndexOf("#"));
                    }
                    if (OrderContainer.IsRegistered <IMsgHandler>(Order))
                    {
                        Reply = OrderContainer.Resolve <IMsgHandler>(Order).Handler(e, SQLPool[_GroupID]);
                        if (Reply != "")
                        {
                            _GroupID.SendGroupMessage(Reply);
                            return(true);
                        }
                    }
                    //return false;
                    _GroupID.SendGroupMessage(CQApi.CQCode_Image("sm/EYHQ.jpg"));
                    return(true);
                }
            }
            ///复读机部分
            if (GroupState.GroupState[_GroupID].AllowRepeat)
            {
                if (GroupState.GroupState[_GroupID].LastRep != e.Message)
                {
                    if (GroupState.GroupState[_GroupID].FromQQ != e.FromQQ && GroupState.GroupState[_GroupID].Msg == e.Message)
                    {
                        _GroupID.SendGroupMessage(e.Message);
                        GroupState.GroupState[_GroupID].LastRep = e.Message;
                        return(true);
                    }
                }
                GroupState.GroupState[_GroupID].FromQQ = e.FromQQ;
                GroupState.GroupState[_GroupID].Msg    = e.Message;
            }
            return(false);
        }