Skip to content

Commit

Permalink
Prevent overflow when converting snowflake to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Benny- committed Nov 11, 2018
1 parent e05a32f commit 7e46786
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/src/nyxx/events/TypingEvent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TypingEvent {

this.channel = chan;
this.user = client.users[Snowflake(json['d']['user_id'] as String)];
if(this.user == null) return;

client._events.onTyping.add(this);
channel._onTyping.add(this);
});
Expand Down
3 changes: 1 addition & 2 deletions lib/src/nyxx/objects/Snowflake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class Snowflake implements Comparable<Snowflake> {
/// Returns timestamp included in [Snowflake]
/// [Snowflake reference](https://discordapp.com/developers/docs/reference#snowflakes)
DateTime get timestamp =>
DateTime.fromMillisecondsSinceEpoch((int.parse(_id) >> 22) + discordEpoch)
.toUtc();
DateTime.fromMillisecondsSinceEpoch((BigInt.parse(_id) >> 22).toInt() + discordEpoch, isUtc: true);

@override
String toString() => _id;
Expand Down
9 changes: 9 additions & 0 deletions test/travis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ void main() {

print("TESTING CLIENT INTERNALS");

nyxx.Snowflake a = nyxx.Snowflake.fromDateTime(new DateTime(2017));
nyxx.Snowflake b = nyxx.Snowflake.fromDateTime(new DateTime(2018));

assert(a.compareTo(b) > 0);
assert(b.compareTo(a) < 0);

assert(a.timestamp == new DateTime(2017));
assert(b.timestamp == new DateTime(2018));

assert(bot.channels.count > 0);
assert(bot.users.count > 0);
assert(bot.shards.length == 1);
Expand Down

0 comments on commit 7e46786

Please sign in to comment.