Check that messages older than two weeks are not passed to bulk delete
This resolves #477
This commit is contained in:
@@ -12,6 +12,8 @@ namespace Discord
|
||||
|
||||
public static DateTimeOffset FromSnowflake(ulong value)
|
||||
=> FromUnixMilliseconds((long)((value >> 22) + 1420070400000UL));
|
||||
public static ulong ToSnowflake(DateTimeOffset value)
|
||||
=> (ulong)(ToUnixMilliseconds(value) - 1420070400000L) << 22;
|
||||
|
||||
public static DateTimeOffset FromTicks(long ticks)
|
||||
=> new DateTimeOffset(ticks, TimeSpan.Zero);
|
||||
|
||||
@@ -181,5 +181,13 @@ namespace Discord
|
||||
if (msg == null) return new ArgumentException($"Value must be less than {value}", name);
|
||||
else return new ArgumentException(msg, name);
|
||||
}
|
||||
|
||||
// Bulk Delete
|
||||
public static void NoMessageOlderThanTwoWeeks(ulong[] collection, string name)
|
||||
{
|
||||
var minimum = DateTimeUtils.ToSnowflake(DateTimeOffset.Now.Subtract(TimeSpan.FromDays(14)));
|
||||
for (var i = 0; i < collection.Length; i++)
|
||||
if (collection[i] <= minimum) throw new ArgumentOutOfRangeException(name, "Messages must be younger than two weeks to delete.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -484,6 +484,7 @@ namespace Discord.API
|
||||
Preconditions.NotNull(args, nameof(args));
|
||||
Preconditions.NotNull(args.MessageIds, nameof(args.MessageIds));
|
||||
Preconditions.AtMost(args.MessageIds.Length, 100, nameof(args.MessageIds.Length));
|
||||
Preconditions.NoMessageOlderThanTwoWeeks(args.MessageIds, nameof(args.MessageIds));
|
||||
options = RequestOptions.CreateOrClone(options);
|
||||
|
||||
switch (args.MessageIds.Length)
|
||||
|
||||
Reference in New Issue
Block a user