From d1da2a00e9944667685c50d1e709bb9bf50e81e6 Mon Sep 17 00:00:00 2001 From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> Date: Thu, 4 Apr 2024 10:57:37 +0300 Subject: [PATCH] [Feature] Guild bans with seconds prune period (#2898) * rip legacy code * rename so no conflicts * eh renamed wrong thing --- .../Entities/Guilds/IGuild.cs | 23 +++++++++++++++++++ .../API/Rest/CreateGuildBanParams.cs | 13 ++++++----- src/Discord.Net.Rest/DiscordRestApiClient.cs | 19 +++++++-------- .../Entities/Guilds/GuildHelper.cs | 12 ++++++---- .../Entities/Guilds/RestGuild.cs | 7 ++++++ .../Entities/Guilds/SocketGuild.cs | 7 ++++++ 6 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs index 20bf3a7f..708e5830 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs +++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs @@ -544,6 +544,29 @@ namespace Discord /// A task that represents the asynchronous add operation for the ban. /// Task AddBanAsync(ulong userId, int pruneDays = 0, string reason = null, RequestOptions options = null); + + /// + /// Bans the user from this guild and optionally prunes their recent messages. + /// + /// The user to ban. + /// The number of seconds to remover messages from this user for, between 0 and 604800 + /// The options to be used when sending the request. + /// + /// A task that represents the asynchronous add operation for the ban. + /// + Task BanUserAsync(IUser user, uint pruneSeconds = 0, RequestOptions options = null); + + /// + /// Bans the user from this guild and optionally prunes their recent messages. + /// + /// The ID of the user to ban. + /// The number of seconds to remover messages from this user for, between 0 and 604800 + /// The options to be used when sending the request. + /// + /// A task that represents the asynchronous add operation for the ban. + /// + Task BanUserAsync(ulong userId, uint pruneSeconds = 0, RequestOptions options = null); + /// /// Unbans the user if they are currently banned. /// diff --git a/src/Discord.Net.Rest/API/Rest/CreateGuildBanParams.cs b/src/Discord.Net.Rest/API/Rest/CreateGuildBanParams.cs index 017f9539..e7e9be9c 100644 --- a/src/Discord.Net.Rest/API/Rest/CreateGuildBanParams.cs +++ b/src/Discord.Net.Rest/API/Rest/CreateGuildBanParams.cs @@ -1,8 +1,9 @@ -namespace Discord.API.Rest +using Newtonsoft.Json; + +namespace Discord.API.Rest; + +internal class CreateGuildBanParams { - internal class CreateGuildBanParams - { - public Optional DeleteMessageDays { get; set; } - public string Reason { get; set; } - } + [JsonProperty("delete_message_seconds")] + public uint DeleteMessageSeconds { get; set; } } diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index 4778f3ed..5c1b6c62 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -1743,22 +1743,23 @@ namespace Discord.API /// /// and must not be equal to zero. /// -and- - /// must be between 0 to 7. + /// must be between 0 and 604800. /// - /// must not be . - public Task CreateGuildBanAsync(ulong guildId, ulong userId, CreateGuildBanParams args, RequestOptions options = null) + public Task CreateGuildBanAsync(ulong guildId, ulong userId, uint deleteMessageSeconds, string reason, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotEqual(userId, 0, nameof(userId)); - Preconditions.NotNull(args, nameof(args)); - Preconditions.AtLeast(args.DeleteMessageDays, 0, nameof(args.DeleteMessageDays), "Prune length must be within [0, 7]"); - Preconditions.AtMost(args.DeleteMessageDays, 7, nameof(args.DeleteMessageDays), "Prune length must be within [0, 7]"); + + Preconditions.AtMost(deleteMessageSeconds, 604800, nameof(deleteMessageSeconds), "Prune length must be within [0, 604800]"); + + var data = new CreateGuildBanParams { DeleteMessageSeconds = deleteMessageSeconds }; + options = RequestOptions.CreateOrClone(options); var ids = new BucketIds(guildId: guildId); - if (!string.IsNullOrWhiteSpace(args.Reason)) - options.AuditLogReason = args.Reason; - return SendAsync("PUT", () => $"guilds/{guildId}/bans/{userId}?delete_message_days={args.DeleteMessageDays}", ids, options: options); + if (!string.IsNullOrWhiteSpace(reason)) + options.AuditLogReason = reason; + return SendJsonAsync("PUT", () => $"guilds/{guildId}/bans/{userId}", data, ids, options: options); } /// and must not be equal to zero. diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index d05352a0..2dae8fc5 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -235,11 +235,15 @@ namespace Discord.Rest return model == null ? null : RestBan.Create(client, model); } - public static Task AddBanAsync(IGuild guild, BaseDiscordClient client, - ulong userId, int pruneDays, string reason, RequestOptions options) + public static Task AddBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, int pruneDays, string reason, RequestOptions options) { - var args = new CreateGuildBanParams { DeleteMessageDays = pruneDays, Reason = reason }; - return client.ApiClient.CreateGuildBanAsync(guild.Id, userId, args, options); + Preconditions.AtLeast(pruneDays, 0, nameof(pruneDays), "Prune length must be within [0, 7]"); + return client.ApiClient.CreateGuildBanAsync(guild.Id, userId, (uint)pruneDays * 86400, reason, options); + } + + public static Task AddBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, uint pruneSeconds, RequestOptions options) + { + return client.ApiClient.CreateGuildBanAsync(guild.Id, userId, pruneSeconds, null, options); } public static Task RemoveBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, RequestOptions options) diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index 3ae3d17d..6818ecde 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -395,6 +395,13 @@ namespace Discord.Rest public Task AddBanAsync(ulong userId, int pruneDays = 0, string reason = null, RequestOptions options = null) => GuildHelper.AddBanAsync(this, Discord, userId, pruneDays, reason, options); + /// + public Task BanUserAsync(IUser user, uint pruneSeconds = 0, RequestOptions options = null) + => GuildHelper.AddBanAsync(this, Discord, user.Id, pruneSeconds, options); + /// + public Task BanUserAsync(ulong userId, uint pruneSeconds = 0, RequestOptions options = null) + => GuildHelper.AddBanAsync(this, Discord, userId, pruneSeconds, options); + /// public Task RemoveBanAsync(IUser user, RequestOptions options = null) => GuildHelper.RemoveBanAsync(this, Discord, user.Id, options); diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index b2b1e661..86335d0a 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -716,6 +716,13 @@ namespace Discord.WebSocket public Task AddBanAsync(ulong userId, int pruneDays = 0, string reason = null, RequestOptions options = null) => GuildHelper.AddBanAsync(this, Discord, userId, pruneDays, reason, options); + /// + public Task BanUserAsync(IUser user, uint pruneSeconds = 0, RequestOptions options = null) + => GuildHelper.AddBanAsync(this, Discord, user.Id, pruneSeconds, options); + /// + public Task BanUserAsync(ulong userId, uint pruneSeconds = 0, RequestOptions options = null) + => GuildHelper.AddBanAsync(this, Discord, userId, pruneSeconds, options); + /// public Task RemoveBanAsync(IUser user, RequestOptions options = null) => GuildHelper.RemoveBanAsync(this, Discord, user.Id, options);