示例#1
0
        /// <summary>
        /// <para>从色图源获取色图</para>
        /// <para>不会支持R18的哦</para>
        /// </summary>
        /// <param name="hso">hso配置实例</param>
        private async Task GiveMeSetu(Hso hso)
        {
            string  localPicPath;
            JObject response;

            ConsoleLog.Debug("源", hso.Source);
            //本地模式
            if (hso.Source == SetuSourceType.Local)
            {
                string[] picNames = Directory.GetFiles(IOUtils.GetHsoPath());
                if (picNames.Length == 0)
                {
                    await QQGroup.SendGroupMessage("机器人管理者没有在服务器上塞色图\r\n你去找他要啦!");

                    return;
                }
                Random randFile = new Random();
                localPicPath = $"{picNames[randFile.Next(0, picNames.Length - 1)]}";
                ConsoleLog.Debug("发送图片", localPicPath);
                await QQGroup.SendGroupMessage(hso.CardImage
                                               ?CQCode.CQCardImage(localPicPath)
                                               : CQCode.CQImage(localPicPath));

                return;
            }
            //网络部分
            try
            {
                ConsoleLog.Info("NET", "尝试获取色图");
                await QQGroup.SendGroupMessage("正在获取色图中...");

                string apiKey;
                string serverUrl;
                //源切换
                switch (hso.Source)
                {
                case SetuSourceType.Mix:
                    Random randSource = new Random();
                    if (randSource.Next(1, 100) > 50)
                    {
                        serverUrl = "https://api.lolicon.app/setu/";
                        apiKey    = hso.LoliconApiKey ?? string.Empty;
                    }
                    else
                    {
                        serverUrl = "https://api.yukari.one/setu/";
                        apiKey    = hso.YukariApiKey ?? string.Empty;
                    }
                    break;

                case SetuSourceType.Yukari:
                    serverUrl = "https://api.yukari.one/setu/";
                    apiKey    = hso.YukariApiKey ?? string.Empty;
                    break;

                case SetuSourceType.Lolicon:
                    serverUrl = "https://api.yukari.one/setu/";
                    apiKey    = hso.YukariApiKey ?? string.Empty;
                    break;

                default:
                    await QQGroup.SendGroupMessage("发生了未知错误");

                    ConsoleLog.Error("Hso", "发生了未知错误");
                    return;
                }
                //向服务器发送请求
                ReqResponse reqResponse = await Requests.GetAsync(serverUrl, new ReqParams
                {
                    Timeout = 3000,
                    Params  = new Dictionary <string, string>
                    {
                        { "apikey", apiKey }
                    }
                });

                if (reqResponse.StatusCode != HttpStatusCode.OK)
                {
                    ConsoleLog.Error("Net", $"{serverUrl} return code {(int)reqResponse.StatusCode}");
                    await HsoEventArgs.SourceGroup.SendGroupMessage($"哇哦~发生了网络错误[{reqResponse.StatusCode}],请联系机器人所在服务器管理员");

                    return;
                }
                response = reqResponse.Json();
                ConsoleLog.Debug("Get Json", response);
            }
            catch (Exception e)
            {
                //网络错误
                await QQGroup.SendGroupMessage("哇哦~发生了网络错误,请联系机器人所在服务器管理员");

                ConsoleLog.Error("网络发生错误", ConsoleLog.ErrorLogBuilder(e.InnerException));
                return;
            }
            //json处理
            try
            {
                if ((int)response["code"] == 0)
                {
                    //图片链接
                    string picUrl = response["data"]?[0]?["url"]?.ToString() ?? "";
                    ConsoleLog.Debug("获取到图片", picUrl);
                    //本地图片存储路径
                    localPicPath = $"{IOUtils.GetHsoPath()}/{Path.GetFileName(picUrl)}".Replace('\\', '/');
                    if (File.Exists(localPicPath)) //检查是否已缓存过图片
                    {
                        await QQGroup.SendGroupMessage(hso.CardImage
                                                       ?CQCode.CQCardImage(localPicPath)
                                                       : CQCode.CQImage(localPicPath));
                    }
                    else
                    {
                        //文件名处理(mirai发送网络图片时pixivcat会返回403暂时无法使用代理发送图片
                        //QQGroup.SendGroupMessage(CQApi.Mirai_UrlImage(picUrl));

                        //检查是否有设置代理
                        if (!string.IsNullOrEmpty(hso.PximyProxy))
                        {
                            string[]      fileNameArgs    = Regex.Split(Path.GetFileName(picUrl), "_p");
                            StringBuilder proxyUrlBuilder = new StringBuilder();
                            proxyUrlBuilder.Append(hso.PximyProxy);
                            //图片Pid部分
                            proxyUrlBuilder.Append(hso.PximyProxy.EndsWith("/") ? $"{fileNameArgs[0]}" : $"/{fileNameArgs[0]}");
                            //图片Index部分
                            proxyUrlBuilder.Append(fileNameArgs[1].Split('.')[0].Equals("0") ? string.Empty : $"/{fileNameArgs[1].Split('.')[0]}");
                            ConsoleLog.Debug("Get Proxy Url", proxyUrlBuilder);
                            DownloadFileFromURL(proxyUrlBuilder.ToString(), localPicPath, hso.UseCache, hso.CardImage);
                        }
                        else
                        {
                            DownloadFileFromURL(picUrl, localPicPath, hso.UseCache, hso.CardImage);
                        }
                    }
                    return;
                }
                if (((int)response["code"] == 401 || (int)response["code"] == 429) &&
                    hso.Source == SetuSourceType.Lolicon)
                {
                    ConsoleLog.Warning("API Token 失效", $"code:{response["code"]}");
                }
                else
                {
                    ConsoleLog.Warning("没有找到图片信息", "服务器拒绝提供信息");
                }
                await QQGroup.SendGroupMessage("哇奧色图不见了\n请联系机器人服务器管理员");
            }
            catch (Exception e)
            {
                ConsoleLog.Error("色图下载失败", $"网络下载数据错误\n{ConsoleLog.ErrorLogBuilder(e)}");
            }
        }
示例#2
0
        // private async void GetGuildRank(string[] commandArgs)
        // {
        //     //TODO 修改为可切换源的分发方法
        // }

        /// <summary>
        /// 从比利比利源查询排名
        /// </summary>
        private async ValueTask BiliWikiRank(string guildName)
        {
            JArray response;

            //获取响应
            try
            {
                //获取查询结果
                Log.Info("NET", $"尝试查询[{guildName}]会站排名");
                await QQGroup.SendGroupMessage($"正在查询公会[{guildName}]的排名...");

                ReqResponse reqResponse = await Requests.GetAsync("https://tools-wiki.biligame.com/pcr/getTableInfo",
                                                                  new ReqParams
                {
                    Timeout = 3000,
                    Params  = new Dictionary <string, string>
                    {
                        { "type", "search" },
                        { "search", guildName },
                        { "page", "0" }
                    }
                });

                //判断响应
                if (reqResponse.StatusCode != HttpStatusCode.OK)
                {
                    await
                    QQGroup
                    .SendGroupMessage($"哇哦~发生了网络错误,请联系机器人所在服务器管理员\r\n{reqResponse.StatusCode}({(int) reqResponse.StatusCode})");

                    Log.Error("网络发生错误", $"Code[{(int) reqResponse.StatusCode}]");
                    //阻止下一步处理
                    return;
                }

                //读取返回值
                response = new JArray(reqResponse.Text);
                Log.Info("获取JSON成功", response);
            }
            catch (Exception e)
            {
                await QQGroup.SendGroupMessage("哇哦~发生了网络错误,请联系机器人所在服务器管理员");

                Log.Error("网络发生错误", Log.ErrorLogBuilder(e));
                //阻止下一步处理
                return;
            }

            //JSON数据处理
            try
            {
                //对返回值进行判断
                if (response.Count == 0)
                {
                    await QQGroup.SendGroupMessage("未找到任意公会\n请检查是否查询的错误的公会名或公会排名在70000之后");

                    Log.Info("JSON处理成功", "所查询列表为空");
                    return;
                }

                JArray  dataArray = JArray.Parse(response.First?.ToString() ?? "[]");
                JObject rankData  = dataArray[0].ToObject <JObject>();
                if (dataArray.Count > 1)
                {
                    await QQGroup.SendGroupMessage("查询到多个公会,可能存在重名或关键词错误");
                }
                if (rankData == null || rankData.Count == 0)
                {
                    await QQGroup.SendGroupMessage("发生了未知错误,请请向开发者反馈问题");

                    Log.Error("JSON数据读取错误", "从网络获取的文本为空");
                    return;
                }

                string rank       = rankData["rank"]?.ToString();
                string totalScore = rankData["damage"]?.ToString();
                string leaderName = rankData["leader_name"]?.ToString();
                Log.Info("JSON处理成功", "向用户发送数据");
                await QQGroup.SendGroupMessage("查询成功!\n" +
                                               $"公会:{guildName}\n" +
                                               $"排名:{rank}\n" +
                                               $"总分数:{totalScore}\n" +
                                               $"会长:{leaderName}\n" +
                                               "如果查询到的信息有误,有可能关键词错误或公会排名在70000之后");
            }
            catch (Exception e)
            {
                await QQGroup.SendGroupMessage("发生了未知错误,请请向开发者反馈问题");

                Log.Error("JSON数据读取错误", $"从网络获取的JSON格式无法解析{Log.ErrorLogBuilder(e)}");
            }
        }
        public static async ValueTask KyoukaRank(GroupMessageEventArgs eventArgs)
        {
            //网络响应
            JToken response;
            //获取公会名
            var guildName = GetGuildName(eventArgs, 4);

            if (string.IsNullOrEmpty(guildName))
            {
                await eventArgs.Reply("此群未被记录为公会\r\n请建会后再查询或输入公会名进行查询");

                return;
            }

            //获取网络响应
            try
            {
                //获取查询结果
                Log.Info("NET", $"尝试查询[{guildName}]会站排名");
                await eventArgs.Reply($"正在查询公会[{guildName}]的排名...");

                ReqResponse reqResponse =
                    await Requests.PostAsync("https://service-kjcbcnmw-1254119946.gz.apigw.tencentcs.com/name/0",
                                             new ReqParams
                {
                    Timeout  = 3000,
                    PostJson = new JObject
                    {
                        ["clanName"] = guildName,
                        ["history"]  = 0
                    },
                    Header = new Dictionary <HttpRequestHeader, string>
                    {
                        { HttpRequestHeader.Referer, "https://kengxxiao.github.io/Kyouka/" }
                    },
                    CustomHeader = new Dictionary <string, string>
                    {
                        { "Custom-Source", "AntiRainBot" }
                    }
                });

                //判断响应
                if (reqResponse.StatusCode != HttpStatusCode.OK)
                {
                    await
                    eventArgs
                    .Reply($"哇哦~发生了网络错误,请联系机器人所在服务器管理员\r\n{reqResponse.StatusCode}({(int) reqResponse.StatusCode})");

                    Log.Error("网络发生错误", $"Code[{reqResponse.StatusCode}({(int) reqResponse.StatusCode})]");
                    //阻止下一步处理
                    return;
                }

                //读取返回值
                response = reqResponse.Json();
                //判断空数据
                if (response == null || !response.Any())
                {
                    await eventArgs.Reply("发生了未知错误,请请向开发者反馈问题");

                    Log.Error("JSON数据读取错误", "从网络获取的文本为空");
                    return;
                }

                Log.Info("获取JSON成功", response);
            }
            catch (Exception e)
            {
                await eventArgs.Reply($"哇哦~发生了网络错误,请联系机器人所在服务器管理员\n{e.Message}");

                Log.Error("网络发生错误", e);
                //阻止下一步处理
                return;
            }

            //JSON数据处理
            try
            {
                if (response["full"] == null)
                {
                    await eventArgs.Reply("发生了未知错误,请请向开发者反馈问题");

                    Log.Error("JSON数据读取错误", "从网络获取的JSON格式可能有问题");
                    return;
                }

                //在有查询结果时查找值
                if (!response["full"]?.ToString().Equals("0") ?? false)
                {
                    if (!response["full"]?.ToString().Equals("1") ?? false)
                    {
                        await eventArgs.Reply("查询到多个公会,可能存在重名或关键词错误");
                    }
                    Log.Info("JSON处理成功", "向用户发送数据");
                    long.TryParse(response["ts"]?.ToString() ?? "0", out long updateTimeStamp);
                    await eventArgs.Reply("查询成功!\n",
                                          $"公会:{guildName}\n",
                                          $"排名:{response["data"]?[0]?["rank"]}\n",
                                          $"总分数:{response["data"]?[0]?["damage"]}\n",
                                          $"会长:{response["data"]?[0]?["leader_name"]}\n",
                                          $"数据更新时间:{updateTimeStamp.ToDateTime():MM-dd HH:mm:ss}\n",
                                          "如果查询到的信息有误,有可能关键词错误或公会排名在20060之后");
                }
                else
                {
                    await eventArgs.Reply("未找到任意公会\n请检查是否查询的错误的公会名或公会排名在20060之后");

                    Log.Info("JSON处理成功", "所查询列表为空");
                }
            }
            catch (Exception e)
            {
                await eventArgs.Reply($"在处理数据时发生了错误,请请向开发者反馈问题\n{e.Message}");

                Log.Error("JSON数据读取错误", e);
            }
        }
示例#4
0
        /// <summary>
        /// 此方法暂时弃用改用比利比利源
        /// </summary>
        private async Task <bool> KyoukaRank(string guildName)
        {
            //网络响应
            JObject response;

            //获取网络响应
            try
            {
                //获取查询结果
                ConsoleLog.Info("NET", $"尝试查询[{guildName}]会站排名");
                await QQGroup.SendGroupMessage($"正在查询公会[{guildName}]的排名...");

                ReqResponse reqResponse =
                    await Requests.PostAsync("https://service-kjcbcnmw-1254119946.gz.apigw.tencentcs.com/name/0",
                                             new ReqParams
                {
                    Timeout  = 3000,
                    PostJson = new JObject
                    {
                        ["clanName"] = guildName,
                        ["history"]  = 0
                    },
                    Header = new Dictionary <HttpRequestHeader, string>
                    {
                        { HttpRequestHeader.Referer, "https://kengxxiao.github.io/Kyouka/" }
                    },
                    CustomHeader = new Dictionary <string, string>
                    {
                        { "Custom-Source", "AntiRainBot" }
                    }
                });

                //判断响应
                if (reqResponse.StatusCode != HttpStatusCode.OK)
                {
                    await QQGroup.SendGroupMessage($"哇哦~发生了网络错误,请联系机器人所在服务器管理员\r\n{reqResponse.StatusCode}({(int)reqResponse.StatusCode})");

                    ConsoleLog.Error("网络发生错误", $"Code[{(int)reqResponse.StatusCode}]");
                    //阻止下一步处理
                    return(false);
                }
                //读取返回值
                response = reqResponse.Json();
                //判断空数据
                if (response == null || response.Count == 0)
                {
                    await QQGroup.SendGroupMessage("发生了未知错误,请请向开发者反馈问题");

                    ConsoleLog.Error("JSON数据读取错误", "从网络获取的文本为空");
                    return(false);
                }
                ConsoleLog.Info("获取JSON成功", response);
            }
            catch (Exception e)
            {
                await QQGroup.SendGroupMessage($"哇哦~发生了网络错误,请联系机器人所在服务器管理员\n{e.Message}");

                ConsoleLog.Error("网络发生错误", e);
                //阻止下一步处理
                return(false);
            }
            //JSON数据处理
            try
            {
                if (response["full"] == null)
                {
                    await QQGroup.SendGroupMessage("发生了未知错误,请请向开发者反馈问题");

                    ConsoleLog.Error("JSON数据读取错误", "从网络获取的JSON格式可能有问题");
                    return(false);
                }
                //在有查询结果时查找值
                if (!response["full"].ToString().Equals("0"))
                {
                    if (!response["full"].ToString().Equals("1"))
                    {
                        await QQGroup.SendGroupMessage("查询到多个公会,可能存在重名或关键词错误");
                    }
                    string rank       = response["data"]?[0]?["rank"]?.ToString();
                    string totalScore = response["data"]?[0]?["damage"]?.ToString();
                    string leaderName = response["data"]?[0]?["leader_name"]?.ToString();
                    ConsoleLog.Info("JSON处理成功", "向用户发送数据");
                    await QQGroup.SendGroupMessage("查询成功!\n" +
                                                   $"公会:{guildName}\n" +
                                                   $"排名:{rank}\n" +
                                                   $"总分数:{totalScore}\n" +
                                                   $"会长:{leaderName}\n" +
                                                   "如果查询到的信息有误,有可能关键词错误或公会排名在20060之后");
                }
                else
                {
                    await QQGroup.SendGroupMessage("未找到任意公会\n请检查是否查询的错误的公会名或公会排名在20060之后");

                    ConsoleLog.Info("JSON处理成功", "所查询列表为空");
                    return(false);
                }
                return(true);
            }
            catch (Exception e)
            {
                await QQGroup.SendGroupMessage($"在处理数据时发生了错误,请请向开发者反馈问题\n{e.Message}");

                ConsoleLog.Error("JSON数据读取错误", e);
                return(false);
            }
        }