Skip to content

Commit

Permalink
[Feature] Guild bans with seconds prune period (#2898)
Browse files Browse the repository at this point in the history
* rip legacy code

* rename so no conflicts

* eh renamed wrong thing
  • Loading branch information
Misha-133 committed Apr 4, 2024
1 parent 787a913 commit d1da2a0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
23 changes: 23 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,29 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// 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>
Expand Down
13 changes: 7 additions & 6 deletions src/Discord.Net.Rest/API/Rest/CreateGuildBanParams.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace Discord.API.Rest
using Newtonsoft.Json;

namespace Discord.API.Rest;

internal class CreateGuildBanParams
{
internal class CreateGuildBanParams
{
public Optional<int> DeleteMessageDays { get; set; }
public string Reason { get; set; }
}
[JsonProperty("delete_message_seconds")]
public uint DeleteMessageSeconds { get; set; }
}
19 changes: 10 additions & 9 deletions src/Discord.Net.Rest/DiscordRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1743,22 +1743,23 @@ public async Task<Ban> GetGuildBanAsync(ulong guildId, ulong userId, RequestOpti
/// <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>
Expand Down
12 changes: 8 additions & 4 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,15 @@ public static async Task<RestBan> GetBanAsync(IGuild guild, BaseDiscordClient cl
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)
Expand Down
7 changes: 7 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ public Task AddBanAsync(IUser user, int pruneDays = 0, string reason = null, Req
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);
Expand Down
7 changes: 7 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,13 @@ public Task AddBanAsync(IUser user, int pruneDays = 0, string reason = null, Req
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);
Expand Down

0 comments on commit d1da2a0

Please sign in to comment.