Skip to content

Commit

Permalink
Merge pull request #120 from mineral-dart/feat/snowflake-timestamp
Browse files Browse the repository at this point in the history
Feat: Implement snowflake timestamp
  • Loading branch information
vic256 authored Jun 20, 2023
2 parents 50e6308 + f3ad0e7 commit e3d6d61
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/framework.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export 'src/internal/entities/interactive_components/interactive_user_menu.dart'
export 'src/internal/entities/interactive_components/interactive_modal.dart';

export 'src/internal/mixins/collection.dart';
export 'src/internal/mixins/string.dart';
export 'src/internal/mixins/string.dart';
export 'src/internal/mixins/snowflake_timestamp.dart';
3 changes: 3 additions & 0 deletions lib/src/api/guilds/guild.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class Guild {
/// The [Guild]'s id.
Snowflake get id => _id;

/// The [Guild]'s creation date.
DateTime get createdAt => _id.dateTime;

/// The [Guild]'s name.
String get name => _name;

Expand Down
3 changes: 3 additions & 0 deletions lib/src/api/guilds/guild_scheduled_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class GuildScheduledEvent {
/// Get the id of this.
Snowflake get id => _id;

/// Get the creation date of this.
DateTime get createdAt => _id.dateTime;

/// Get the [Guild] of this.
Guild? get guild => ioc.use<MineralClient>().guilds.cache.get(_guildId);

Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/role.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:mineral/core/api.dart';
import 'package:mineral/exception.dart';
import 'package:mineral/src/api/managers/guild_role_manager.dart';
import 'package:mineral/src/helper.dart';
import 'package:mineral/src/internal/mixins/snowflake_timestamp.dart';
import 'package:mineral_ioc/ioc.dart';

class Tag {
Expand Down Expand Up @@ -50,6 +51,7 @@ class Role {
bool get isMentionable => _mentionable;
Tag? get tags => _tags;
GuildRoleManager get manager => _manager;
DateTime get createdAt => _id.dateTime;

/// ### Modifies the [label] of the role.
///
Expand Down
1 change: 1 addition & 0 deletions lib/src/api/sticker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Sticker {
StickerType get type => _type;
FormatType get format => _format;
int? get sortValue => _sortValue;
DateTime get createdAt => _id.dateTime;

/// Set name of this
///
Expand Down
4 changes: 4 additions & 0 deletions lib/src/api/users/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class User {
/// Returns the unique identifier of the user as a [Snowflake].
Snowflake get id => _id;

/// Returns the creation date of the user as a [DateTime].
DateTime get createdAt => _id.dateTime;

/// Returns the username of the user as a [String].
String get username => _username;

Expand Down Expand Up @@ -72,6 +75,7 @@ class User {
/// Returns the user's locale as a [Locale] enum value based on the value of language
Locale get lang => Locale.values.firstWhere((element) => element.locale == _lang);

/// Returns a [bool] indicating whether the user use the new Discord username system.
bool get isMigrated => _discriminator == '0';

/// Return [GuildMember] of [Guild] context for this
Expand Down
1 change: 1 addition & 0 deletions lib/src/api/webhook.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Webhook {
String? get url => _url;
SourceChannel? get sourceChannel => _sourceChannel;
SourceGuild? get sourceGuild => _sourceGuild;
DateTime get createdAt => _id.dateTime;

/// ### Update the label of this
///
Expand Down
3 changes: 3 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class Constants {
// Version of API
static const int apiVersion = 10;

// Discord epoch
static const int discordEpoch = 1420070400000;

// Name of Minéral
static const String name = "Mineral";

Expand Down
9 changes: 9 additions & 0 deletions lib/src/internal/mixins/snowflake_timestamp.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:mineral/core.dart';
import 'package:mineral/core/api.dart';

extension SnowflakeTimestamp on Snowflake {
DateTime get dateTime {
int timestamp = (int.parse(this) >> 22) + Constants.discordEpoch;
return DateTime.fromMillisecondsSinceEpoch(timestamp);
}
}

0 comments on commit e3d6d61

Please sign in to comment.