Skip to content

Commit

Permalink
重构部分事件,添加断线重连功能,修复事件未注册时调用会报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
XeronOwO committed Dec 9, 2023
1 parent c5676e4 commit 345f6bd
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 176 deletions.
65 changes: 56 additions & 9 deletions QQBot4Sharp/BotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,37 @@ internal async Task SendReadyEventAsync(ContextEventArgs e)
{
try
{
await OnReadyAsync?.Invoke(this, e);
if (OnReadyAsync != null)
{
await OnReadyAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Log.Error(ex, "处理READY事件时发生异常");
}
}

/// <summary>
/// RESUMED事件(恢复登录态成功后,QQ 后台会下发一个 Resumed Event)
/// </summary>
public event AsyncEventHandler<ContextEventArgs> OnResumedAsync;

internal async Task SendResumeEventAsync(ContextEventArgs e)
{
try
{
if (OnResumedAsync != null)
{
await OnResumedAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Log.Error(ex, "处理RESUMED事件时发生异常");
}
}

/// <summary>
/// 文字子频道全量消息(私域)<br/>用户在文字子频道内发送的所有聊天消息(私域)
/// </summary>
Expand All @@ -107,7 +130,10 @@ internal async Task SendCreateMessageEventAsync(GuildMessageEventArgs e)
{
try
{
await OnMessageCreateAsync?.Invoke(this, e);
if (OnMessageCreateAsync != null)
{
await OnMessageCreateAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand All @@ -124,7 +150,10 @@ internal async Task SendAtMessageCreateEventAsync(AtMessageEventArgs e)
{
try
{
await OnAtMessageCreateAsync?.Invoke(this, e);
if (OnAtMessageCreateAsync != null)
{
await OnAtMessageCreateAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand All @@ -141,7 +170,10 @@ internal async Task SendDirectMessageCreateEventAsync(DirectMessageEventArgs e)
{
try
{
await OnDirectMessageCreateAsync?.Invoke(this, e);
if (OnDirectMessageCreateAsync != null)
{
await OnDirectMessageCreateAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand All @@ -158,7 +190,10 @@ internal async Task SendC2CMessageCreateEventAsync(Models.QQ.QQMessageEventArgs
{
try
{
await OnC2CMessageCreateAsync?.Invoke(this, e);
if (OnC2CMessageCreateAsync != null)
{
await OnC2CMessageCreateAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand All @@ -175,7 +210,10 @@ internal async Task SendGroupAtMessageCreateEventAsync(Models.QQ.QQMessageEventA
{
try
{
await OnGroupAtMessageCreateAsync?.Invoke(this, e);
if (OnGroupAtMessageCreateAsync != null)
{
await OnGroupAtMessageCreateAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand All @@ -192,7 +230,10 @@ internal async Task SendMessageReactionAddEventAsync(MessageReactionEventArgs e)
{
try
{
await OnMessageReactionAddAsync?.Invoke(this, e);
if (OnMessageReactionAddAsync != null)
{
await OnMessageReactionAddAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand All @@ -209,7 +250,10 @@ internal async Task SendMessageReactionRemoveEventAsync(MessageReactionEventArgs
{
try
{
await OnMessageReactionRemoveAsync?.Invoke(this, e);
if (OnMessageReactionRemoveAsync != null)
{
await OnMessageReactionRemoveAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand All @@ -226,7 +270,10 @@ internal async Task SendInteractionCreateEventAsync(InteractionEventArgs e)
{
try
{
await OnInteractionCreateAsync?.Invoke(this, e);
if (OnInteractionCreateAsync != null)
{
await OnInteractionCreateAsync.Invoke(this, e);
}
}
catch (Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions QQBot4Sharp/Internal/API/Payload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal struct Payload
/// 下行消息都会有一个序列号,标识消息的唯一性,客户端需要再发送心跳的时候,携带客户端收到的最新的s
/// </summary>
[JsonProperty("s")]
public int? Serial;
public int? Seq;

/// <summary>
/// 代表事件类型。主要是用在op为 0 Dispatch 的时候
Expand All @@ -38,7 +38,7 @@ public Payload<T> Cast<T>()
{
OpCode = OpCode,
Data = JsonConvert.DeserializeObject<T>(Data.ToString(Formatting.None)),
Serial = Serial,
Seq = Seq,
Type = Type,
};
}
Expand Down
2 changes: 1 addition & 1 deletion QQBot4Sharp/Internal/API/Payload`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal struct Payload<T>
/// 下行消息都会有一个序列号,标识消息的唯一性,客户端需要再发送心跳的时候,携带客户端收到的最新的s
/// </summary>
[JsonProperty("s")]
public int? Serial;
public int? Seq;

/// <summary>
/// 代表事件类型。主要是用在op为 0 Dispatch 的时候
Expand Down
19 changes: 19 additions & 0 deletions QQBot4Sharp/Internal/API/ResumeReq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace QQBot4Sharp.Internal.API
{
internal struct ResumeReq
{
[JsonProperty("token")]
public string Token { get; set; }

[JsonProperty("session_id")]
public string SessionID { get; set; }

[JsonProperty("seq")]
public int? Seq { get; set; }
}
}

0 comments on commit 345f6bd

Please sign in to comment.