Skip to content

Commit

Permalink
Delete a message
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Oct 11, 2024
1 parent e0e3479 commit ba14a49
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 17 deletions.
14 changes: 14 additions & 0 deletions src/QQBot.Net.Core/Entities/IDeletable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace QQBot;

/// <summary>
/// 表示实体对象可以被删除。
/// </summary>
public interface IDeletable
{
/// <summary>
/// 删除此对实体象及其所有子实体对象。
/// </summary>
/// <param name="hideTip"> 是否隐藏删除提示,仅在 <see cref="ITextChannel"/> 或 <see cref="IDMChannel"/> 内的消息支持。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
Task DeleteAsync(bool? hideTip = null, RequestOptions? options = null);
}
2 changes: 1 addition & 1 deletion src/QQBot.Net.Core/Entities/Messages/IUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace QQBot;
/// <summary>
/// 表示一个通用的由用户发送的消息。
/// </summary>
public interface IUserMessage : IMessage
public interface IUserMessage : IMessage, IDeletable
{

}
5 changes: 0 additions & 5 deletions src/QQBot.Net.Rest/API/Rest/DeleteChannelMessageParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ internal class DeleteChannelMessageParams
{
public bool? HideTip { get; set; }
}

internal class DeleteDirectMessageParams
{
public bool? HideTip { get; set; }
}
6 changes: 6 additions & 0 deletions src/QQBot.Net.Rest/API/Rest/DeleteDirectMessageParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace QQBot.API.Rest;

internal class DeleteDirectMessageParams
{
public bool? HideTip { get; set; }
}
31 changes: 30 additions & 1 deletion src/QQBot.Net.Rest/Entities/Messages/MessageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace QQBot.Rest;
using QQBot.API.Rest;

namespace QQBot.Rest;

internal static class MessageHelper
{
Expand All @@ -12,4 +14,31 @@ public static async Task<IUser> GetAuthorAsync(BaseQQBotClient client, IGuild? g
: null;
return author ?? RestGuildUser.Create(client, model);
}

public static async Task DeleteAsync(IUserMessage message, BaseQQBotClient client, bool? hideTip, RequestOptions? options)
{
switch (message.Channel)
{
case IUserChannel userChannel:
await client.ApiClient.DeleteUserMessageAsync(userChannel.Id, message.Id, options).ConfigureAwait(false);
break;
case IGroupChannel groupChannel:
await client.ApiClient.DeleteGroupMessageAsync(groupChannel.Id, message.Id, options).ConfigureAwait(false);
break;
case ITextChannel textChannel:
await client.ApiClient
.DeleteChannelMessageAsync(textChannel.Id, message.Id,
new DeleteChannelMessageParams { HideTip = hideTip }, options)
.ConfigureAwait(false);
break;
case IDMChannel dmChannel:
await client.ApiClient
.DeleteDirectMessageAsync(dmChannel.Id, message.Id,
new DeleteDirectMessageParams { HideTip = hideTip }, options)
.ConfigureAwait(false);
break;
default:
throw new NotSupportedException("Unsupported channel type.");
}
}
}
4 changes: 4 additions & 0 deletions src/QQBot.Net.Rest/Entities/Messages/RestUserMessage..cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ internal RestUserMessage(BaseQQBotClient client, string id,
entity.Update(model);
return entity;
}

/// <inheritdoc />
public Task DeleteAsync(bool? hideTip = null, RequestOptions? options = null) =>
MessageHelper.DeleteAsync(this, Client, hideTip, options);
}
22 changes: 12 additions & 10 deletions src/QQBot.Net.Rest/QQBotRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public async Task<SendUserGroupMessageResponse> SendUserMessageAsync(Guid openId
options = RequestOptions.CreateOrClone(options);

BucketIds ids = new();
string id = openId.ToString("N").ToUpperInvariant();
string id = openId.ToIdString();
return await SendJsonAsync<SendUserGroupMessageResponse>(HttpMethod.Post,
() => $"v2/users/{id}/messages", args, ids, ClientBucketType.SendEdit, false, options)
.ConfigureAwait(false);
Expand Down Expand Up @@ -548,27 +548,28 @@ public async Task<ChannelMessage> SendDirectMessageAsync(ulong directGuildId, Se
.ConfigureAwait(false);
}

public async Task DeleteUserMessageAsync(ulong openId, string messageId, RequestOptions? options = null)
public async Task DeleteUserMessageAsync(Guid openId, string messageId, RequestOptions? options = null)
{
Preconditions.NotEqual(openId, 0, nameof(openId));
Preconditions.NotEqual(openId, Guid.Empty, nameof(openId));
Preconditions.NotNullOrWhiteSpace(messageId, nameof(messageId));
options = RequestOptions.CreateOrClone(options);

BucketIds ids = new(0, openId);
BucketIds ids = new();
await SendAsync(HttpMethod.Delete,
() => $"v2/users/{openId}/messages/{messageId}", ids, ClientBucketType.SendEdit, options)
() => $"v2/users/{openId.ToIdString()}/messages/{messageId}", ids, ClientBucketType.SendEdit, options)
.ConfigureAwait(false);
}

public async Task DeleteGroupMessageAsync(ulong groupOpenId, string messageId, RequestOptions? options = null)
public async Task DeleteGroupMessageAsync(Guid groupOpenId, string messageId, RequestOptions? options = null)
{
Preconditions.NotEqual(groupOpenId, 0, nameof(groupOpenId));
Preconditions.NotEqual(groupOpenId, Guid.Empty, nameof(groupOpenId));
Preconditions.NotNullOrWhiteSpace(messageId, nameof(messageId));
options = RequestOptions.CreateOrClone(options);

BucketIds ids = new(0, groupOpenId);
BucketIds ids = new();
string id = groupOpenId.ToIdString();
await SendAsync(HttpMethod.Delete,
() => $"v2/groups/{groupOpenId}/messages/{messageId}", ids, ClientBucketType.SendEdit, options)
() => $"v2/groups/{id}/messages/{messageId}", ids, ClientBucketType.SendEdit, options)
.ConfigureAwait(false);
}

Expand All @@ -582,8 +583,9 @@ public async Task DeleteChannelMessageAsync(ulong channelId, string messageId, D
? $"?hidetip={args.HideTip.Value.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()}"
: string.Empty;
BucketIds ids = new(0, channelId);
string id = channelId.ToIdString();
await SendAsync(HttpMethod.Delete,
() => $"v2/groups/{channelId}/messages/{messageId}{query}", ids, ClientBucketType.SendEdit, options)
() => $"v2/groups/{id}/messages/{messageId}{query}", ids, ClientBucketType.SendEdit, options)
.ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using QQBot.API;
using QQBot.API.Gateway;
using QQBot.Rest;

namespace QQBot.WebSocket;

Expand Down Expand Up @@ -31,4 +32,8 @@ internal SocketUserMessage(QQBotSocketClient client, string id,
entity.Update(state, model);
return entity;
}

/// <inheritdoc />
public Task DeleteAsync(bool? hideTip = null, RequestOptions? options = null) =>
MessageHelper.DeleteAsync(this, Client, hideTip, options);
}

0 comments on commit ba14a49

Please sign in to comment.