Beispiel #1
0
        public CoolQMessage(params CoolQCode[] cqCodes)
        {
            var cqCodeList = new List <CoolQCode>(cqCodes.Length);

            cqCodeList.AddRange(cqCodes);
            _assembledCqCodes = new Assemblage(cqCodeList);
        }
Beispiel #2
0
        private static string InnerCompose(CoolQCode cqCode)
        {
            switch (cqCode)
            {
            case Text text:
                return(text.Content);

            case At at:
                return($"[CQ:at,qq={at.UserId}]");

            case Face face:
                return($"[CQ:face,id={face.FaceId}]");

            case Emoji emoji:
                return($"[CQ:emoji,id={emoji.EmojiId}]");

            case BFace bface:
                return($"[CQ:bface,id={bface.BFaceId}]");

            case SFace sface:
                return($"[CQ:sface,id={sface.SFaceId}]");

            case CustomMusic music:
                return(string.Format("[CQ:music,type=custom,url={0},audio={1},title={2},content={3},image={4}]",
                                     music.LinkUrl, music.AudioUrl, music.Title, music.Content, music.ImageUrl));

            case Share share:
                return(string.Format("[CQ:share,url={0},title={1},content={2},image={3}]",
                                     share.LinkUrl, share.Title, share.Content, share.ImageUrl));

            case FileImage image:
            {
                switch (image.FileType)
                {
                case FileCoolQCode.FileTypeEnum.Local:
                    return(image.UseBase64
                                    ? $"[CQ:image,file=base64://{EncodeFileToBase64(image.Path)}]"
                                    : $"[CQ:image,file=file://{image.Path}]");

                // ReSharper disable once RedundantCaseLabel
                case FileCoolQCode.FileTypeEnum.Url:
                default:
                    return($"[CQ:image,file=http://{image.Url.Replace("http://", "")}]");
                }
            }

            case FileRecord record:
            {
                switch (record.FileType)
                {
                case FileCoolQCode.FileTypeEnum.Local:
                    return(record.UseBase64
                                    ? $"[CQ:record,file=base64://{EncodeFileToBase64(record.Path)}]"
                                    : $"[CQ:record,file=file://{record.Path}]");

                // ReSharper disable once RedundantCaseLabel
                case FileCoolQCode.FileTypeEnum.Url:
                default:
                    return($"[CQ:record,file=http://{record.Url.Replace("http://", "")}]");
                }
            }

            case Assemblage assemblage:
                var           codes = assemblage.CoolQCodes;
                StringBuilder sb    = new StringBuilder();
                foreach (var code in codes)
                {
                    sb.Append(InnerCompose(code));
                }
                return(sb.ToString());

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #3
0
 public static string Compose(CoolQCode cqCode) => InnerCompose(cqCode);
Beispiel #4
0
        public override async void SendMessageAsync(RouteMessage message)
        {
            var routeMsg = (CoolQRouteMessage)message;

            OnMessageSending?.Invoke(routeMsg);
            if (routeMsg.Canceled && !routeMsg.IsForced)
            {
                return;
            }

            if (routeMsg.DelayTime > TimeSpan.FromSeconds(0))
            {
                await Task.Delay(routeMsg.DelayTime).ConfigureAwait(false);
            }

            var msg = (routeMsg.EnableAt && routeMsg.MessageType != MessageType.Private
                          ? new At(routeMsg.UserId) + " "
                          : "") + ((CoolQMessage)routeMsg.Message).Compose();
            string info = routeMsg.Identity.ToString();
            string status;

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

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

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

            default:
                throw new ArgumentOutOfRangeException();
            }

            Logger.Message(string.Format("({0}) 我: {{status: {1}}}\r\n  {2}", info, status, CoolQCode.DecodeToString(msg)));
        }