[Feature] Guild bans with seconds prune period (#2898)

* rip legacy code

* rename so no conflicts

* eh renamed wrong thing
This commit is contained in:
Mihail Gribkov
2024-04-04 10:57:37 +03:00
committed by GitHub
parent 787a913625
commit d1da2a00e9
6 changed files with 62 additions and 19 deletions

View File

@@ -544,6 +544,29 @@ namespace Discord
/// A task that represents the asynchronous add operation for the ban.
/// </returns>
Task AddBanAsync(ulong userId, int pruneDays = 0, string reason = null, RequestOptions options = null);
/// <summary>
/// Bans the user from this guild and optionally prunes their recent messages.
/// </summary>
/// <param name="user">The user to ban.</param>
/// <param name="pruneSeconds">The number of seconds to remover messages from this user for, between 0 and 604800</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous add operation for the ban.
/// </returns>
Task BanUserAsync(IUser user, uint pruneSeconds = 0, RequestOptions options = null);
/// <summary>
/// Bans the user from this guild and optionally prunes their recent messages.
/// </summary>
/// <param name="userId">The ID of the user to ban.</param>
/// <param name="pruneSeconds">The number of seconds to remover messages from this user for, between 0 and 604800</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous add operation for the ban.
/// </returns>
Task BanUserAsync(ulong userId, uint pruneSeconds = 0, RequestOptions options = null);
/// <summary>
/// Unbans the user if they are currently banned.
/// </summary>

View File

@@ -1,8 +1,9 @@
namespace Discord.API.Rest
{
using Newtonsoft.Json;
namespace Discord.API.Rest;
internal class CreateGuildBanParams
{
public Optional<int> DeleteMessageDays { get; set; }
public string Reason { get; set; }
}
[JsonProperty("delete_message_seconds")]
public uint DeleteMessageSeconds { get; set; }
}

View File

@@ -1743,22 +1743,23 @@ namespace Discord.API
/// <exception cref="ArgumentException">
/// <paramref name="guildId"/> and <paramref name="userId"/> must not be equal to zero.
/// -and-
/// <paramref name="args.DeleteMessageDays"/> must be between 0 to 7.
/// <paramref name="deleteMessageSeconds"/> must be between 0 and 604800.
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="args"/> must not be <see langword="null"/>.</exception>
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);
}
/// <exception cref="ArgumentException"><paramref name="guildId"/> and <paramref name="userId"/> must not be equal to zero.</exception>

View File

@@ -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)

View File

@@ -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);
/// <inheritdoc />
public Task BanUserAsync(IUser user, uint pruneSeconds = 0, RequestOptions options = null)
=> GuildHelper.AddBanAsync(this, Discord, user.Id, pruneSeconds, options);
/// <inheritdoc />
public Task BanUserAsync(ulong userId, uint pruneSeconds = 0, RequestOptions options = null)
=> GuildHelper.AddBanAsync(this, Discord, userId, pruneSeconds, options);
/// <inheritdoc />
public Task RemoveBanAsync(IUser user, RequestOptions options = null)
=> GuildHelper.RemoveBanAsync(this, Discord, user.Id, options);

View File

@@ -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);
/// <inheritdoc />
public Task BanUserAsync(IUser user, uint pruneSeconds = 0, RequestOptions options = null)
=> GuildHelper.AddBanAsync(this, Discord, user.Id, pruneSeconds, options);
/// <inheritdoc />
public Task BanUserAsync(ulong userId, uint pruneSeconds = 0, RequestOptions options = null)
=> GuildHelper.AddBanAsync(this, Discord, userId, pruneSeconds, options);
/// <inheritdoc />
public Task RemoveBanAsync(IUser user, RequestOptions options = null)
=> GuildHelper.RemoveBanAsync(this, Discord, user.Id, options);