Skip to content

Commit

Permalink
Create, modify, delete roles
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Nov 10, 2024
1 parent c94ebe4 commit 74c7a10
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 72 deletions.
17 changes: 17 additions & 0 deletions src/QQBot.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ public interface IGuild : IEntity<ulong>
/// <returns> 一个表示异步获取操作的任务。任务的结果包含此频道的所有角色。 </returns>
Task<IReadOnlyCollection<IRole>> GetRolesAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions? options = null);

/// <summary>
/// 获取此频道的角色。
/// </summary>
/// <param name="id"> 要获取的角色的 ID。 </param>
/// <param name="mode"> 指示当前方法是否应该仅从缓存中获取结果,还是可以通过 API 请求获取数据。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步获取操作的任务。任务的结果包含此频道的所有角色。 </returns>
Task<IRole?> GetRoleAsync(uint id, CacheMode mode = CacheMode.AllowDownload, RequestOptions? options = null);

/// <summary>
/// 在此服务器内创建一个新的角色。
/// </summary>
/// <param name="func"> 一个包含要应用到新创建角色的配置的委托。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步创建操作的任务。任务的结果包含新创建的角色。 </returns>
Task<IRole> CreateRoleAsync(Action<RoleProperties> func, RequestOptions? options = null);

#endregion

#region Users
Expand Down
13 changes: 12 additions & 1 deletion src/QQBot.Net.Core/Entities/Roles/IRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// 表示一个子频道身份组。
/// </summary>
public interface IRole : IEntity<uint>
public interface IRole : IEntity<uint>, IDeletable
{
/// <summary>
/// 获取拥有此角色的频道。
Expand Down Expand Up @@ -47,4 +47,15 @@ public interface IRole : IEntity<uint>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步获取操作的任务。任务的结果包含所有拥有此身份组的用户。 </returns>
IAsyncEnumerable<IReadOnlyCollection<IGuildMember>> GetUsersAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions? options = null);

/// <summary>
/// 修改此角色。
/// </summary>
/// <remarks>
/// 此方法使用指定的属性修改当前角色信息。要查看可用的属性,请参考 <see cref="QQBot.RoleProperties"/>。
/// </remarks>
/// <param name="func"> 一个包含修改角色属性的委托。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步修改操作的任务。 </returns>
Task ModifyAsync(Action<RoleProperties> func, RequestOptions? options = null);
}
23 changes: 23 additions & 0 deletions src/QQBot.Net.Core/Entities/Roles/RoleProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace QQBot;

/// <summary>
/// 提供用于创建与修改 <see cref="QQBot.IRole"/> 的属性。
/// </summary>
/// <seealso cref="QQBot.IGuild.CreateRoleAsync(System.Action{QQBot.RoleProperties},QQBot.RequestOptions)" />
public class RoleProperties
{
/// <summary>
/// 获取或设置要设置到此角色的名称。
/// </summary>
public string? Name { get; set; }

/// <summary>
/// 获取或设置要设置到此角色的颜色。
/// </summary>
public AlphaColor? Color { get; set; }

/// <summary>
/// 获取或设置要设置到此角色拥有此角色的用户是否在用户列表中与普通在线成员分开显示。
/// </summary>
public bool? IsHoisted { get; set; }
}
1 change: 0 additions & 1 deletion src/QQBot.Net.Core/Net/HttpException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Immutable;
using System.Net;

namespace QQBot.Net;
Expand Down
4 changes: 1 addition & 3 deletions src/QQBot.Net.Core/Net/Queue/BaseMessageQueue.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json;

namespace QQBot.Net.Queue;
namespace QQBot.Net.Queue;

/// <summary>
/// 表示一个通用的消息队列抽象类。
Expand Down
4 changes: 1 addition & 3 deletions src/QQBot.Net.Core/Net/Queue/MessageQueueProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json;

namespace QQBot.Net.Queue;
namespace QQBot.Net.Queue;

/// <summary>
/// 表示一个提供新的 <see cref="IMessageQueue"/> 实例的委托。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json;

namespace QQBot.Net.Queue.SynchronousImmediate;
namespace QQBot.Net.Queue.SynchronousImmediate;

/// <summary>
/// 表示一个同步处理消息队列。
Expand Down
1 change: 0 additions & 1 deletion src/QQBot.Net.Core/Utils/Paging/Page.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace QQBot
Expand Down
3 changes: 0 additions & 3 deletions src/QQBot.Net.Core/Utils/TokenUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Globalization;
using System.Text;

namespace QQBot;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/QQBot.Net.Rest/API/Rest/GetGuildRoleMembersParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Text.Json.Serialization;

namespace QQBot.API.Rest;

internal class GetGuildRoleMembersParams
Expand Down
30 changes: 26 additions & 4 deletions src/QQBot.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,37 @@ public static IAsyncEnumerable<IReadOnlyCollection<IGuildMember>> GetUsersAsync(

#region Roles



#endregion

public static async Task<IReadOnlyCollection<RestRole>> GetRolesAsync(IGuild guild,
BaseQQBotClient client, RequestOptions? options)
{
GetGuildRolesResponse model =
await client.ApiClient.GetGuildRolesAsync(guild.Id, options).ConfigureAwait(false);
return [..model.Roles.Select(x => RestRole.Create(guild, client, x))];
}

public static async Task<RestRole?> GetRoleAsync(IGuild guild,
BaseQQBotClient client, ulong id, RequestOptions? options)
{
GetGuildRolesResponse model =
await client.ApiClient.GetGuildRolesAsync(guild.Id, options).ConfigureAwait(false);
Role? role = model.Roles.FirstOrDefault(x => x.Id == id);
return role is not null ? RestRole.Create(guild, client, role) : null;
}

public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseQQBotClient client,
Action<RoleProperties>? func, RequestOptions? options)
{
RoleProperties properties = new();
func?.Invoke(properties);
CreateGuildRoleParams args = new()
{
Name = properties.Name,
Color = properties.Color,
Hoist = properties.IsHoisted
};
CreateGuildRoleResponse model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, args, options);
return RestRole.Create(guild, client, model.Role);
}

#endregion
}
48 changes: 34 additions & 14 deletions src/QQBot.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name,
public Task<IReadOnlyCollection<RestRole>> GetRolesAsync(RequestOptions? options = null) =>
GuildHelper.GetRolesAsync(this, Client, options);

/// <summary>
/// 获取此频道的角色。
/// </summary>
/// <param name="id"> 要获取的角色的 ID。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步获取操作的任务。任务的结果包含此频道的所有角色。 </returns>
public Task<RestRole?> GetRoleAsync(uint id, RequestOptions? options = null) =>
GuildHelper.GetRoleAsync(this, Client, id, options);

/// <inheritdoc cref="QQBot.IGuild.CreateRoleAsync(System.Action{QQBot.RoleProperties},QQBot.RequestOptions)" />
public Task<RestRole> CreateRoleAsync(Action<RoleProperties>? func = null, RequestOptions? options = null) =>
GuildHelper.CreateRoleAsync(this, Client, func, options);

#endregion

#region Users
Expand Down Expand Up @@ -398,29 +411,36 @@ async Task<IReadOnlyCollection<ICategoryChannel>> IGuild.GetCategoryChannelsAsyn
async Task<ICategoryChannel?> IGuild.GetCategoryChannelAsync(ulong id, CacheMode mode, RequestOptions? options) =>
mode == CacheMode.AllowDownload ? await GetCategoryChannelAsync(id, options).ConfigureAwait(false) : null;

async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, Action<CreateTextChannelProperties>? action, RequestOptions? options) =>
await CreateTextChannelAsync(name, action, options).ConfigureAwait(false);
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, Action<CreateTextChannelProperties>? func, RequestOptions? options) =>
await CreateTextChannelAsync(name, func, options).ConfigureAwait(false);

async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, Action<CreateVoiceChannelProperties>? action, RequestOptions? options) =>
await CreateVoiceChannelAsync(name, action, options).ConfigureAwait(false);
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, Action<CreateVoiceChannelProperties>? func, RequestOptions? options) =>
await CreateVoiceChannelAsync(name, func, options).ConfigureAwait(false);

async Task<ILiveStreamChannel> IGuild.CreateLiveStreamChannelAsync(string name, Action<CreateLiveStreamChannelProperties>? action, RequestOptions? options) =>
await CreateLiveStreamChannelAsync(name, action, options).ConfigureAwait(false);
async Task<ILiveStreamChannel> IGuild.CreateLiveStreamChannelAsync(string name, Action<CreateLiveStreamChannelProperties>? func, RequestOptions? options) =>
await CreateLiveStreamChannelAsync(name, func, options).ConfigureAwait(false);

async Task<IApplicationChannel> IGuild.CreateApplicationChannelAsync(string name, Action<CreateApplicationChannelProperties>? action, RequestOptions? options) =>
await CreateApplicationChannelAsync(name, action, options).ConfigureAwait(false);
async Task<IApplicationChannel> IGuild.CreateApplicationChannelAsync(string name, Action<CreateApplicationChannelProperties>? func, RequestOptions? options) =>
await CreateApplicationChannelAsync(name, func, options).ConfigureAwait(false);

async Task<IForumChannel> IGuild.CreateForumChannelAsync(string name, Action<CreateForumChannelProperties>? action, RequestOptions? options) =>
await CreateForumChannelAsync(name, action, options).ConfigureAwait(false);
async Task<IForumChannel> IGuild.CreateForumChannelAsync(string name, Action<CreateForumChannelProperties>? func, RequestOptions? options) =>
await CreateForumChannelAsync(name, func, options).ConfigureAwait(false);

async Task<IScheduleChannel> IGuild.CreateScheduleChannelAsync(string name, Action<CreateScheduleChannelProperties>? action, RequestOptions? options) =>
await CreateScheduleChannelAsync(name, action, options).ConfigureAwait(false);
async Task<IScheduleChannel> IGuild.CreateScheduleChannelAsync(string name, Action<CreateScheduleChannelProperties>? func, RequestOptions? options) =>
await CreateScheduleChannelAsync(name, func, options).ConfigureAwait(false);

async Task<ICategoryChannel> IGuild.CreateCategoryChannelAsync(string name, Action<CreateCategoryChannelProperties>? action, RequestOptions? options) =>
await CreateCategoryChannelAsync(name, action, options).ConfigureAwait(false);
async Task<ICategoryChannel> IGuild.CreateCategoryChannelAsync(string name, Action<CreateCategoryChannelProperties>? func, RequestOptions? options) =>
await CreateCategoryChannelAsync(name, func, options).ConfigureAwait(false);

async Task<IReadOnlyCollection<IRole>> IGuild.GetRolesAsync(CacheMode mode, RequestOptions? options) =>
mode is CacheMode.CacheOnly ? ([]) : await GetRolesAsync(options).ConfigureAwait(false);

async Task<IRole?> IGuild.GetRoleAsync(uint id, CacheMode mode, RequestOptions? options) =>
mode is CacheMode.CacheOnly ? null : await GetRoleAsync(id, options).ConfigureAwait(false);

/// <inheritdoc />
async Task<IRole> IGuild.CreateRoleAsync(Action<RoleProperties> func, RequestOptions? options) =>
await CreateRoleAsync(func, options);

#endregion
}
1 change: 0 additions & 1 deletion src/QQBot.Net.Rest/Entities/Messages/MessageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Text.RegularExpressions;
using QQBot.API.Rest;

namespace QQBot.Rest;
Expand Down
11 changes: 11 additions & 0 deletions src/QQBot.Net.Rest/Entities/Roles/RestRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ internal void Update(Model model)
public IAsyncEnumerable<IReadOnlyCollection<RestGuildMember>> GetUsersAsync(RequestOptions? options = null) =>
RoleHelper.GetUsersAsync(this, Client, null, options);

/// <inheritdoc />
public async Task ModifyAsync(Action<RoleProperties> func, RequestOptions? options = null)
{
Model model = await RoleHelper.ModifyAsync(this, Client, func, options);
Update(model);
}

/// <inheritdoc />
public Task DeleteAsync(RequestOptions? options = null) =>
RoleHelper.DeleteAsync(this, Client, options);

/// <inheritdoc cref="Name" />
public override string ToString() => Name;

Expand Down
30 changes: 29 additions & 1 deletion src/QQBot.Net.Rest/Entities/Roles/RoleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ namespace QQBot.Rest;

internal static class RoleHelper
{
#region Roles

public static async Task<API.Role> ModifyAsync(IRole role, BaseQQBotClient client,
Action<RoleProperties>? func, RequestOptions? options)
{
RoleProperties properties = new()
{
Name = role.Name,
Color = role.Color,
IsHoisted = role.IsHoisted
};
func?.Invoke(properties);
ModifyGuildRoleParams args = new()
{
Name = properties.Name,
Color = properties.Color,
Hoist = properties.IsHoisted
};
ModifyGuildRoleResponse model = await client.ApiClient
.ModifyGuildRoleAsync(role.Guild.Id, role.Id, args, options);
return model.Role;
}

#endregion

#region Users

public static async IAsyncEnumerable<IReadOnlyCollection<RestGuildMember>> GetUsersAsync(
Expand All @@ -16,7 +41,7 @@ public static async IAsyncEnumerable<IReadOnlyCollection<RestGuildMember>> GetUs
{
GetGuildRoleMembersParams args = new()
{
Limit = QQBotConfig.MaxMembersPerBatch,
Limit = Math.Clamp(limit ?? QQBotConfig.MaxMembersPerBatch, 1, QQBotConfig.MaxMembersPerBatch),
StartIndex = startIndex
};
GetGuildRoleMembersResponse model = await client.ApiClient
Expand All @@ -31,4 +56,7 @@ public static async IAsyncEnumerable<IReadOnlyCollection<RestGuildMember>> GetUs
}

#endregion

public static async Task DeleteAsync(IRole role, BaseQQBotClient client, RequestOptions? options) =>
await client.ApiClient.DeleteGuildRoleAsync(role.Guild.Id, role.Id, options).ConfigureAwait(false);
}
3 changes: 1 addition & 2 deletions src/QQBot.Net.Rest/Net/Converters/HexAlphaColorConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace QQBot.Net.Converters;
Expand Down
1 change: 0 additions & 1 deletion src/QQBot.Net.WebSocket/API/Gateway/GatewaySocketFrame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace QQBot.API.Gateway;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Immutable;
using System.Diagnostics;
using QQBot.Rest;

Expand Down
Loading

0 comments on commit 74c7a10

Please sign in to comment.