Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一些issue的处理 #85

Merged
merged 4 commits into from
Oct 14, 2023
Merged

一些issue的处理 #85

merged 4 commits into from
Oct 14, 2023

Conversation

Natsukage
Copy link
Contributor

修正OfErrorMessage的错误提示文字
公开MiraiHttpUtils中的PostJsonAsync与GetAsync方法,允许发送非标准EndPoints的请求
在InvalidResponseException的Data中提供更多的信息
允许用户传入自定义的错误处理方法,并通过ex.Data["msg"]等属性获得错误的具体信息
修复无法上传中文文件名群文件问题

@Natsukage
Copy link
Contributor Author

Natsukage commented Oct 12, 2023

关于自定义错误处理:接受一个exception类型evoid方法委托。

public static void CatchException(Exception e)
{
    if (!(e.Data["url"]?.ToString() ?? "").Contains("sendGroupMessage")) //只处理发送群消息的异常
    {
        string type = e.Data["code"]?.ToString() switch
        {
            "0" => "成功", // Success : StateCode(0, "success") // 成功
            "1" => "错误的verify key", // AuthKeyFail : StateCode(1, "Auth Key错误")
            "2" => "指定的Bot不存在", // NoBot : StateCode(2, "指定Bot不存在")
            "3" => "Session失效或不存在", // IllegalSession : StateCode(3, "Session失效或不存在")
            "4" => "Session未认证", // NotVerifySession : StateCode(4, "Session未认证")
            "5" => "指定对象不存在", // NoElement : StateCode(5, "指定对象不存在")
            "6" => "指定操作不支持", // NoOperateSupport : StateCode(6, "指定操作不支持")
            "10" => "Bot没有对应操作的权限", // PermissionDenied : StateCode(10, "无操作权限")
            "20" => "Bot被禁言", // BotMuted : StateCode(20, "Bot被禁言")
            "30" => "消息过长", // MessageTooLarge : StateCode(30, "消息过长")
            "400" => "无效参数", // InvalidParameter : StateCode(400, "无效参数")
            "500" => "内部错误", // class InternalError() : StateCode(500, "")
            null => "未知错误",
            _ => e.Data["code"].ToString(), // default case if code does not match any known codes
        }; 
        //考虑到Mirai随时可能会有改动,并且MAH文档常年不更新,code对应的具体错误信息不建议写在框架内
        string message = $"[{type}]{e.Data["msg"]}";

        //记录日志
        Log("错误", message, LoggerLevel.Error);
 
       //通过Bark向手机推送通知
        _ = "https://api.day.app"
            .AppendPathSegments(_barkAPI, "群消息错误", WebUtility.UrlEncode($"{message}\n发生于{DateTime.Now:MM-dd HH:mm:ss}"))
            .SetQueryParam("group", "群消息错误")
            .GetStringAsync(); 

        //发送好友信息出现问题时,不要循环发送。实际上前面已经筛选过只处理群消息了,所以这里一定是`!false`,只是做一个演示。
        if (!(e.Data["url"]?.ToString() ?? "").Contains("sendFriendMessage")) 
            _ = MessageManager.SendFriendMessageAsync(Common.MasterQQ, $"{message}\n发生于{DateTime.Now:MM-dd HH:mm:ss}");
    }
}

在初始化Bot时,传入委托即可
Mirai.Net.Utils.Internal.MiraiHttpUtils.HttpErrorHandler += CatchException;

@SinoAHpx SinoAHpx merged commit 3a52f56 into Mirai-NET-Shelter:2.4 Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants