Exemple #1
0
        internal static async Task <int> CommonSendMessage(long id, bool isGroup, IMessageBase[] chain, int?quote)
        {
            if (!MiddlewareCore.OnSend(ref id, ref isGroup, ref chain, ref quote))
            {
                return(0);
            }

            if (isGroup)
            {
                return(await session.SendGroupMessageAsync(id, chain, quote));
            }
            else
            {
                if (BotInfo.HasFriend(id))
                {
                    return(await session.SendFriendMessageAsync(id, chain, quote));
                }
                int    msgid    = 0;
                long[] tempPath = BotInfo.GetTempPath(id);
                foreach (var t in tempPath)
                {
                    try
                    {
                        msgid = await session.SendTempMessageAsync(id, t, chain, quote);

                        break;
                    }
                    catch { }
                }
                return(msgid);
            }
        }
#pragma warning disable CS1998 // 异步方法缺少 "await" 运算符,将以同步方式运行
        protected static async Task OnMessage(MiraiHttpSession session, string message, Source Sender)
#pragma warning restore CS1998 // 异步方法缺少 "await" 运算符,将以同步方式运行
        {
            if (!booted)
            {
                return;
            }

            bool isAdmin = AdminQQ == Sender.FromQQ || Configuration.GetConfig <Admin>().hash.Contains(Sender.FromQQ);
            long ticks   = DateTime.Now.Ticks;

            if (IsIgnore(Sender))
            {
                return;
            }

            Action <string> callback = delegate(string s)
            {
                try
                {
                    Utils.Log(LoggerLevel.Debug, $"[{(DateTime.Now.Ticks - ticks) / 10000}ms] sent msg: " + s);
                    if (Sender.FromGroup != 0)
                    {
                        session.SendGroupMessageAsync(Sender.FromGroup, Utils.GetMessageChain(s, p => session.UploadPictureAsync(UploadTarget.Group, p).Result)).Wait();
                    }
                    else if (!Sender.IsTemp)
                    {
                        session.SendFriendMessageAsync(Sender.FromQQ, Utils.GetMessageChain(s, p => session.UploadPictureAsync(UploadTarget.Temp, p).Result)).Wait();
                    }
                    else
                    {
                        session.SendTempMessageAsync(Sender.FromQQ, Sender.FromGroup, Utils.GetMessageChain(s, p => session.UploadPictureAsync(UploadTarget.Friend, p).Result)).Wait();
                    }
                }
                catch (Exception e)
                {
                    Utils.Log(LoggerLevel.Error, "error in msg: " + s + "\n" + e.ToString());
                }
            };

            Utils.Log(LoggerLevel.Debug, "recv msg: " + message);

            Task.Run(() =>
            {
                instance.OnMessage(message, Sender, isAdmin, callback);
            }).Start();
        }
Exemple #3
0
        public void sendMessage(string msg)
        {
            switch (type)
            {
            case 0:
                session.SendFriendMessageAsync(peerid, new IMessageBase[] { new PlainMessage(msg) }).Wait();
                break;

            case 1:
                session.SendGroupMessageAsync(peerid, new IMessageBase[] { new PlainMessage(msg) }).Wait();
                break;

            case 2:
                session.SendTempMessageAsync(peerid, tgroup, new IMessageBase[] { new PlainMessage(msg) }).Wait();
                break;
            }
        }
Exemple #4
0
        protected static void OnMessage(MiraiHttpSession session, string message, Source Sender)
        {
            if (!booted)
            {
                return;
            }

            long ticks = DateTime.Now.Ticks;

            Func <string, Task> callback = async s =>
            {
                try
                {
                    Utils.Log(LoggerLevel.Debug, $"[{(DateTime.Now.Ticks - ticks) / 10000}ms] sent msg: " + s);
                    if (Sender.FromGroup != 0)
                    {
                        await session.SendGroupMessageAsync(Sender.FromGroup, await Utils.GetMessageChain(s, async p => await session.UploadPictureAsync(UploadTarget.Group, p)));
                    }
                    else if (!Sender.IsTemp)
                    {
                        await session.SendFriendMessageAsync(Sender.FromQQ, await Utils.GetMessageChain(s, async p => await session.UploadPictureAsync(UploadTarget.Temp, p)));
                    }
                    else
                    {
                        await session.SendTempMessageAsync(Sender.FromQQ, Sender.FromGroup, await Utils.GetMessageChain(s, async p => await session.UploadPictureAsync(UploadTarget.Friend, p)));
                    }
                }
                catch (Exception e)
                {
                    Utils.Log(LoggerLevel.Error, "error in msg: " + s + "\n" + e.ToString());
                }
            };

            Utils.Log(LoggerLevel.Debug, "recv msg: " + message);

            Task.Run(() => instance.OnMessage(new HandlerArgs
            {
                message  = message,
                Sender   = Sender,
                Callback = callback
            }));
        }
Exemple #5
0
        async Task <bool> ITempMessage.TempMessage(MiraiHttpSession session, ITempMessageEventArgs e)
        {
            if (BotInfo.BannedUser.Contains(e.Sender.Id))
            {
                return(false);
            }
            if (BotInfo.DebugMode)
            {
                if (BotInfo.DebugReplyAdminOnly)
                {
                    if (!BotInfo.AdminQQ.Contains(e.Sender.Id))
                    {
                        return(false);
                    }
                }
            }
            if (e.Chain.Length > 1)  //普通消息
            {
                switch (e.Chain[1].Type)
                {
                case "Plain":
                    PlainMessageHandler.HandleFriendMesage(session, e.Chain, e.Sender.Id);
                    break;

                case "Image":
                    for (int i = 1; i < e.Chain.Length; i++)
                    {
                        ImageMessage imgMsg = e.Chain[i] as ImageMessage;
                        await SearchPictureHandler.SearchPicture(imgMsg, picStream => session.UploadPictureAsync(UploadTarget.Group, picStream), msg => session.SendTempMessageAsync(e.Sender.Id, e.Sender.Group.Id, msg));
                    }
                    break;
                }
            }
            return(false);
        }