Merge pull request #537 from RogueException/issue/477

Check that messages older than two weeks are not passed to bulk delete
This commit is contained in:
RogueException
2017-03-01 18:19:43 -04:00
committed by GitHub
3 changed files with 11 additions and 0 deletions

View File

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

View File

@@ -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 YoungerThanTwoWeeks(ulong[] collection, string name)
{
var minimum = DateTimeUtils.ToSnowflake(DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(1209540000)));
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.");
}
}
}

View File

@@ -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.YoungerThanTwoWeeks(args.MessageIds, nameof(args.MessageIds));
options = RequestOptions.CreateOrClone(options);
switch (args.MessageIds.Length)