Esempio n. 1
0
        public static void SendMessage(CommonMessageResponse resp)
        {
            var msg = (resp.EnableAt && resp.MessageType != MessageType.Private ? new At(resp.UserId) + " " : "") +
                      resp.Message;
            var info = SessionInfo[resp.Identity] == null
                ? $"{resp.Identity.Type}{resp.Identity.Id}"
                : SessionInfo[resp.Identity].Name;
            string status;

            switch (resp.MessageType)
            {
            case MessageType.Group:
                status = CqApi.SendGroupMessageAsync(resp.GroupId, msg).Status;
                break;

            case MessageType.Discuss:
                status = CqApi.SendDiscussMessageAsync(resp.DiscussId, msg).Status;
                break;

            case MessageType.Private:
                status = CqApi.SendPrivateMessageAsync(resp.UserId, msg).Status;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Message(string.Format("({0}) 我: {{status: {1}}}\r\n  {2}", info, status, CqCode.DecodeToString(msg)));
        }
Esempio n. 2
0
        /// <summary>
        /// 确定字体是否为空或有效。若为空或无效,则用默认语句替代。
        /// </summary>
        /// <returns></returns>
        private string GetRealWord(FontFamily font, string pandaPath)
        {
            string word = PandaWord;

            if (word == null || word.Replace("\n", "").Replace("\r", "").Trim() == "")
            {
                word = _blankReply[Rnd.Next(0, _blankReply.Length)];
                return(word);
            }

            word = CqCode.DecodeToString(PandaWord.Replace("!", "!").Replace("?", "?"));
            if (!IsLengthValid(word, pandaPath, font))
            {
                word = _invalidReply[Rnd.Next(0, _invalidReply.Length)];
            }
            return(word);
        }
Esempio n. 3
0
        public static void Query()
        {
            while (MessageQueue.Count != 0)
            {
                if (!MessageQueue.TryDequeue(out var messageObj))
                {
                    continue;
                }
                var cmd = messageObj.Command;

                const long cabbageId = 1020640876;
                string     uname;
                if (cmd == "statme" || cmd == "bpme" || cmd == "mybp" || cmd == "costme" || cmd == "mycost")
                {
                    BllUserRole        bllUserRole = new BllUserRole();
                    List <TblUserRole> userInfo    = bllUserRole.GetUserRoleByQq(long.Parse(messageObj.UserId));
                    if (userInfo.Count == 0)
                    {
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(LoliReply.IdNotBound, messageObj, true));
                    }

                    uname = userInfo[0].CurrentUname;
                }
                else
                {
                    uname = messageObj.ArgString;
                }

                using (Session session = new Session(25000, new Identity(cabbageId, MessageType.Private), cabbageId))
                {
                    CoolQDispatcher.SendMessage(
                        new CommonMessageResponse($"!{cmd.Replace("my", "").Replace("me", "")} {uname}",
                                                  new Identity(cabbageId, MessageType.Private)));
                    try
                    {
                        CommonMessage result = session.GetMessage();
                        session.Timeout = 600;
                        CommonMessage result2 = null;
                        try
                        {
                            result2 = session.GetMessage();
                        }
                        catch
                        {
                            // ignored
                        }

                        ImageInfo[] imgList =
                            CqCode.GetImageInfo(result.Message) ?? CqCode.GetImageInfo(result2?.Message);

                        if (imgList == null)
                        {
                            CoolQDispatcher.SendMessage(new CommonMessageResponse(result.Message, messageObj));
                            if (result2 != null)
                            {
                                CoolQDispatcher.SendMessage(new CommonMessageResponse(result2.Message, messageObj));
                            }
                            continue;
                        }
                        //throw new IndexOutOfRangeException("查询失败:" + result.Message);
                        var message = CqCode.DecodeToString(result.Message);
                        foreach (var item in imgList)
                        {
                            var          str = new FileImage(new Uri(item.Url));
                            StringFinder sf  = new StringFinder(message);
                            sf.FindNext("[图片]");
                            string str1 = sf.Cut();
                            if (sf.FindNext("[图片]", false) > message.Length - 1)
                            {
                                message = str1 + str;
                                continue;
                            }

                            sf.FindToLast();
                            string str2 = sf.Cut();
                            message = str1 + str + str2;
                        }

                        CoolQDispatcher.SendMessage(
                            new CommonMessageResponse(message + "\r\n(查询由白菜支持)", messageObj));
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        string msg = e.Message;
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(msg, messageObj, true));
                    }
                    catch (TimeoutException)
                    {
                        string msg = "查询失败,白菜没有搭理人家..";
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(msg, messageObj, true));
                    }
                    catch (Exception ex)
                    {
                        string msg = "查询失败,未知错误。";
                        Logger.Exception(ex);
                        CoolQDispatcher.SendMessage(new CommonMessageResponse(msg, messageObj, true));
                    } // catch
                }     // using
            }         // while
        }             //void
Esempio n. 4
0
        private static void HandleMessage(CommonMessage cm)
        {
            bool cmdFlag   = false;
            long groupId   = Convert.ToInt64(cm.GroupId);
            long userId    = Convert.ToInt64(cm.UserId);
            long discussId = Convert.ToInt64(cm.DiscussId);
            var  type      = cm.MessageType;

            string group, sender, message = cm.Message;

            if (cm.MessageType == MessageType.Private)
            {
                group  = "私聊";
                sender = SessionInfo[cm.Identity].Name;
            }
            else if (cm.MessageType == MessageType.Discuss)
            {
                group  = SessionInfo[cm.Identity].Name;
                sender = cm.UserId;
            }
            else
            {
                var userInfo = SessionInfo[cm.Identity]?.GroupInfo?.Members?.FirstOrDefault(i => i.UserId == userId) ??
                               CqApi.GetGroupMemberInfo(cm.GroupId, cm.UserId).Data;
                group  = SessionInfo?[cm.Identity]?.Name;
                sender = string.IsNullOrEmpty(userInfo.Card)
                    ? userInfo.Nickname
                    : userInfo.Card;
            }

            Message($"({group}) {sender}:\r\n  {CqCode.DecodeToString(message)}");

            if (cm.Message.Substring(0, 1) == CommandFlag)
            {
                if (cm.Message.IndexOf(CommandFlag + "root ", StringComparison.InvariantCulture) == 0)
                {
                    if (cm.UserId != "2241521134")
                    {
                        SendMessage(new CommonMessageResponse(LoliReply.FakeRoot, cm));
                    }
                    else
                    {
                        cm.FullCommand     = cm.Message.Substring(6, cm.Message.Length - 6);
                        cm.PermissionLevel = PermissionLevel.Root;
                        cmdFlag            = true;
                        HandleMessageCmd(cm);
                    }
                }
                else if (message.IndexOf(CommandFlag + "sudo ", StringComparison.InvariantCulture) == 0 &&
                         type == MessageType.Group)
                {
                    if (SessionInfo[cm.Identity].GroupInfo.Admins.Count(q => q.UserId == userId) == 0)
                    {
                        SendMessage(new CommonMessageResponse(LoliReply.FakeAdmin, cm));
                    }
                    else
                    {
                        cm.FullCommand     = message.Substring(6, message.Length - 6);
                        cm.PermissionLevel = PermissionLevel.Admin;
                        cmdFlag            = true;
                        HandleMessageCmd(cm);
                    }
                }
                else
                {
                    // auto
                    if (SessionInfo[cm.Identity].GroupInfo?.Admins.Count(q => q.UserId == userId) != 0)
                    {
                        cm.PermissionLevel = PermissionLevel.Admin;
                    }
                    if (cm.UserId == "2241521134")
                    {
                        cm.PermissionLevel = PermissionLevel.Root;
                    }

                    cm.FullCommand = message.Substring(1, message.Length - 1);
                    cmdFlag        = true;
                    HandleMessageCmd(cm);
                }
            }
            if (!cmdFlag)
            {
                SessionReceived?.Invoke(null, new SessionReceivedEventArgs
                {
                    MessageObj = cm
                });
            }

            HandleMesasgeApp(cm);
            Thread.Sleep(Rnd.Next(MinTime, MaxTime));
        }
Esempio n. 5
0
        private static void HandleMessage(CommonMessage cm)
        {
            long groupId   = Convert.ToInt64(cm.GroupId);
            long userId    = Convert.ToInt64(cm.UserId);
            long discussId = Convert.ToInt64(cm.DiscussId);
            var  type      = cm.MessageType;

            string group, sender, message = cm.Message;

            switch (cm.MessageType)
            {
            case MessageType.Private:
                group  = "私聊";
                sender = SessionInfo[cm.Identity].Name;
                break;

            case MessageType.Discuss:
                group  = SessionInfo[cm.Identity].Name;
                sender = cm.UserId;
                break;

            default:
            case MessageType.Group:
                var userInfo = CqApi.GetGroupMemberInfo(cm.GroupId, cm.UserId);     // 有点费时间
                group  = SessionInfo[cm.Identity].Name;
                sender = string.IsNullOrEmpty(userInfo.Data.Card)
                        ? userInfo.Data.Nickname
                        : userInfo.Data.Card;
                break;
            }

            Message($"({group}) {sender}:\r\n  {CqCode.DecodeToString(message)}");

            if (cm.Message.Substring(0, 1) == CommandFlag)
            {
                if (cm.Message.IndexOf(CommandFlag + "root ", StringComparison.InvariantCulture) == 0)
                {
                    if (cm.UserId != "2241521134")
                    {
                        SendMessage(new CommonMessageResponse(LoliReply.FakeRoot, cm));
                    }
                    else
                    {
                        cm.FullCommand     = cm.Message.Substring(6, cm.Message.Length - 6);
                        cm.PermissionLevel = PermissionLevel.Root;
                        HandleMessageCmd(cm);
                    }
                }
                else if (message.IndexOf(CommandFlag + "sudo ", StringComparison.InvariantCulture) == 0 &&
                         type == MessageType.Group)
                {
                    if (SessionInfo[cm.Identity].GroupInfo.Admins.Count(q => q.UserId == userId) == 0)
                    {
                        SendMessage(new CommonMessageResponse(LoliReply.FakeAdmin, cm));
                    }
                    else
                    {
                        cm.FullCommand     = message.Substring(6, message.Length - 6);
                        cm.PermissionLevel = PermissionLevel.Admin;
                        HandleMessageCmd(cm);
                    }
                }
                else
                {
                    cm.FullCommand = message.Substring(1, message.Length - 1);
                    HandleMessageCmd(cm);
                }
            }

            HandleMesasgeApp(cm);
            Thread.Sleep(Rnd.Next(MinTime, MaxTime));
        }