Exemplo n.º 1
0
        public void ProcessGroupMessage(GroupID groupId, UserID senderId, string message)
        {
            // 检查每分钟最大调用
            if (CheckCallPerMin(groupId))
            {
                return;
            }

            // 处理以 '/' 开头的消息
            RunAutoReply(groupId, message);
            if (Config.Instance.IsSlashRequired && !message.StartsWith('/'))
            {
                if (!showedSlashTip)
                {
                    Trace.WriteLine("提示: 设置中要求命令必须以 / 开头. ");
                    showedSlashTip = true;
                }
                return;
            }
            message = message.TrimStart('/', '、', '/');

            var handler = new GroupMessageHandler(senderId, groupId, message);

            // TODO 优化task数量
            // TODO cancellation token
            Task.Run(async() =>
            {
                var sw           = Stopwatch.StartNew();
                var cancelSource = new CancellationTokenSource();
                AsyncContext.SetCancellationToken(cancelSource.Token);
                var sender = new GroupMessageSender(groupId);
                AsyncContext.SetMessageSender(sender);
                var commandProcessTask = handler.ProcessCommandInput();

                using var locker = WFBotResourceLock.Create(
                          $"命令处理 #{Interlocked.Increment(ref commandCount)} 群[{groupId}] 用户[{senderId}] 内容[{message}]");
                await Task.WhenAny(commandProcessTask, Task.Delay(TimeSpan.FromSeconds(60)));

                if (!commandProcessTask.IsCompleted)
                {
                    cancelSource.Cancel();
                    await Task.Delay(10.Seconds());
                    if (!commandProcessTask.IsCompleted)
                    {
                        sender.SendMessage($"命令 [{message}] 处理超时.");
                    }
                    Trace.WriteLine($"命令 群[{groupId}] 用户[{senderId}] 内容[{message}] 处理超时.");
                    return;
                }
#if !DEBUG
                if (commandProcessTask.Result.matched)
                {
                    Trace.WriteLine($"命令 群 [{groupId}] 用户 [{senderId}] 内容 [{message}] 处理完成: {sw.Elapsed.Seconds:N1}s.");
                }
#endif
            });
        }
Exemplo n.º 2
0
 public static void SendTo(this string content, GroupMessageSender sender)
 {
     sender.SendMessage(content);
 }