From 079437a53120b6b1d200a8c15adc6101c3aced3e Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Tue, 11 Jun 2024 22:06:57 +0200 Subject: [PATCH 1/7] replace enum with an enum like --- lib/src/gateway/gateway.dart | 4 + .../http/managers/interaction_manager.dart | 5 + lib/src/http/managers/message_manager.dart | 3 + lib/src/models/application.dart | 72 +++---- lib/src/models/channel/channel.dart | 65 +++--- lib/src/models/channel/stage_instance.dart | 19 +- lib/src/models/channel/types/forum.dart | 47 ++--- lib/src/models/channel/voice_channel.dart | 23 +-- .../models/commands/application_command.dart | 28 ++- .../commands/application_command_option.dart | 59 +++--- .../application_command_permissions.dart | 28 ++- lib/src/models/entitlement.dart | 46 +++-- lib/src/models/guild/audit_log.dart | 194 +++++++++++------- lib/src/models/guild/auto_moderation.dart | 101 +++------ lib/src/models/guild/guild.dart | 149 ++++---------- lib/src/models/guild/integration.dart | 23 +-- lib/src/models/guild/onboarding.dart | 23 +-- lib/src/models/guild/scheduled_event.dart | 51 ++--- lib/src/models/interaction.dart | 69 +++---- lib/src/models/invite/invite.dart | 26 +-- lib/src/models/locale.dart | 113 ++++++---- lib/src/models/message/activity.dart | 27 +-- lib/src/models/message/component.dart | 110 ++++------ lib/src/models/message/message.dart | 131 +++++++----- lib/src/models/message/poll.dart | 21 +- lib/src/models/permission_overwrite.dart | 23 +-- lib/src/models/presence.dart | 69 +++---- lib/src/models/sku.dart | 33 +-- lib/src/models/sticker/sticker.dart | 49 ++--- lib/src/models/team.dart | 54 ++--- lib/src/models/user/connection.dart | 107 +++++----- lib/src/models/user/user.dart | 31 +-- lib/src/models/webhook.dart | 25 +-- lib/src/utils/enum_like.dart | 24 +++ 34 files changed, 835 insertions(+), 1017 deletions(-) create mode 100644 lib/src/utils/enum_like.dart diff --git a/lib/src/gateway/gateway.dart b/lib/src/gateway/gateway.dart index ccc2c11bc..ed0aea300 100644 --- a/lib/src/gateway/gateway.dart +++ b/lib/src/gateway/gateway.dart @@ -1019,6 +1019,8 @@ class Gateway extends GatewayManager with EventParser { InteractionCreateEvent> parseInteractionCreate(Map raw) { final interaction = client.interactions.parse(raw); + assert(!interaction.type.isUnknown, 'Unknown interaction type: ${interaction.type}'); + // Needed to get proper type promotion. return switch (interaction.type) { InteractionType.ping => InteractionCreateEvent(gateway: this, interaction: interaction as PingInteraction), @@ -1029,6 +1031,8 @@ class Gateway extends GatewayManager with EventParser { InteractionType.modalSubmit => InteractionCreateEvent(gateway: this, interaction: interaction as ModalSubmitInteraction), InteractionType.applicationCommandAutocomplete => InteractionCreateEvent(gateway: this, interaction: interaction as ApplicationCommandAutocompleteInteraction), + // stub, should never be reached + InteractionType() => null, } as InteractionCreateEvent>; } diff --git a/lib/src/http/managers/interaction_manager.dart b/lib/src/http/managers/interaction_manager.dart index a1d3266fd..9ac5d494e 100644 --- a/lib/src/http/managers/interaction_manager.dart +++ b/lib/src/http/managers/interaction_manager.dart @@ -34,6 +34,9 @@ class InteractionManager { Interaction parse(Map raw) { final type = InteractionType.parse(raw['type'] as int); + + assert (!type.isUnknown, 'Unknown interaction type: $type'); + final guildId = maybeParse(raw['guild_id'], Snowflake.parse); final channelId = maybeParse(raw['channel_id'], Snowflake.parse); final id = Snowflake.parse(raw['id']!); @@ -167,6 +170,8 @@ class InteractionManager { authorizingIntegrationOwners: authorizingIntegrationOwners, context: context, ), + // stub, should never be reached + InteractionType() => null, } as Interaction; } diff --git a/lib/src/http/managers/message_manager.dart b/lib/src/http/managers/message_manager.dart index c151f98b6..9dbd0aeee 100644 --- a/lib/src/http/managers/message_manager.dart +++ b/lib/src/http/managers/message_manager.dart @@ -247,6 +247,8 @@ class MessageManager extends Manager { MessageComponent parseMessageComponent(Map raw) { final type = MessageComponentType.parse(raw['type'] as int); + assert (!type.isUnknown, 'Unknown message component type: $type'); + return switch (type) { MessageComponentType.actionRow => ActionRowComponent( components: parseMany(raw['components'] as List, parseMessageComponent), @@ -285,6 +287,7 @@ class MessageManager extends Manager { maxValues: raw['max_values'] as int?, isDisabled: raw['disabled'] as bool?, ), + MessageComponentType() => throw StateError('Unknown message component type: $type'), }; } diff --git a/lib/src/models/application.dart b/lib/src/models/application.dart index 3f07fb798..ad7909368 100644 --- a/lib/src/models/application.dart +++ b/lib/src/models/application.dart @@ -9,6 +9,7 @@ import 'package:nyxx/src/models/sku.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/team.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/flags.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; @@ -183,28 +184,18 @@ class Application extends PartialApplication { ); } -enum ApplicationIntegrationType { +final class ApplicationIntegrationType extends EnumLike { /// App is installable to servers. - guildInstall._(0), + static const ApplicationIntegrationType guildInstall = ApplicationIntegrationType._(0); /// App is installable to users. - userInstall._(1); + static const ApplicationIntegrationType userInstall = ApplicationIntegrationType._(1); - /// The value of this [ApplicationIntegrationType]. - final int value; + static const List values = [guildInstall, userInstall]; - const ApplicationIntegrationType._(this.value); + factory ApplicationIntegrationType.parse(int value) => parseEnum(values, value); - /// Parse an [ApplicationIntegrationType] from an [int]. - /// - /// The [value] must be a valid application integration type. - factory ApplicationIntegrationType.parse(int value) => ApplicationIntegrationType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown ApplicationIntegrationType', value), - ); - - @override - String toString() => 'ApplicationIntegrationType($value)'; + const ApplicationIntegrationType._(super.value); } /// Flags for an [Application]. @@ -326,29 +317,28 @@ class ApplicationRoleConnectionMetadata with ToStringHelper { } /// The type of an [ApplicationRoleConnectionMetadata]. -enum ConnectionMetadataType { - integerLessThanOrEqual._(1), - integerGreaterThanOrEqual._(2), - integerEqual._(3), - integerNotEqual._(4), - dateTimeLessThanOrEqual._(5), - dateTimeGreaterThanOrEqual._(6), - booleanEqual._(7), - booleanNotEqual._(8); - - /// The value of this [ConnectionMetadataType]. - final int value; - - const ConnectionMetadataType._(this.value); - - /// Parse a [ConnectionMetadataType] from an [int]. - /// - /// The [value] must be a valid connection metadata type. - factory ConnectionMetadataType.parse(int value) => ConnectionMetadataType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown connection metadata type', value), - ); - - @override - String toString() => 'ConnectionMetadataType($value)'; +final class ConnectionMetadataType extends EnumLike { + static const ConnectionMetadataType integerLessThanOrEqual = ConnectionMetadataType._(1); + static const ConnectionMetadataType integerGreaterThanOrEqual = ConnectionMetadataType._(2); + static const ConnectionMetadataType integerEqual = ConnectionMetadataType._(3); + static const ConnectionMetadataType integerNotEqual = ConnectionMetadataType._(4); + static const ConnectionMetadataType dateTimeLessThanOrEqual = ConnectionMetadataType._(5); + static const ConnectionMetadataType dateTimeGreaterThanOrEqual = ConnectionMetadataType._(6); + static const ConnectionMetadataType booleanEqual = ConnectionMetadataType._(7); + static const ConnectionMetadataType booleanNotEqual = ConnectionMetadataType._(8); + + static const values = [ + integerLessThanOrEqual, + integerGreaterThanOrEqual, + integerEqual, + integerNotEqual, + dateTimeLessThanOrEqual, + dateTimeGreaterThanOrEqual, + booleanEqual, + booleanNotEqual, + ]; + + const ConnectionMetadataType._(super.value); + + factory ConnectionMetadataType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/channel/channel.dart b/lib/src/models/channel/channel.dart index e455b02cc..63eb7034d 100644 --- a/lib/src/models/channel/channel.dart +++ b/lib/src/models/channel/channel.dart @@ -2,6 +2,7 @@ import 'package:nyxx/src/builders/builder.dart'; import 'package:nyxx/src/http/managers/channel_manager.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/flags.dart'; /// A partial [Channel] object. @@ -48,61 +49,65 @@ abstract class Channel extends PartialChannel { } /// The type of a channel. -enum ChannelType { +final class ChannelType extends EnumLike { /// A text channel in a [Guild]. - guildText._(0), + static const guildText = ChannelType._(0); /// A DM channel with a single other recipient. - dm._(1), + static const dm = ChannelType._(1); /// A voice channel in a [Guild]. - guildVoice._(2), + static const guildVoice = ChannelType._(2); /// A DM channel with multiple recipients. - groupDm._(3), + static const groupDm = ChannelType._(3); /// A category in a [Guild]. - guildCategory._(4), + static const guildCategory = ChannelType._(4); /// An announcement channel in a [Guild]. - guildAnnouncement._(5), + static const guildAnnouncement = ChannelType._(5); /// A [Thread] in an announcement channel. - announcementThread._(10), + static const announcementThread = ChannelType._(10); /// A public thread. - publicThread._(11), + static const publicThread = ChannelType._(11); /// A private thread. - privateThread._(12), + static const privateThread = ChannelType._(12); /// A stage channel in a [Guild]. - guildStageVoice._(13), + static const guildStageVoice = ChannelType._(13); /// A [Guild] directory. - guildDirectory._(14), + static const guildDirectory = ChannelType._(14); /// A forum channel in a [Guild]. - guildForum._(15), + static const guildForum = ChannelType._(15); /// A media channel in a [Guild]. - guildMedia._(16); - - /// The value of this [ChannelType]. - final int value; - - const ChannelType._(this.value); - - /// Parse a [ChannelType] from a [value]. - /// - /// The [value] must be a valid channel type. - factory ChannelType.parse(int value) => ChannelType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown channel type', value), - ); - - @override - String toString() => 'ChannelType($value)'; + static const guildMedia = ChannelType._(16); + + static const List values = [ + guildText, + dm, + guildVoice, + groupDm, + guildCategory, + guildAnnouncement, + announcementThread, + publicThread, + privateThread, + guildStageVoice, + guildDirectory, + guildForum, + guildMedia, + ]; + + const ChannelType._(super.value); + + factory ChannelType.parse(int value) => parseEnum(values, value); } /// A set of flags applied to channels. diff --git a/lib/src/models/channel/stage_instance.dart b/lib/src/models/channel/stage_instance.dart index b18f1b28a..1ef595093 100644 --- a/lib/src/models/channel/stage_instance.dart +++ b/lib/src/models/channel/stage_instance.dart @@ -6,6 +6,7 @@ import 'package:nyxx/src/models/guild/guild.dart'; import 'package:nyxx/src/models/guild/scheduled_event.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; /// {@template stage_instance} /// Information about a live stage. @@ -58,19 +59,13 @@ class StageInstance extends SnowflakeEntity { } /// The privacy level of a [StageInstance]. -enum PrivacyLevel { - public._(1), - guildOnly._(2); +final class PrivacyLevel extends EnumLike { + static const public = PrivacyLevel._(1); + static const guildOnly = PrivacyLevel._(2); - final int value; + static const List values = [public, guildOnly]; - const PrivacyLevel._(this.value); + const PrivacyLevel._(super.value); - /// Parse a [PrivacyLevel] from an [int]. - /// - /// The [value] must be a valid privacy level. - factory PrivacyLevel.parse(int value) => PrivacyLevel.values.firstWhere( - (level) => level.value == value, - orElse: () => throw FormatException('Unknown privacy level', value), - ); + factory PrivacyLevel.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/channel/types/forum.dart b/lib/src/models/channel/types/forum.dart index 79ece2592..e4724266d 100644 --- a/lib/src/models/channel/types/forum.dart +++ b/lib/src/models/channel/types/forum.dart @@ -12,6 +12,7 @@ import 'package:nyxx/src/models/invite/invite_metadata.dart'; import 'package:nyxx/src/models/permission_overwrite.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/webhook.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template forum_channel} @@ -184,46 +185,26 @@ class DefaultReaction with ToStringHelper { } /// The sorting order in a [ForumChannel]. -enum ForumSort { - latestActivity._(0), - creationDate._(1); +final class ForumSort extends EnumLike { + static const latestActivity = ForumSort._(0); + static const creationDate = ForumSort._(1); - /// The value of this forum sort. - final int value; + static const values = [latestActivity, creationDate]; - const ForumSort._(this.value); + const ForumSort._(super.value); - /// Parse a [ForumSort] from an [int]. - /// - /// The [value] must be a valid forum sort. - factory ForumSort.parse(int value) => ForumSort.values.firstWhere( - (sort) => sort.value == value, - orElse: () => throw FormatException('Unknown forum sort', value), - ); - - @override - String toString() => 'ForumSort($value)'; + factory ForumSort.parse(int value) => parseEnum(values, value); } /// The layout in a [ForumChannel]. -enum ForumLayout { - notSet._(0), - listView._(1), - galleryView._(2); +final class ForumLayout extends EnumLike { + static const notSet = ForumLayout._(0); + static const listView = ForumLayout._(1); + static const galleryView = ForumLayout._(2); - /// The value of this forum layout. - final int value; + static const values = [notSet, listView, galleryView]; - const ForumLayout._(this.value); + const ForumLayout._(super.value); - /// Parse a [ForumLayout] from an [int]. - /// - /// The [value] must be a valid forum layout. - factory ForumLayout.parse(int value) => ForumLayout.values.firstWhere( - (layout) => layout.value == value, - orElse: () => throw FormatException('Unknown forum layout', value), - ); - - @override - String toString() => 'ForumLayout($value)'; + factory ForumLayout.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/channel/voice_channel.dart b/lib/src/models/channel/voice_channel.dart index aed347644..d297ca3f1 100644 --- a/lib/src/models/channel/voice_channel.dart +++ b/lib/src/models/channel/voice_channel.dart @@ -1,4 +1,5 @@ import 'package:nyxx/src/models/channel/channel.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; /// A voice channel. abstract class VoiceChannel implements Channel { @@ -16,26 +17,16 @@ abstract class VoiceChannel implements Channel { } /// The quality mode of cameras in a [VoiceChannel]. -enum VideoQualityMode { +final class VideoQualityMode extends EnumLike { /// Automatic. - auto._(1), + static const VideoQualityMode auto = VideoQualityMode._(1); /// 720p. - full._(2); + static const VideoQualityMode full = VideoQualityMode._(2); - /// The value of this [VideoQualityMode]. - final int value; + static const List values = [auto, full]; - const VideoQualityMode._(this.value); + const VideoQualityMode._(super.value); - /// Parse a [VideoQualityMode] from an [int]. - /// - /// [value] must be a valid [VideoQualityMode]. - factory VideoQualityMode.parse(int value) => VideoQualityMode.values.firstWhere( - (mode) => mode.value == value, - orElse: () => throw FormatException('Unknown VideoQualityMode', value), - ); - - @override - String toString() => 'VideoQualityMode($value)'; + factory VideoQualityMode.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/commands/application_command.dart b/lib/src/models/commands/application_command.dart index 3dd49b312..e34a22071 100644 --- a/lib/src/models/commands/application_command.dart +++ b/lib/src/models/commands/application_command.dart @@ -8,6 +8,7 @@ import 'package:nyxx/src/models/locale.dart'; import 'package:nyxx/src/models/permissions.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; /// A partial [ApplicationCommand]. class PartialApplicationCommand extends WritableSnowflakeEntity { @@ -100,24 +101,19 @@ class ApplicationCommand extends PartialApplicationCommand { } /// The type of an [ApplicationCommand]. -enum ApplicationCommandType { - chatInput._(1), - user._(2), - message._(3); +final class ApplicationCommandType extends EnumLike { + /// A chat input command. + static const ApplicationCommandType chatInput = ApplicationCommandType._(1); - /// The value of this [ApplicationCommandType]. - final int value; + /// A user command. + static const ApplicationCommandType user = ApplicationCommandType._(2); - const ApplicationCommandType._(this.value); + /// A message command. + static const ApplicationCommandType message = ApplicationCommandType._(3); - /// Parse an [ApplicationCommandType] from an [int]. - /// - /// The [value] must be a valid application command type. - factory ApplicationCommandType.parse(int value) => ApplicationCommandType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown application command type', value), - ); + static const List values = [chatInput, user, message]; - @override - String toString() => 'ApplicationCommandType($value)'; + const ApplicationCommandType._(super.value); + + factory ApplicationCommandType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/commands/application_command_option.dart b/lib/src/models/commands/application_command_option.dart index d1cbbf076..59605cbaf 100644 --- a/lib/src/models/commands/application_command_option.dart +++ b/lib/src/models/commands/application_command_option.dart @@ -1,6 +1,7 @@ import 'package:nyxx/src/models/channel/channel.dart'; import 'package:nyxx/src/models/locale.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template command_option} @@ -70,34 +71,36 @@ class CommandOption with ToStringHelper { } /// The type of a [CommandOption]. -enum CommandOptionType { - subCommand._(1), - subCommandGroup._(2), - string._(3), - integer._(4), - boolean._(5), - user._(6), - channel._(7), - role._(8), - mentionable._(9), - number._(10), - attachment._(11); - - /// The value of this [CommandOptionType]. - final int value; - - const CommandOptionType._(this.value); - - /// Parse a [CommandOptionType] from an [int]. - /// - /// The [value] must be a valid command option type. - factory CommandOptionType.parse(int value) => CommandOptionType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown command option type', value), - ); - - @override - String toString() => 'CommandOptionType($value)'; +final class CommandOptionType extends EnumLike { + static const subCommand = CommandOptionType._(1); + static const subCommandGroup = CommandOptionType._(2); + static const string = CommandOptionType._(3); + static const integer = CommandOptionType._(4); + static const boolean = CommandOptionType._(5); + static const user = CommandOptionType._(6); + static const channel = CommandOptionType._(7); + static const role = CommandOptionType._(8); + static const mentionable = CommandOptionType._(9); + static const number = CommandOptionType._(10); + static const attachment = CommandOptionType._(11); + + static const values = [ + subCommand, + subCommandGroup, + string, + integer, + boolean, + user, + channel, + role, + mentionable, + number, + attachment, + ]; + + const CommandOptionType._(super.value); + + factory CommandOptionType.parse(int value) => parseEnum(values, value); } /// {@template command_option_choice} diff --git a/lib/src/models/commands/application_command_permissions.dart b/lib/src/models/commands/application_command_permissions.dart index b6b0a9fad..e7d880606 100644 --- a/lib/src/models/commands/application_command_permissions.dart +++ b/lib/src/models/commands/application_command_permissions.dart @@ -4,6 +4,7 @@ import 'package:nyxx/src/models/commands/application_command.dart'; import 'package:nyxx/src/models/guild/guild.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template command_permissions} @@ -74,24 +75,19 @@ class CommandPermission with ToStringHelper { } /// The type of a [CommandPermission]. -enum CommandPermissionType { - role._(1), - user._(2), - channel._(3); +final class CommandPermissionType extends EnumLike { + /// The permission applies to a role. + static const CommandPermissionType role = CommandPermissionType._(1); - /// The value of this [CommandPermissionType]. - final int value; + /// The permission applies to a user. + static const CommandPermissionType user = CommandPermissionType._(2); - const CommandPermissionType._(this.value); + /// The permission applies to a channel. + static const CommandPermissionType channel = CommandPermissionType._(3); - /// Parse a [CommandPermissionType] from an [int]. - /// - /// The [value] must be a valid command permission type. - factory CommandPermissionType.parse(int value) => CommandPermissionType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown command permission type', value), - ); + static const List values = [role, user, channel]; - @override - String toString() => 'CommandPermissionType($value)'; + const CommandPermissionType._(super.value); + + factory CommandPermissionType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/entitlement.dart b/lib/src/models/entitlement.dart index 6001fccb1..bbb62bfb7 100644 --- a/lib/src/models/entitlement.dart +++ b/lib/src/models/entitlement.dart @@ -4,6 +4,7 @@ import 'package:nyxx/src/models/guild/guild.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; /// A partial [Entitlement]. class PartialEntitlement extends ManagedSnowflakeEntity { @@ -76,40 +77,43 @@ class Entitlement extends PartialEntitlement { } /// The type of an [Entitlement]. -enum EntitlementType { +final class EntitlementType extends EnumLike { /// Entitlement was purchased by user. - purchase._(1), + static const EntitlementType purchase = EntitlementType._(1); /// Entitlement was granted by Discord Nitro subscription. - premiumSubscription._(2), + static const EntitlementType premiumSubscription = EntitlementType._(2); /// Entitlement was gifted by developer. - developerGift._(3), + static const EntitlementType developerGift = EntitlementType._(3); /// Entitlement was purchased by a dev in application test mode. - testModePurchase._(4), + static const EntitlementType testModePurchase = EntitlementType._(4); /// Entitlement was granted when the SKU was free. - freePurchase._(5), + static const EntitlementType freePurchase = EntitlementType._(5); /// Entitlement was gifted by another user. - userGift._(6), + static const EntitlementType userGift = EntitlementType._(6); /// Entitlement was claimed by user for free as a Nitro Subscriber. - premiumPurchase._(7), + static const EntitlementType premiumPurchase = EntitlementType._(7); /// Entitlement was purchased as an app subscription. - applicationSubscription._(8); - - final int value; - - const EntitlementType._(this.value); - - factory EntitlementType.parse(int value) => EntitlementType.values.firstWhere( - (element) => element.value == value, - orElse: () => throw FormatException('Unknown entitlement type', value), - ); - - @override - String toString() => 'EntitlementType($value)'; + static const EntitlementType applicationSubscription = EntitlementType._(8); + + static const List values = [ + purchase, + premiumSubscription, + developerGift, + testModePurchase, + freePurchase, + userGift, + premiumPurchase, + applicationSubscription, + ]; + + const EntitlementType._(super.value); + + factory EntitlementType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/guild/audit_log.dart b/lib/src/models/guild/audit_log.dart index 359d7ae1d..f1c8aa0c0 100644 --- a/lib/src/models/guild/audit_log.dart +++ b/lib/src/models/guild/audit_log.dart @@ -7,6 +7,7 @@ import 'package:nyxx/src/models/permission_overwrite.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// A partial [AuditLogEntry]. @@ -81,79 +82,126 @@ class AuditLogChange with ToStringHelper { } /// The type of event an [AuditLogEntry] represents. -enum AuditLogEvent { - guildUpdate._(1), - channelCreate._(10), - channelUpdate._(11), - channelDelete._(12), - channelOverwriteCreate._(13), - channelOverwriteUpdate._(14), - channelOverwriteDelete._(15), - memberKick._(20), - memberPrune._(21), - memberBanAdd._(22), - memberBanRemove._(23), - memberUpdate._(24), - memberRoleUpdate._(25), - memberMove._(26), - memberDisconnect._(27), - botAdd._(28), - roleCreate._(30), - roleUpdate._(31), - roleDelete._(32), - inviteCreate._(40), - inviteUpdate._(41), - inviteDelete._(42), - webhookCreate._(50), - webhookUpdate._(51), - webhookDelete._(52), - emojiCreate._(60), - emojiUpdate._(61), - emojiDelete._(62), - messageDelete._(72), - messageBulkDelete._(73), - messagePin._(74), - messageUnpin._(75), - integrationCreate._(80), - integrationUpdate._(81), - integrationDelete._(82), - stageInstanceCreate._(83), - stageInstanceUpdate._(84), - stageInstanceDelete._(85), - stickerCreate._(90), - stickerUpdate._(91), - stickerDelete._(92), - guildScheduledEventCreate._(100), - guildScheduledEventUpdate._(101), - guildScheduledEventDelete._(102), - threadCreate._(110), - threadUpdate._(111), - threadDelete._(112), - applicationCommandPermissionUpdate._(121), - autoModerationRuleCreate._(140), - autoModerationRuleUpdate._(141), - autoModerationRuleDelete._(142), - autoModerationBlockMessage._(143), - autoModerationFlagToChannel._(144), - autoModerationUserCommunicationDisabled._(145), - creatorMonetizationRequestCreated._(150), - creatorMonetizationTermsAccepted._(151); - - /// The value of this [AuditLogEvent]. - final int value; - - const AuditLogEvent._(this.value); - - /// Parse an [AuditLogEvent] from an [int]. - /// - /// The [value] must be a valid audit log event. - factory AuditLogEvent.parse(int value) => AuditLogEvent.values.firstWhere( - (event) => event.value == value, - orElse: () => throw FormatException('Unknown audit log event', value), - ); - - @override - String toString() => 'AuditLogEvent($value)'; +final class AuditLogEvent extends EnumLike { + static const guildUpdate = AuditLogEvent._(1); + static const channelCreate = AuditLogEvent._(10); + static const channelUpdate = AuditLogEvent._(11); + static const channelDelete = AuditLogEvent._(12); + static const channelOverwriteCreate = AuditLogEvent._(13); + static const channelOverwriteUpdate = AuditLogEvent._(14); + static const channelOverwriteDelete = AuditLogEvent._(15); + static const memberKick = AuditLogEvent._(20); + static const memberPrune = AuditLogEvent._(21); + static const memberBanAdd = AuditLogEvent._(22); + static const memberBanRemove = AuditLogEvent._(23); + static const memberUpdate = AuditLogEvent._(24); + static const memberRoleUpdate = AuditLogEvent._(25); + static const memberMove = AuditLogEvent._(26); + static const memberDisconnect = AuditLogEvent._(27); + static const botAdd = AuditLogEvent._(28); + static const roleCreate = AuditLogEvent._(30); + static const roleUpdate = AuditLogEvent._(31); + static const roleDelete = AuditLogEvent._(32); + static const inviteCreate = AuditLogEvent._(40); + static const inviteUpdate = AuditLogEvent._(41); + static const inviteDelete = AuditLogEvent._(42); + static const webhookCreate = AuditLogEvent._(50); + static const webhookUpdate = AuditLogEvent._(51); + static const webhookDelete = AuditLogEvent._(52); + static const emojiCreate = AuditLogEvent._(60); + static const emojiUpdate = AuditLogEvent._(61); + static const emojiDelete = AuditLogEvent._(62); + static const messageDelete = AuditLogEvent._(72); + static const messageBulkDelete = AuditLogEvent._(73); + static const messagePin = AuditLogEvent._(74); + static const messageUnpin = AuditLogEvent._(75); + static const integrationCreate = AuditLogEvent._(80); + static const integrationUpdate = AuditLogEvent._(81); + static const integrationDelete = AuditLogEvent._(82); + static const stageInstanceCreate = AuditLogEvent._(83); + static const stageInstanceUpdate = AuditLogEvent._(84); + static const stageInstanceDelete = AuditLogEvent._(85); + static const stickerCreate = AuditLogEvent._(90); + static const stickerUpdate = AuditLogEvent._(91); + static const stickerDelete = AuditLogEvent._(92); + static const guildScheduledEventCreate = AuditLogEvent._(100); + static const guildScheduledEventUpdate = AuditLogEvent._(101); + static const guildScheduledEventDelete = AuditLogEvent._(102); + static const threadCreate = AuditLogEvent._(110); + static const threadUpdate = AuditLogEvent._(111); + static const threadDelete = AuditLogEvent._(112); + static const applicationCommandPermissionUpdate = AuditLogEvent._(121); + static const autoModerationRuleCreate = AuditLogEvent._(140); + static const autoModerationRuleUpdate = AuditLogEvent._(141); + static const autoModerationRuleDelete = AuditLogEvent._(142); + static const autoModerationBlockMessage = AuditLogEvent._(143); + static const autoModerationFlagToChannel = AuditLogEvent._(144); + static const autoModerationUserCommunicationDisabled = AuditLogEvent._(145); + static const creatorMonetizationRequestCreated = AuditLogEvent._(150); + static const creatorMonetizationTermsAccepted = AuditLogEvent._(151); + + static const values = [ + guildUpdate, + channelCreate, + channelUpdate, + channelDelete, + channelOverwriteCreate, + channelOverwriteUpdate, + channelOverwriteDelete, + memberKick, + memberPrune, + memberBanAdd, + memberBanRemove, + memberUpdate, + memberRoleUpdate, + memberMove, + memberDisconnect, + botAdd, + roleCreate, + roleUpdate, + roleDelete, + inviteCreate, + inviteUpdate, + inviteDelete, + webhookCreate, + webhookUpdate, + webhookDelete, + emojiCreate, + emojiUpdate, + emojiDelete, + messageDelete, + messageBulkDelete, + messagePin, + messageUnpin, + integrationCreate, + integrationUpdate, + integrationDelete, + stageInstanceCreate, + stageInstanceUpdate, + stageInstanceDelete, + stickerCreate, + stickerUpdate, + stickerDelete, + guildScheduledEventCreate, + guildScheduledEventUpdate, + guildScheduledEventDelete, + threadCreate, + threadUpdate, + threadDelete, + applicationCommandPermissionUpdate, + autoModerationRuleCreate, + autoModerationRuleUpdate, + autoModerationRuleDelete, + autoModerationBlockMessage, + autoModerationFlagToChannel, + autoModerationUserCommunicationDisabled, + creatorMonetizationRequestCreated, + creatorMonetizationTermsAccepted, + ]; + + const AuditLogEvent._(super.value); + + factory AuditLogEvent.parse(int value) => parseEnum(values, value); } /// {@template audit_log_entry_info} diff --git a/lib/src/models/guild/auto_moderation.dart b/lib/src/models/guild/auto_moderation.dart index cb0177a39..c856ac150 100644 --- a/lib/src/models/guild/auto_moderation.dart +++ b/lib/src/models/guild/auto_moderation.dart @@ -8,6 +8,7 @@ import 'package:nyxx/src/models/role.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// A partial [AutoModerationRule]. @@ -83,62 +84,42 @@ class AutoModerationRule extends PartialAutoModerationRule { } /// The type of event on which an [AutoModerationRule] triggers. -enum AutoModerationEventType { +final class AutoModerationEventType extends EnumLike { /// When a member sends or edits a message in the guild. - messageSend._(1), + static const AutoModerationEventType messageSend = AutoModerationEventType._(1); /// When a member edits their profile. - memberUpdate._(2); + static const AutoModerationEventType memberUpdate = AutoModerationEventType._(2); - /// The value of this [AutoModerationEventType]. - final int value; + static const List values = [messageSend, memberUpdate]; - const AutoModerationEventType._(this.value); + const AutoModerationEventType._(super.value); - /// Parse an [AutoModerationEventType] from an [int]. - /// - /// The [value] must be a valid auto moderation event type. - factory AutoModerationEventType.parse(int value) => AutoModerationEventType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown auto moderation event type', value), - ); - - @override - String toString() => 'AutoModerationEventType($value)'; + factory AutoModerationEventType.parse(int value) => parseEnum(values, value); } /// The type of a trigger for an [AutoModerationRule] -enum TriggerType { +final class TriggerType extends EnumLike { /// Check if content contains words from a user defined list of keywords. - keyword._(1), + static const TriggerType keyword = TriggerType._(1); /// Check if content represents generic spam. - spam._(3), + static const TriggerType spam = TriggerType._(3); /// Check if content contains words from internal pre-defined wordsets. - keywordPreset._(4), + static const TriggerType keywordPreset = TriggerType._(4); /// Check if content contains more unique mentions than allowed. - mentionSpam._(5), + static const TriggerType mentionSpam = TriggerType._(5); /// Check if member profile contains words from a user defined list of keywords. - memberProfile._(6); + static const TriggerType memberProfile = TriggerType._(6); - /// The value of this [TriggerType]. - final int value; + static const List values = [keyword, spam, keywordPreset, mentionSpam, memberProfile]; - const TriggerType._(this.value); + const TriggerType._(super.value); - /// Parse an [TriggerType] from an [int]. - /// - /// The [value] must be a valid trigger type. - factory TriggerType.parse(int value) => TriggerType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown trigger type', value), - ); - - @override - String toString() => 'TriggerType($value)'; + factory TriggerType.parse(int value) => parseEnum(values, value); } /// {@template trigger_metadata} @@ -189,26 +170,16 @@ class TriggerMetadata with ToStringHelper implements TriggerMetadataBuilder { } /// A preset list of trigger keywords for an [AutoModerationRule]. -enum KeywordPresetType { - profanity._(1), - sexualContent._(2), - slurs._(3); +final class KeywordPresetType extends EnumLike { + static const profanity = KeywordPresetType._(1); + static const sexualContent = KeywordPresetType._(2); + static const slurs = KeywordPresetType._(3); - /// The value of this [KeywordPresetType]. - final int value; + static const values = [profanity, sexualContent, slurs]; - const KeywordPresetType._(this.value); + const KeywordPresetType._(super.value); - /// Parse an [KeywordPresetType] from an [int]. - /// - /// The [value] must be a valid keyword preset type. - factory KeywordPresetType.parse(int value) => KeywordPresetType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown keyword preset type', value), - ); - - @override - String toString() => 'KeywordPresetType($value)'; + factory KeywordPresetType.parse(int value) => parseEnum(values, value); } /// {@template auto_moderation_action} @@ -238,27 +209,17 @@ class AutoModerationAction with ToStringHelper implements AutoModerationActionBu } /// The type of action for an [AutoModerationAction]. -enum ActionType { - blockMessage._(1), - sendAlertMessage._(2), - timeout._(3), - blockMemberInteraction._(4); +final class ActionType extends EnumLike { + static const blockMessage = ActionType._(1); + static const sendAlertMessage = ActionType._(2); + static const timeout = ActionType._(3); + static const blockMemberInteraction = ActionType._(4); - /// The value of this [ActionType]. - final int value; + static const values = [blockMessage, sendAlertMessage, timeout, blockMemberInteraction]; - const ActionType._(this.value); + const ActionType._(super.value); - /// Parse an [ActionType] from an [int]. - /// - /// The [value] must be a valid action type. - factory ActionType.parse(int value) => ActionType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown action type', value), - ); - - @override - String toString() => 'ActionType($value)'; + factory ActionType.parse(int value) => parseEnum(values, value); } /// {@template action_metadata} diff --git a/lib/src/models/guild/guild.dart b/lib/src/models/guild/guild.dart index 37dea7ef8..305c46294 100644 --- a/lib/src/models/guild/guild.dart +++ b/lib/src/models/guild/guild.dart @@ -42,6 +42,7 @@ import 'package:nyxx/src/models/sticker/guild_sticker.dart'; import 'package:nyxx/src/models/user/user.dart'; import 'package:nyxx/src/models/voice/voice_region.dart'; import 'package:nyxx/src/models/voice/voice_state.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/flags.dart'; /// A partial [Guild]. @@ -466,73 +467,43 @@ class Guild extends UserGuild { } /// The verification level for a guild. -enum VerificationLevel { - none._(0), - low._(1), - medium._(2), - high._(3), - veryHigh._(4); +final class VerificationLevel extends EnumLike { + static const none = VerificationLevel._(0); + static const low = VerificationLevel._(1); + static const medium = VerificationLevel._(2); + static const high = VerificationLevel._(3); + static const veryHigh = VerificationLevel._(4); - /// The value of this verification level. - final int value; + static const values = [none, low, medium, high, veryHigh]; - const VerificationLevel._(this.value); + const VerificationLevel._(super.value); - /// Parses a [VerificationLevel] from an [int]. - /// - /// The [value] must be a valid verification level. - factory VerificationLevel.parse(int value) => VerificationLevel.values.firstWhere( - (level) => level.value == value, - orElse: () => throw FormatException('Invalid verification level', value), - ); - - @override - String toString() => 'VerificationLevel($value)'; + factory VerificationLevel.parse(int value) => parseEnum(values, value); } /// The level at which message notifications are sent in a guild. -enum MessageNotificationLevel { - allMessages._(0), - onlyMentions._(1); +final class MessageNotificationLevel extends EnumLike { + static const allMessages = MessageNotificationLevel._(0); + static const onlyMentions = MessageNotificationLevel._(1); - /// The value of this message notification level. - final int value; + static const values = [allMessages, onlyMentions]; - const MessageNotificationLevel._(this.value); - - /// Parses a [MessageNotificationLevel] from an [int]. - /// - /// The [value] must be a valid message notification level. - factory MessageNotificationLevel.parse(int value) => MessageNotificationLevel.values.firstWhere( - (level) => level.value == value, - orElse: () => throw FormatException('Invalid message notification level', value), - ); + const MessageNotificationLevel._(super.value); - @override - String toString() => 'MessageNotificationLevel($value)'; + factory MessageNotificationLevel.parse(int value) => parseEnum(values, value); } /// The level of explicit content filtering in a guild. -enum ExplicitContentFilterLevel { - disabled._(0), - membersWithoutRoles._(1), - allMembers._(2); - - /// The value of this explicit content filter level. - final int value; +final class ExplicitContentFilterLevel extends EnumLike { + static const disabled = ExplicitContentFilterLevel._(0); + static const membersWithoutRoles = ExplicitContentFilterLevel._(1); + static const allMembers = ExplicitContentFilterLevel._(2); - const ExplicitContentFilterLevel._(this.value); + static const values = [disabled, membersWithoutRoles, allMembers]; - /// Parses an [ExplicitContentFilterLevel] from an [int]. - /// - /// The [value] must be a valid explicit content filter level. - factory ExplicitContentFilterLevel.parse(int value) => ExplicitContentFilterLevel.values.firstWhere( - (level) => level.value == value, - orElse: () => throw FormatException('Invalid explicit content filter level', value), - ); + const ExplicitContentFilterLevel._(super.value); - @override - String toString() => 'ExplicitContentFilterLevel($value)'; + factory ExplicitContentFilterLevel.parse(int value) => parseEnum(values, value); } /// Features that can be enabled in certain guilds. @@ -705,25 +676,13 @@ class GuildFeatures extends Flags { } /// The MFA level required for moderators of a guild. -enum MfaLevel { - none._(0), - elevated._(1); - - /// The value of this MFA level. - final int value; +final class MfaLevel extends EnumLike { + static const none = MfaLevel._(0); + static const elevated = MfaLevel._(1); - const MfaLevel._(this.value); + const MfaLevel._(super.value); - /// Parses an [MfaLevel] from an [int]. - /// - /// The [value] must be a valid mfa level. - factory MfaLevel.parse(int value) => MfaLevel.values.firstWhere( - (level) => level.value == value, - orElse: () => throw FormatException('Invalid mfa level', value), - ); - - @override - String toString() => 'MfaLevel($value)'; + factory MfaLevel.parse(int value) => parseEnum([none, elevated], value); } /// The configuration of a guild's system channel. @@ -769,49 +728,29 @@ class SystemChannelFlags extends Flags { } /// The premium tier of a guild. -enum PremiumTier { - none._(0), - one._(1), - two._(2), - three._(3); - - /// The value of this tier. - final int value; +final class PremiumTier extends EnumLike { + static const none = PremiumTier._(0); + static const one = PremiumTier._(1); + static const two = PremiumTier._(2); + static const three = PremiumTier._(3); - const PremiumTier._(this.value); + static const values = [none, one, two, three]; - /// Parses a [PremiumTier] from an [int]. - /// - /// The [value] must be a valid premium tier. - factory PremiumTier.parse(int value) => PremiumTier.values.firstWhere( - (level) => level.value == value, - orElse: () => throw FormatException('Invalid premium tier', value), - ); + const PremiumTier._(super.value); - @override - String toString() => 'PremiumTier($value)'; + factory PremiumTier.parse(int value) => parseEnum(values, value); } /// The NSFW level of a guild. -enum NsfwLevel { - unset._(0), - explicit._(1), - safe._(2), - ageRestricted._(3); - - /// The value of this NSFW level. - final int value; +final class NsfwLevel extends EnumLike { + static const unset = NsfwLevel._(0); + static const explicit = NsfwLevel._(1); + static const safe = NsfwLevel._(2); + static const ageRestricted = NsfwLevel._(3); - const NsfwLevel._(this.value); + static const values = [unset, explicit, safe, ageRestricted]; - /// Parses an [NsfwLevel] from an [int]. - /// - /// The [value] must be a valid nsfw level. - factory NsfwLevel.parse(int value) => NsfwLevel.values.firstWhere( - (level) => level.value == value, - orElse: () => throw FormatException('Invalid nsfw level', value), - ); + const NsfwLevel._(super.value); - @override - String toString() => 'NsfwLevel($value)'; + factory NsfwLevel.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/guild/integration.dart b/lib/src/models/guild/integration.dart index 3a1e59e9a..f2d1f2fff 100644 --- a/lib/src/models/guild/integration.dart +++ b/lib/src/models/guild/integration.dart @@ -3,6 +3,7 @@ import 'package:nyxx/src/models/role.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// A partial [Integration]. @@ -94,25 +95,15 @@ class Integration extends PartialIntegration { } /// The behavior of an integration when a member's subscription expires. -enum IntegrationExpireBehavior { - removeRole._(0), - kick._(1); +final class IntegrationExpireBehavior extends EnumLike { + static const removeRole = IntegrationExpireBehavior._(0); + static const kick = IntegrationExpireBehavior._(1); - /// TThe value of this [IntegrationExpireBehavior]. - final int value; + static const List values = [removeRole, kick]; - const IntegrationExpireBehavior._(this.value); + const IntegrationExpireBehavior._(super.value); - /// Parse an [IntegrationExpireBehavior] from an [int]. - /// - /// The [value] must be a valid integration expire behavior. - factory IntegrationExpireBehavior.parse(int value) => IntegrationExpireBehavior.values.firstWhere( - (behavior) => behavior.value == value, - orElse: () => throw FormatException('Unknown integration expire behavior', value), - ); - - @override - String toString() => 'IntegrationExpireBehavior($value)'; + factory IntegrationExpireBehavior.parse(int value) => parseEnum(values, value); } /// {@template integration_account} diff --git a/lib/src/models/guild/onboarding.dart b/lib/src/models/guild/onboarding.dart index 85eebdeef..effe387c3 100644 --- a/lib/src/models/guild/onboarding.dart +++ b/lib/src/models/guild/onboarding.dart @@ -3,6 +3,7 @@ import 'package:nyxx/src/models/channel/channel.dart'; import 'package:nyxx/src/models/emoji.dart'; import 'package:nyxx/src/models/guild/guild.dart'; import 'package:nyxx/src/models/snowflake.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template onboarding} @@ -82,25 +83,15 @@ class OnboardingPrompt with ToStringHelper { } /// The type of an [Onboarding] prompt. -enum OnboardingPromptType { - multipleChoice._(0), - dropdown._(1); +final class OnboardingPromptType extends EnumLike { + static const multipleChoice = OnboardingPromptType._(0); + static const dropdown = OnboardingPromptType._(1); - /// The value of this [OnboardingPromptType]. - final int value; + static const List values = [multipleChoice, dropdown]; - const OnboardingPromptType._(this.value); + const OnboardingPromptType._(super.value); - /// Parse an [OnboardingPromptType] from an [int]. - /// - /// The [value] must be a valid onboarding prompt type. - factory OnboardingPromptType.parse(int value) => OnboardingPromptType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown onboarding prompt type', value), - ); - - @override - String toString() => 'OnboardingPromptType($value)'; + factory OnboardingPromptType.parse(int value) => parseEnum(values, value); } /// {@template onboarding_prompt_option} diff --git a/lib/src/models/guild/scheduled_event.dart b/lib/src/models/guild/scheduled_event.dart index 225182a2d..3d22c7627 100644 --- a/lib/src/models/guild/scheduled_event.dart +++ b/lib/src/models/guild/scheduled_event.dart @@ -8,6 +8,7 @@ import 'package:nyxx/src/models/guild/member.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// A partial [ScheduledEvent]. @@ -117,50 +118,30 @@ class ScheduledEvent extends PartialScheduledEvent { } /// The status of a [ScheduledEvent]. -enum EventStatus { - scheduled._(1), - active._(2), - completed._(3), - cancelled._(4); +final class EventStatus extends EnumLike { + static const scheduled = EventStatus._(1); + static const active = EventStatus._(2); + static const completed = EventStatus._(3); + static const cancelled = EventStatus._(4); - /// TThe value of this [EventStatus]. - final int value; + static const List values = [scheduled, active, completed, cancelled]; - const EventStatus._(this.value); + const EventStatus._(super.value); - /// Parse an [EventStatus] from an [int]. - /// - /// The [value] must be a valid event status. - factory EventStatus.parse(int value) => EventStatus.values.firstWhere( - (status) => status.value == value, - orElse: () => throw FormatException('Unknown event status', value), - ); - - @override - String toString() => 'EventStatus($value)'; + factory EventStatus.parse(int value) => parseEnum(values, value); } /// The type of the entity associated with a [ScheduledEvent]. -enum ScheduledEntityType { - stageInstance._(1), - voice._(2), - external._(3); +final class ScheduledEntityType extends EnumLike { + static const stageInstance = ScheduledEntityType._(1); + static const voice = ScheduledEntityType._(2); + static const external = ScheduledEntityType._(3); - /// The value of this [ScheduledEntityType]. - final int value; + static const List values = [stageInstance, voice, external]; - const ScheduledEntityType._(this.value); + const ScheduledEntityType._(super.value); - /// Parse a [ScheduledEntityType] from an [int]. - /// - /// The [value] must be a valid scheduled entity type. - factory ScheduledEntityType.parse(int value) => ScheduledEntityType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown scheduled entity type', value), - ); - - @override - String toString() => 'ScheduledEntityType($value)'; + factory ScheduledEntityType.parse(int value) => parseEnum(values, value); } /// {@template entity_metadata} diff --git a/lib/src/models/interaction.dart b/lib/src/models/interaction.dart index 4adbe8020..1c782a9dc 100644 --- a/lib/src/models/interaction.dart +++ b/lib/src/models/interaction.dart @@ -19,37 +19,32 @@ import 'package:nyxx/src/models/permissions.dart'; import 'package:nyxx/src/models/role.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// A context indicating whether command can be used in DMs, groups, or guilds. /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-context-types -enum InteractionContextType { +final class InteractionContextType extends EnumLike { /// Interaction can be used within servers. - guild._(0), + static const InteractionContextType guild = InteractionContextType._(0); /// Interaction can be used within DMs with the app's bot user. - botDm._(1), + static const InteractionContextType botDm = InteractionContextType._(1); /// Interaction can be used within Group DMs and DMs other than the app's bot user. - privateChannel._(2); + static const InteractionContextType privateChannel = InteractionContextType._(2); - /// The value of this [InteractionContextType]. - final int value; + static const List values = [ + guild, + botDm, + privateChannel, + ]; - const InteractionContextType._(this.value); + const InteractionContextType._(super.value); - /// Parse an [InteractionContextType] from an [int]. - /// - /// The [value] must be a valid interaction context type. - factory InteractionContextType.parse(int value) => InteractionContextType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown InteractionContextType', value), - ); - - @override - String toString() => 'InteractionContextType($value)'; + factory InteractionContextType.parse(int value) => parseEnum(values, value); } /// {@template interaction} @@ -427,28 +422,24 @@ class ApplicationCommandAutocompleteInteraction extends Interaction InteractionType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown interaction type', value), - ); - - @override - String toString() => 'InteractionType($value)'; +final class InteractionType extends EnumLike { + static const InteractionType ping = InteractionType._(1); + static const InteractionType applicationCommand = InteractionType._(2); + static const InteractionType messageComponent = InteractionType._(3); + static const InteractionType applicationCommandAutocomplete = InteractionType._(4); + static const InteractionType modalSubmit = InteractionType._(5); + + static const List values = [ + ping, + applicationCommand, + messageComponent, + applicationCommandAutocomplete, + modalSubmit, + ]; + + const InteractionType._(super.value); + + factory InteractionType.parse(int value) => parseEnum(values, value); } /// {@template application_command_interaction_data} diff --git a/lib/src/models/invite/invite.dart b/lib/src/models/invite/invite.dart index fb4200ac5..50d218334 100644 --- a/lib/src/models/invite/invite.dart +++ b/lib/src/models/invite/invite.dart @@ -3,6 +3,7 @@ import 'package:nyxx/src/models/channel/channel.dart'; import 'package:nyxx/src/models/guild/guild.dart'; import 'package:nyxx/src/models/guild/scheduled_event.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template invite} @@ -69,26 +70,13 @@ class Invite with ToStringHelper { } /// The type of an [Invite]'s target. -enum TargetType { - /// The invite is targeting a stream. - stream._(1), +final class TargetType extends EnumLike { + static const stream = TargetType._(1); + static const embeddedApplication = TargetType._(2); - /// The invite is targeting an embedded application. - embeddedApplication._(2); + static const List values = [stream, embeddedApplication]; - /// The value of this [TargetType]. - final int value; + const TargetType._(super.value); - /// Parse a [TargetType] from an [int]. - /// - /// The [value] must be a valid target type. - factory TargetType.parse(int value) => TargetType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown TargetType', value), - ); - - const TargetType._(this.value); - - @override - String toString() => 'TargetType($value)'; + factory TargetType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/locale.dart b/lib/src/models/locale.dart index 061742cbb..c5f337f00 100644 --- a/lib/src/models/locale.dart +++ b/lib/src/models/locale.dart @@ -1,40 +1,77 @@ +import 'package:nyxx/src/utils/enum_like.dart'; + /// A language locale available in the Discord client. /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/reference#locales -enum Locale { - id._('id', 'Indonesian', 'Bahasa Indonesia'), - da._('da', 'Danish', 'Dansk'), - de._('de', 'German', 'Deutsch'), - enGb._('en-GB', 'English, UK', 'English, UK'), - enUs._('en-US', 'English, US', 'English, US'), - esEs._('es-ES', 'Spanish', 'Español'), - es419._('es-419', 'Spanish, LATAM', 'Español, LATAM'), - fr._('fr', 'French', 'Français'), - hr._('hr', 'Croatian', 'Hrvatski'), - it._('it', 'Italian', 'Italiano'), - lt._('lt', 'Lithuanian', 'Lietuviškai'), - hu._('hu', 'Hungarian', 'Magyar'), - nl._('nl', 'Dutch', 'Nederlands'), - no._('no', 'Norwegian', 'Norsk'), - pl._('pl', 'Polish', 'Polski'), - ptBr._('pt-BR', 'Portuguese, Brazilian', 'Português do Brasil'), - ro._('ro', 'Romanian, Romania', 'Română'), - fi._('fi', 'Finnish', 'Suomi'), - svSe._('sv-SE', 'Swedish', 'Svenska'), - vi._('vi', 'Vietnamese', 'Tiếng Việt'), - tr._('tr', 'Turkish', 'Türkçe'), - cs._('cs', 'Czech', 'Čeština'), - el._('el', 'Greek', 'Ελληνικά'), - bg._('bg', 'Bulgarian', 'български'), - ru._('ru', 'Russian', 'Pусский'), - uk._('uk', 'Ukrainian', 'Українська'), - hi._('hi', 'Hindi', 'हिन्दी'), - th._('th', 'Thai', 'ไทย'), - zhCn._('zh-CN', 'Chinese, China', '中文'), - ja._('ja', 'Japanese', '日本語'), - zhTw._('zh-TW', 'Chinese, Taiwan', '繁體中文'), - ko._('ko', 'Korean', '한국어'); +final class Locale extends EnumLike { + static const Locale id = Locale._('id', 'Indonesian', 'Bahasa Indonesia'); + static const Locale da = Locale._('da', 'Danish', 'Dansk'); + static const Locale de = Locale._('de', 'German', 'Deutsch'); + static const Locale enGb = Locale._('en-GB', 'English, UK', 'English, UK'); + static const Locale enUs = Locale._('en-US', 'English, US', 'English, US'); + static const Locale esEs = Locale._('es-ES', 'Spanish', 'Español'); + static const Locale es419 = Locale._('es-419', 'Spanish, LATAM', 'Español, LATAM'); + static const Locale fr = Locale._('fr', 'French', 'Français'); + static const Locale hr = Locale._('hr', 'Croatian', 'Hrvatski'); + static const Locale it = Locale._('it', 'Italian', 'Italiano'); + static const Locale lt = Locale._('lt', 'Lithuanian', 'Lietuviškai'); + static const Locale hu = Locale._('hu', 'Hungarian', 'Magyar'); + static const Locale nl = Locale._('nl', 'Dutch', 'Nederlands'); + static const Locale no = Locale._('no', 'Norwegian', 'Norsk'); + static const Locale pl = Locale._('pl', 'Polish', 'Polski'); + static const Locale ptBr = Locale._('pt-BR', 'Portuguese, Brazilian', 'Português do Brasil'); + static const Locale ro = Locale._('ro', 'Romanian, Romania', 'Română'); + static const Locale fi = Locale._('fi', 'Finnish', 'Suomi'); + static const Locale svSe = Locale._('sv-SE', 'Swedish', 'Svenska'); + static const Locale vi = Locale._('vi', 'Vietnamese', 'Tiếng Việt'); + static const Locale tr = Locale._('tr', 'Turkish', 'Türkçe'); + static const Locale cs = Locale._('cs', 'Czech', 'Čeština'); + static const Locale el = Locale._('el', 'Greek', 'Ελληνικά'); + static const Locale bg = Locale._('bg', 'Bulgarian', 'български'); + static const Locale ru = Locale._('ru', 'Russian', 'Pусский'); + static const Locale uk = Locale._('uk', 'Ukrainian', 'Українська'); + static const Locale hi = Locale._('hi', 'Hindi', 'हिन्दी'); + static const Locale th = Locale._('th', 'Thai', 'ไทย'); + static const Locale zhCn = Locale._('zh-CN', 'Chinese, China', '中文'); + static const Locale ja = Locale._('ja', 'Japanese', '日本語'); + static const Locale zhTw = Locale._('zh-TW', 'Chinese, Taiwan', '繁體中文'); + static const Locale ko = Locale._('ko', 'Korean', '한국어'); + + static const List values = [ + id, + da, + de, + enGb, + enUs, + esEs, + es419, + fr, + hr, + it, + lt, + hu, + nl, + no, + pl, + ptBr, + ro, + fi, + svSe, + vi, + tr, + cs, + el, + bg, + ru, + uk, + hi, + th, + zhCn, + ja, + zhTw, + ko, + ]; /// The identifier for this locale. final String identifier; @@ -45,17 +82,11 @@ enum Locale { /// The native name of this locale. final String nativeName; - const Locale._(this.identifier, this.name, this.nativeName); + const Locale._(this.identifier, this.name, this.nativeName) : super(identifier); /// Parse a string into a locale. /// /// [identifier] must be a string containing an identifier matching [Locale.identifier] for one of /// the listed locales. - factory Locale.parse(String identifier) => Locale.values.firstWhere( - (locale) => locale.identifier == identifier, - orElse: () => throw FormatException('Unknown Locale', identifier), - ); - - @override - String toString() => 'Locale($identifier)'; + factory Locale.parse(String identifier) => parseEnum(values, identifier); } diff --git a/lib/src/models/message/activity.dart b/lib/src/models/message/activity.dart index 0f6c72ba5..32ae94856 100644 --- a/lib/src/models/message/activity.dart +++ b/lib/src/models/message/activity.dart @@ -1,3 +1,4 @@ +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template message_activity} @@ -25,25 +26,15 @@ class MessageActivity with ToStringHelper { /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/channel#message-object-message-activity-types -enum MessageActivityType { - join._(1), - spectate._(2), - listen._(3), - joinRequest._(5); +final class MessageActivityType extends EnumLike { + static const join = MessageActivityType._(1); + static const spectate = MessageActivityType._(2); + static const listen = MessageActivityType._(3); + static const joinRequest = MessageActivityType._(5); - /// The value of this [MessageActivityType]. - final int value; + static const List values = [join, spectate, listen, joinRequest]; - const MessageActivityType._(this.value); + const MessageActivityType._(super.value); - /// Parse a [MessageActivityType] from an [int]. - /// - /// [value] must be a valid message activity type. - factory MessageActivityType.parse(int value) => MessageActivityType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown MessageActivityType', value), - ); - - @override - String toString() => 'MessageActivityType($value)'; + factory MessageActivityType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/message/component.dart b/lib/src/models/message/component.dart index cffeaa29b..49ff67cb5 100644 --- a/lib/src/models/message/component.dart +++ b/lib/src/models/message/component.dart @@ -1,34 +1,25 @@ import 'package:nyxx/src/models/channel/channel.dart'; import 'package:nyxx/src/models/emoji.dart'; import 'package:nyxx/src/models/snowflake.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// The type of a [MessageComponent]. -enum MessageComponentType { - actionRow._(1), - button._(2), - stringSelect._(3), - textInput._(4), - userSelect._(5), - roleSelect._(6), - mentionableSelect._(7), - channelSelect._(8); - - /// The value of this [MessageComponentType]. - final int value; - - const MessageComponentType._(this.value); - - /// Parse a [MessageComponentType] from an [int]. - /// - /// The [value] must be a valid message component type. - factory MessageComponentType.parse(int value) => MessageComponentType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown message component type', value), - ); +final class MessageComponentType extends EnumLike { + static const actionRow = MessageComponentType._(1); + static const button = MessageComponentType._(2); + static const stringSelect = MessageComponentType._(3); + static const textInput = MessageComponentType._(4); + static const userSelect = MessageComponentType._(5); + static const roleSelect = MessageComponentType._(6); + static const mentionableSelect = MessageComponentType._(7); + static const channelSelect = MessageComponentType._(8); - @override - String toString() => 'MessageComponentType($value)'; + static const List values = [actionRow, button, stringSelect, textInput, userSelect, roleSelect, mentionableSelect, channelSelect]; + + const MessageComponentType._(super.value); + + factory MessageComponentType.parse(int value) => parseEnum(values, value); } /// A component in a [Message]. @@ -86,28 +77,18 @@ class ButtonComponent extends MessageComponent { } /// The style of a [ButtonComponent]. -enum ButtonStyle { - primary._(1), - secondary._(2), - success._(3), - danger._(4), - link._(5); - - /// The value of this [ButtonStyle]. - final int value; +final class ButtonStyle extends EnumLike { + static const primary = ButtonStyle._(1); + static const secondary = ButtonStyle._(2); + static const success = ButtonStyle._(3); + static const danger = ButtonStyle._(4); + static const link = ButtonStyle._(5); - const ButtonStyle._(this.value); + static const List values = [primary, secondary, success, danger, link]; - /// Parse a [ButtonStyle] from an [int]. - /// - /// The [value] must be a valid button style. - factory ButtonStyle.parse(int value) => ButtonStyle.values.firstWhere( - (style) => style.value == value, - orElse: () => throw FormatException('Unknown button style', value), - ); + const ButtonStyle._(super.value); - @override - String toString() => 'ButtonStyle($value)'; + factory ButtonStyle.parse(int value) => parseEnum(values, value); } /// A dropdown menu in which users can select from on or more choices. @@ -159,23 +140,16 @@ class SelectMenuComponent extends MessageComponent { } /// The type of a [SelectMenuDefaultValue]. -enum SelectMenuDefaultValueType { - user._('user'), - role._('role'), - channel._('channel'); +final class SelectMenuDefaultValueType extends EnumLike { + static const user = SelectMenuDefaultValueType._('user'); + static const role = SelectMenuDefaultValueType._('role'); + static const channel = SelectMenuDefaultValueType._('channel'); - /// The value of this [SelectMenuDefaultValue]. - final String value; + static const List values = [user, role, channel]; - const SelectMenuDefaultValueType._(this.value); + const SelectMenuDefaultValueType._(super.value); - /// Parse a [SelectMenuDefaultValueType] from a [String]. - /// - /// The [value] must be a valid select menu default value type. - factory SelectMenuDefaultValueType.parse(String value) => SelectMenuDefaultValueType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown select menu default value type', value), - ); + factory SelectMenuDefaultValueType.parse(String value) => parseEnum(values, value); } /// A default value in a [SelectMenuComponent]. @@ -263,23 +237,13 @@ class TextInputComponent extends MessageComponent { } /// The type of a [TextInputComponent]. -enum TextInputStyle { - short._(1), - paragraph._(2); +final class TextInputStyle extends EnumLike { + static const short = TextInputStyle._(1); + static const paragraph = TextInputStyle._(2); - /// The value of this [TextInputStyle]. - final int value; + static const List values = [short, paragraph]; - const TextInputStyle._(this.value); + const TextInputStyle._(super.value); - /// Parse a [TextInputComponent] from an [int]. - /// - /// The [value] must beb a valid text input style. - factory TextInputStyle.parse(int value) => TextInputStyle.values.firstWhere( - (style) => style.value == value, - orElse: () => throw FormatException('Unknown text input style', value), - ); - - @override - String toString() => 'TextInputStyle($value)'; + factory TextInputStyle.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/message/message.dart b/lib/src/models/message/message.dart index 1b0c6de90..3e5d9e157 100644 --- a/lib/src/models/message/message.dart +++ b/lib/src/models/message/message.dart @@ -21,6 +21,7 @@ import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/sticker/sticker.dart'; import 'package:nyxx/src/models/user/user.dart'; import 'package:nyxx/src/models/webhook.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/flags.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; @@ -252,58 +253,84 @@ class Message extends PartialMessage { /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/channel#message-object-message-types -enum MessageType { - normal._(0), - recipientAdd._(1), - recipientRemove._(2), - call._(3), - channelNameChange._(4), - channelIconChange._(5), - channelPinnedMessage._(6), - userJoin._(7), - guildBoost._(8), - guildBoostTier1._(9), - guildBoostTier2._(10), - guildBoostTier3._(11), - channelFollowAdd._(12), - guildDiscoveryDisqualified._(14), - guildDiscoveryRequalified._(15), - guildDiscoveryGracePeriodInitialWarning._(16), - guildDiscoveryGracePeriodFinalWarning._(17), - threadCreated._(18), - reply._(19), - chatInputCommand._(20), - threadStarterMessage._(21), - guildInviteReminder._(22), - contextMenuCommand._(23), - autoModerationAction._(24), - roleSubscriptionPurchase._(25), - interactionPremiumUpsell._(26), - stageStart._(27), - stageEnd._(28), - stageSpeaker._(29), - stageTopic._(31), - guildApplicationPremiumSubscription._(32), - guildIncidentAlertModeEnabled._(36), - guildIncidentAlertModeDisabled._(37), - guildIncidentReportRaid._(38), - guildIncidentReportFalseAlarm._(39); - - /// The value of this [MessageType]. - final int value; - - const MessageType._(this.value); - - /// Parse a [MessageType] from an [int]. - /// - /// [value] must be a valid [MessageType]. - factory MessageType.parse(int value) => MessageType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown MessageType', value), - ); - - @override - String toString() => 'MessageType($value)'; +final class MessageType extends EnumLike { + static const normal = MessageType._(0); + static const recipientAdd = MessageType._(1); + static const recipientRemove = MessageType._(2); + static const call = MessageType._(3); + static const channelNameChange = MessageType._(4); + static const channelIconChange = MessageType._(5); + static const channelPinnedMessage = MessageType._(6); + static const userJoin = MessageType._(7); + static const guildBoost = MessageType._(8); + static const guildBoostTier1 = MessageType._(9); + static const guildBoostTier2 = MessageType._(10); + static const guildBoostTier3 = MessageType._(11); + static const channelFollowAdd = MessageType._(12); + static const guildDiscoveryDisqualified = MessageType._(14); + static const guildDiscoveryRequalified = MessageType._(15); + static const guildDiscoveryGracePeriodInitialWarning = MessageType._(16); + static const guildDiscoveryGracePeriodFinalWarning = MessageType._(17); + static const threadCreated = MessageType._(18); + static const reply = MessageType._(19); + static const chatInputCommand = MessageType._(20); + static const threadStarterMessage = MessageType._(21); + static const guildInviteReminder = MessageType._(22); + static const contextMenuCommand = MessageType._(23); + static const autoModerationAction = MessageType._(24); + static const roleSubscriptionPurchase = MessageType._(25); + static const interactionPremiumUpsell = MessageType._(26); + static const stageStart = MessageType._(27); + static const stageEnd = MessageType._(28); + static const stageSpeaker = MessageType._(29); + static const stageTopic = MessageType._(31); + static const guildApplicationPremiumSubscription = MessageType._(32); + static const guildIncidentAlertModeEnabled = MessageType._(36); + static const guildIncidentAlertModeDisabled = MessageType._(37); + static const guildIncidentReportRaid = MessageType._(38); + static const guildIncidentReportFalseAlarm = MessageType._(39); + + static const List values = [ + normal, + recipientAdd, + recipientRemove, + call, + channelNameChange, + channelIconChange, + channelPinnedMessage, + userJoin, + guildBoost, + guildBoostTier1, + guildBoostTier2, + guildBoostTier3, + channelFollowAdd, + guildDiscoveryDisqualified, + guildDiscoveryRequalified, + guildDiscoveryGracePeriodInitialWarning, + guildDiscoveryGracePeriodFinalWarning, + threadCreated, + reply, + chatInputCommand, + threadStarterMessage, + guildInviteReminder, + contextMenuCommand, + autoModerationAction, + roleSubscriptionPurchase, + interactionPremiumUpsell, + stageStart, + stageEnd, + stageSpeaker, + stageTopic, + guildApplicationPremiumSubscription, + guildIncidentAlertModeEnabled, + guildIncidentAlertModeDisabled, + guildIncidentReportRaid, + guildIncidentReportFalseAlarm, + ]; + + const MessageType._(super.value); + + factory MessageType.parse(int value) => parseEnum(values, value); } /// Flags that can be applied to a [Message]. diff --git a/lib/src/models/message/poll.dart b/lib/src/models/message/poll.dart index ad26e04fe..40b2debc2 100644 --- a/lib/src/models/message/poll.dart +++ b/lib/src/models/message/poll.dart @@ -1,29 +1,20 @@ import 'package:nyxx/src/models/emoji.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// A layout type indicating how poll looks. /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/poll#layout-type -enum PollLayoutType { +final class PollLayoutType extends EnumLike { /// The default layout type. - defaultLayout._(1); + static const defaultLayout = PollLayoutType._(1); - /// The value of this [PollLayoutType]. - final int value; + static const List values = [defaultLayout]; - const PollLayoutType._(this.value); + const PollLayoutType._(super.value); - /// Parse an [PollLayoutType] from an [int]. - /// - /// The [value] must be a valid poll layout type. - factory PollLayoutType.parse(int value) => PollLayoutType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown PollLayoutType', value), - ); - - @override - String toString() => 'PollLayoutType($value)'; + factory PollLayoutType.parse(int value) => parseEnum(values, value); } /// {@template poll_media} diff --git a/lib/src/models/permission_overwrite.dart b/lib/src/models/permission_overwrite.dart index 30fae0d63..d163bd853 100644 --- a/lib/src/models/permission_overwrite.dart +++ b/lib/src/models/permission_overwrite.dart @@ -1,5 +1,6 @@ import 'package:nyxx/src/models/permissions.dart'; import 'package:nyxx/src/models/snowflake.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template permission_overwrite} @@ -42,26 +43,16 @@ class PermissionOverwrite with ToStringHelper { } /// The type of a permission overwrite. -enum PermissionOverwriteType { +final class PermissionOverwriteType extends EnumLike { /// The overwrite applies to a [Role]'s permissions. - role._(0), + static const role = PermissionOverwriteType._(0); /// The overwrite applies to a [Member]'s permissions. - member._(1); + static const member = PermissionOverwriteType._(1); - /// The value of this type. - final int value; + static const values = [role, member]; - const PermissionOverwriteType._(this.value); + const PermissionOverwriteType._(super.value); - /// Parse a [PermissionOverwriteType] from a [value]. - /// - /// The [value] must be a valid [PermissionOverwriteType]. - factory PermissionOverwriteType.parse(int value) => PermissionOverwriteType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown PermissionOverwriteType', value), - ); - - @override - String toString() => 'PermissionOverwriteType($value)'; + factory PermissionOverwriteType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/presence.dart b/lib/src/models/presence.dart index 1de6ad7c6..892e97be2 100644 --- a/lib/src/models/presence.dart +++ b/lib/src/models/presence.dart @@ -1,5 +1,6 @@ import 'package:nyxx/src/models/emoji.dart'; import 'package:nyxx/src/models/snowflake.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/flags.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; @@ -22,27 +23,17 @@ class ClientStatus with ToStringHelper { } /// The status of a client. -enum UserStatus { - online._('online'), - idle._('idle'), - dnd._('dnd'), - offline._('offline'); - - /// The value of this [UserStatus]. - final String value; - - const UserStatus._(this.value); - - /// Parse a [UserStatus] from a [String]. - /// - /// The [value] must be a valid user status. - factory UserStatus.parse(String value) => UserStatus.values.firstWhere( - (status) => status.value == value, - orElse: () => throw FormatException('Unknown user status', value), - ); - - @override - String toString() => 'UserStatus($value)'; +final class UserStatus extends EnumLike { + static const online = UserStatus._('online'); + static const dnd = UserStatus._('dnd'); + static const idle = UserStatus._('idle'); + static const offline = UserStatus._('offline'); + + static const values = [online, dnd, idle, offline]; + + const UserStatus._(super.value); + + factory UserStatus.parse(String value) => parseEnum(values, value); } /// {@template activity} @@ -116,29 +107,19 @@ class Activity with ToStringHelper { } /// The type of an activity. -enum ActivityType { - game._(0), - streaming._(1), - listening._(2), - watching._(3), - custom._(4), - competing._(5); - - /// The value of this [ActivityType]. - final int value; - - const ActivityType._(this.value); - - /// Parse an [ActivityType] from an [int]. - /// - /// The [value] must be a valid activity type. - factory ActivityType.parse(int value) => ActivityType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown activity type', value), - ); - - @override - String toString() => 'ActivityType($value)'; +final class ActivityType extends EnumLike { + static const game = ActivityType._(0); + static const streaming = ActivityType._(1); + static const listening = ActivityType._(2); + static const watching = ActivityType._(3); + static const custom = ActivityType._(4); + static const competing = ActivityType._(5); + + static const values = [game, streaming, listening, watching, custom, competing]; + + const ActivityType._(super.value); + + factory ActivityType.parse(int value) => parseEnum(values, value); } /// {@template activity_timestamps} diff --git a/lib/src/models/sku.dart b/lib/src/models/sku.dart index cfbcda741..dd712adcd 100644 --- a/lib/src/models/sku.dart +++ b/lib/src/models/sku.dart @@ -1,6 +1,7 @@ import 'package:nyxx/src/http/managers/application_manager.dart'; import 'package:nyxx/src/models/application.dart'; import 'package:nyxx/src/models/snowflake.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/flags.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; @@ -46,29 +47,29 @@ class Sku with ToStringHelper { } /// The type of an [Sku]. -enum SkuType { +final class SkuType extends EnumLike { /// Durable one-time purchase. - durable._(2), + static const SkuType durable = SkuType._(2); /// Consumable one-time purchase. - consumable._(3), - subscription._(5), - subscriptionGroup._(6); + static const SkuType consumable = SkuType._(3); - final int value; + /// Subscription. + static const SkuType subscription = SkuType._(5); - const SkuType._(this.value); + /// Subscription group. + static const SkuType subscriptionGroup = SkuType._(6); - /// Parse an [SkuType] from an [int]. - /// - /// The [value] must be a valid sku type. - factory SkuType.parse(int value) => SkuType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown SKU type', value), - ); + static const List values = [ + durable, + consumable, + subscription, + subscriptionGroup, + ]; - @override - String toString() => 'SkuType($value)'; + const SkuType._(super.value); + + factory SkuType.parse(int value) => parseEnum(values, value); } /// Flags applied to an [Sku]. diff --git a/lib/src/models/sticker/sticker.dart b/lib/src/models/sticker/sticker.dart index 01aceb6d7..450c5fc21 100644 --- a/lib/src/models/sticker/sticker.dart +++ b/lib/src/models/sticker/sticker.dart @@ -1,48 +1,29 @@ import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; -enum StickerType { - standard._(1), - guild._(2); +final class StickerType extends EnumLike { + static const standard = StickerType._(1); + static const guild = StickerType._(2); - /// The value of this [StickerType]. - final int value; + static const List values = [standard, guild]; - const StickerType._(this.value); + const StickerType._(super.value); - /// Parse a [StickerType] from a [value]. - /// - /// The [value] must be a valid sticker type - factory StickerType.parse(int value) => StickerType.values.firstWhere( - (state) => state.value == value, - orElse: () => throw FormatException('Unknown sticker type', value), - ); - - @override - String toString() => 'StickerType($value)'; + factory StickerType.parse(int value) => parseEnum(values, value); } -enum StickerFormatType { - png._(1), - apng._(2), - lottie._(3), - gif._(4); +final class StickerFormatType extends EnumLike { + static const png = StickerFormatType._(1); + static const apng = StickerFormatType._(2); + static const lottie = StickerFormatType._(3); + static const gif = StickerFormatType._(4); - /// The value of this [StickerFormatType]. - final int value; + static const List values = [png, apng, lottie, gif]; - const StickerFormatType._(this.value); + const StickerFormatType._(super.value); - /// Parse a [StickerFormatType] from a [value]. - /// - /// The [value] must be a valid sticker format type - factory StickerFormatType.parse(int value) => StickerFormatType.values.firstWhere( - (state) => state.value == value, - orElse: () => throw FormatException('Unknown sticker format type', value), - ); - - @override - String toString() => 'StickerFormatType($value)'; + factory StickerFormatType.parse(int value) => parseEnum(values, value); } /// Mixin with shared properties with stickers diff --git a/lib/src/models/team.dart b/lib/src/models/team.dart index 4e15f3eef..98e100d9a 100644 --- a/lib/src/models/team.dart +++ b/lib/src/models/team.dart @@ -3,6 +3,7 @@ import 'package:nyxx/src/http/managers/application_manager.dart'; import 'package:nyxx/src/http/route.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// {@template team} @@ -81,46 +82,29 @@ class TeamMember with ToStringHelper { } /// The status of a member in a [Team]. -enum TeamMembershipState { - invited._(1), - accepted._(2); +final class TeamMembershipState extends EnumLike { + /// The user has been invited to the team. + static const TeamMembershipState invited = TeamMembershipState._(1); - /// The value of this [TeamMembershipState]. - final int value; + /// The user has accepted the invitation to the team. + static const TeamMembershipState accepted = TeamMembershipState._(2); - const TeamMembershipState._(this.value); + static const List values = [invited, accepted]; - /// Parse a [TeamMembershipState] from a [value]. - /// - /// The [value] must be a valid team membership state. - factory TeamMembershipState.parse(int value) => TeamMembershipState.values.firstWhere( - (state) => state.value == value, - orElse: () => throw FormatException('Unknown team membership state', value), - ); + const TeamMembershipState._(super.value); - @override - String toString() => 'TeamMembershipState($value)'; + factory TeamMembershipState.parse(int value) => parseEnum(values, value); } /// The role of a [TeamMember]. -enum TeamMemberRole { - admin._('admin'), - developer._('developer'), - readOnly._('read_only'); - - /// The value of this [TeamMemberRole]. - final String value; - - const TeamMemberRole._(this.value); - - /// Parse a [TeamMemberRole] from a [String]. - /// - /// The [value] must be a valid team member role. - factory TeamMemberRole.parse(String value) => TeamMemberRole.values.firstWhere( - (role) => role.value == value, - orElse: () => throw FormatException('Unknown team member role', value), - ); - - @override - String toString() => 'TeamMemberRole($value)'; +final class TeamMemberRole extends EnumLike { + static const admin = TeamMemberRole._('admin'); + static const developer = TeamMemberRole._('developer'); + static const readOnly = TeamMemberRole._('read_only'); + + static const List values = [admin, developer, readOnly]; + + const TeamMemberRole._(super.value); + + factory TeamMemberRole.parse(String value) => parseEnum(values, value); } diff --git a/lib/src/models/user/connection.dart b/lib/src/models/user/connection.dart index 426f202aa..f207b34bc 100644 --- a/lib/src/models/user/connection.dart +++ b/lib/src/models/user/connection.dart @@ -1,4 +1,5 @@ import 'package:nyxx/src/models/guild/integration.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// A link to an account on a service other than Discord. @@ -56,71 +57,77 @@ class Connection with ToStringHelper { /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/user#connection-object-services -enum ConnectionType { - battleNet._('battlenet', 'Battle.net'), - bungieNet._('bungie', 'Bungie.net'), - domain._('domain', 'Domain'), - ebay._('ebay', 'eBay'), - epicGames._('epicgames', 'Epic Games'), - facebook._('facebook', 'Facebook'), - github._('github', 'GitHub'), - instagram._('instagram', 'Instagram'), - leagueOfLegends._('leagueoflegends', 'League of Legends'), - paypal._('paypal', 'PayPal'), - playstation._('playstation', 'PlayStation Network'), - reddit._('reddit', 'Reddit'), - riotGames._('riotgames', 'Riot Games'), - roblox._('roblox', 'ROBLOX'), - spotify._('spotify', 'Spotify'), - skype._('skype', 'Skype'), - steam._('steam', 'Steam'), - tikTok._('tiktok', 'TikTok'), - twitch._('twitch', 'Twitch'), - twitter._('twitter', 'Twitter'), - xbox._('xbox', 'Xbox'), - youtube._('youtube', 'YouTube'); - - /// The value of this connection type. - final String value; +final class ConnectionType extends EnumLike { + static const battleNet = ConnectionType._('battlenet', 'Battle.net'); + static const bungieNet = ConnectionType._('bungie', 'Bungie.net'); + static const domain = ConnectionType._('domain', 'Domain'); + static const ebay = ConnectionType._('ebay', 'eBay'); + static const epicGames = ConnectionType._('epicgames', 'Epic Games'); + static const facebook = ConnectionType._('facebook', 'Facebook'); + static const github = ConnectionType._('github', 'GitHub'); + static const instagram = ConnectionType._('instagram', 'Instagram'); + static const leagueOfLegends = ConnectionType._('leagueoflegends', 'League of Legends'); + static const paypal = ConnectionType._('paypal', 'PayPal'); + static const playstation = ConnectionType._('playstation', 'PlayStation Network'); + static const reddit = ConnectionType._('reddit', 'Reddit'); + static const riotGames = ConnectionType._('riotgames', 'Riot Games'); + static const roblox = ConnectionType._('roblox', 'ROBLOX'); + static const spotify = ConnectionType._('spotify', 'Spotify'); + static const skype = ConnectionType._('skype', 'Skype'); + static const steam = ConnectionType._('steam', 'Steam'); + static const tikTok = ConnectionType._('tiktok', 'TikTok'); + static const twitch = ConnectionType._('twitch', 'Twitch'); + static const twitter = ConnectionType._('twitter', 'Twitter'); + static const xbox = ConnectionType._('xbox', 'Xbox'); + static const youtube = ConnectionType._('youtube', 'YouTube'); + + static const List values = [ + battleNet, + bungieNet, + domain, + ebay, + epicGames, + facebook, + github, + instagram, + leagueOfLegends, + paypal, + playstation, + reddit, + riotGames, + roblox, + spotify, + skype, + steam, + tikTok, + twitch, + twitter, + xbox, + youtube, + ]; /// A human-readable name for this connection type. final String name; - const ConnectionType._(this.value, this.name); + const ConnectionType._(super.value, this.name); /// Parse a string to a [ConnectionType]. /// /// The [value] must be a string containing a valid [ConnectionType.value]. - factory ConnectionType.parse(String value) => ConnectionType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown ConnectionType', value), - ); - - @override - String toString() => 'ConnectionType($name)'; + factory ConnectionType.parse(String value) => parseEnum(values, value); } /// The visibility level of a connection. /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/user#connection-object-visibility-types -enum ConnectionVisibility { - none._(0), - everyone._(1); +final class ConnectionVisibility extends EnumLike { + static const none = ConnectionVisibility._(0); + static const everyone = ConnectionVisibility._(1); - /// THe value of this connection visibility level. - final int value; + static const List values = [none, everyone]; - const ConnectionVisibility._(this.value); + const ConnectionVisibility._(super.value); - /// Parse an integer value to a [ConnectionVisibility]. - /// - /// The [value] must be a valid [ConnectionVisibility]. - factory ConnectionVisibility.parse(int value) => ConnectionVisibility.values.firstWhere( - (visibility) => visibility.value == value, - orElse: () => throw FormatException('Unknown ConnectionVisibility', value), - ); - - @override - String toString() => 'ConnectionVisibility($name)'; + factory ConnectionVisibility.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/user/user.dart b/lib/src/models/user/user.dart index 60a0ffe0a..de5988031 100644 --- a/lib/src/models/user/user.dart +++ b/lib/src/models/user/user.dart @@ -6,6 +6,7 @@ import 'package:nyxx/src/models/discord_color.dart'; import 'package:nyxx/src/models/locale.dart'; import 'package:nyxx/src/models/message/author.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/flags.dart'; /// A partial [User] object. @@ -227,25 +228,15 @@ class UserFlags extends Flags { } /// The types of Discord Nitro subscription a user can have. -enum NitroType { - none._(0), - classic._(1), - nitro._(2), - basic._(3); - - /// The value of this [NitroType]. - final int value; - - const NitroType._(this.value); - - /// Parse an integer from the API to a [NitroType]. - /// - /// The [value] must be a valid nitro type. - factory NitroType.parse(int value) => NitroType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown NitroType', value), - ); +final class NitroType extends EnumLike { + static const none = NitroType._(0); + static const classic = NitroType._(1); + static const nitro = NitroType._(2); + static const basic = NitroType._(3); - @override - String toString() => 'NitroType($value)'; + static const List values = [none, classic, nitro, basic]; + + const NitroType._(super.value); + + factory NitroType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/models/webhook.dart b/lib/src/models/webhook.dart index 8ed2c416f..d8677dda0 100644 --- a/lib/src/models/webhook.dart +++ b/lib/src/models/webhook.dart @@ -11,6 +11,7 @@ import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/http/managers/webhook_manager.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/utils/enum_like.dart'; /// A partial [Webhook]. class PartialWebhook extends WritableSnowflakeEntity { @@ -173,29 +174,19 @@ class Webhook extends PartialWebhook { } /// The type of a [Webhook]. -enum WebhookType { +final class WebhookType extends EnumLike { /// A webhook which sends messages to a channel using a [Webhook.token]. - incoming._(1), + static const WebhookType incoming = WebhookType._(1); /// An internal webhook used to manage Channel Followers. - channelFollower._(2), + static const WebhookType channelFollower = WebhookType._(2); /// A webhook for use with interactions. - application._(3); + static const WebhookType application = WebhookType._(3); - /// The value of this webhook type. - final int value; + static const List values = [incoming, channelFollower, application]; - const WebhookType._(this.value); + const WebhookType._(super.value); - /// Parse a [WebhookType] from a [value]. - /// - /// The [value] must be a valid webhook type. - factory WebhookType.parse(int value) => WebhookType.values.firstWhere( - (type) => type.value == value, - orElse: () => throw FormatException('Unknown webhook type', value), - ); - - @override - String toString() => 'WebhookType($value)'; + factory WebhookType.parse(int value) => parseEnum(values, value); } diff --git a/lib/src/utils/enum_like.dart b/lib/src/utils/enum_like.dart new file mode 100644 index 000000000..af33d9344 --- /dev/null +++ b/lib/src/utils/enum_like.dart @@ -0,0 +1,24 @@ +base class EnumLike { + /// The value this enum-like holds. + final T value; + + /// Whether this enum-like is unknown. + final bool isUnknown; + + //@nodoc + const EnumLike(this.value, [this.isUnknown = false]); + + @override + String toString() => '$runtimeType($value)'; + + @override + int get hashCode => value.hashCode; + + @override + bool operator ==(Object? other) => (other is EnumLike && other.value == value) || (other is T && other == value); +} + +R parseEnum>(List> values, T value) { + final enumValue = values.firstWhere((enumValue) => enumValue.value == value, orElse: () => EnumLike(value, true)); + return enumValue as R; +} From e32a69453ae0c43328dd37c7ae66d63060e31fcc Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Tue, 11 Jun 2024 22:09:29 +0200 Subject: [PATCH 2/7] Implicit inference --- lib/src/models/application.dart | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/src/models/application.dart b/lib/src/models/application.dart index ad7909368..3ab730e01 100644 --- a/lib/src/models/application.dart +++ b/lib/src/models/application.dart @@ -186,12 +186,12 @@ class Application extends PartialApplication { final class ApplicationIntegrationType extends EnumLike { /// App is installable to servers. - static const ApplicationIntegrationType guildInstall = ApplicationIntegrationType._(0); + static const guildInstall = ApplicationIntegrationType._(0); /// App is installable to users. - static const ApplicationIntegrationType userInstall = ApplicationIntegrationType._(1); + static const userInstall = ApplicationIntegrationType._(1); - static const List values = [guildInstall, userInstall]; + static const values = [guildInstall, userInstall]; factory ApplicationIntegrationType.parse(int value) => parseEnum(values, value); @@ -318,14 +318,14 @@ class ApplicationRoleConnectionMetadata with ToStringHelper { /// The type of an [ApplicationRoleConnectionMetadata]. final class ConnectionMetadataType extends EnumLike { - static const ConnectionMetadataType integerLessThanOrEqual = ConnectionMetadataType._(1); - static const ConnectionMetadataType integerGreaterThanOrEqual = ConnectionMetadataType._(2); - static const ConnectionMetadataType integerEqual = ConnectionMetadataType._(3); - static const ConnectionMetadataType integerNotEqual = ConnectionMetadataType._(4); - static const ConnectionMetadataType dateTimeLessThanOrEqual = ConnectionMetadataType._(5); - static const ConnectionMetadataType dateTimeGreaterThanOrEqual = ConnectionMetadataType._(6); - static const ConnectionMetadataType booleanEqual = ConnectionMetadataType._(7); - static const ConnectionMetadataType booleanNotEqual = ConnectionMetadataType._(8); + static const integerLessThanOrEqual = ConnectionMetadataType._(1); + static const integerGreaterThanOrEqual = ConnectionMetadataType._(2); + static const integerEqual = ConnectionMetadataType._(3); + static const integerNotEqual = ConnectionMetadataType._(4); + static const dateTimeLessThanOrEqual = ConnectionMetadataType._(5); + static const dateTimeGreaterThanOrEqual = ConnectionMetadataType._(6); + static const booleanEqual = ConnectionMetadataType._(7); + static const booleanNotEqual = ConnectionMetadataType._(8); static const values = [ integerLessThanOrEqual, From 9ff6fd7d1ff49a4d6592dc458dca5ef9a50a414e Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Tue, 11 Jun 2024 22:12:18 +0200 Subject: [PATCH 3/7] fmt --- lib/src/http/managers/interaction_manager.dart | 2 +- lib/src/http/managers/message_manager.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/http/managers/interaction_manager.dart b/lib/src/http/managers/interaction_manager.dart index 9ac5d494e..324c10438 100644 --- a/lib/src/http/managers/interaction_manager.dart +++ b/lib/src/http/managers/interaction_manager.dart @@ -35,7 +35,7 @@ class InteractionManager { Interaction parse(Map raw) { final type = InteractionType.parse(raw['type'] as int); - assert (!type.isUnknown, 'Unknown interaction type: $type'); + assert(!type.isUnknown, 'Unknown interaction type: $type'); final guildId = maybeParse(raw['guild_id'], Snowflake.parse); final channelId = maybeParse(raw['channel_id'], Snowflake.parse); diff --git a/lib/src/http/managers/message_manager.dart b/lib/src/http/managers/message_manager.dart index 9dbd0aeee..4995da639 100644 --- a/lib/src/http/managers/message_manager.dart +++ b/lib/src/http/managers/message_manager.dart @@ -247,7 +247,7 @@ class MessageManager extends Manager { MessageComponent parseMessageComponent(Map raw) { final type = MessageComponentType.parse(raw['type'] as int); - assert (!type.isUnknown, 'Unknown message component type: $type'); + assert(!type.isUnknown, 'Unknown message component type: $type'); return switch (type) { MessageComponentType.actionRow => ActionRowComponent( From d762d6932df48d7e8196ec1bdedf55ed011b3aea Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Fri, 14 Jun 2024 16:32:48 +0200 Subject: [PATCH 4/7] fix: apply suggestions --- lib/src/gateway/gateway.dart | 5 +- .../http/managers/interaction_manager.dart | 5 +- lib/src/http/managers/message_manager.dart | 2 - lib/src/models/application.dart | 51 ++--- lib/src/models/channel/channel.dart | 53 ++--- lib/src/models/channel/stage_instance.dart | 13 +- lib/src/models/channel/types/forum.dart | 28 ++- lib/src/models/channel/voice_channel.dart | 13 +- .../models/commands/application_command.dart | 15 +- .../commands/application_command_option.dart | 47 ++--- .../application_command_permissions.dart | 15 +- lib/src/models/entitlement.dart | 37 ++-- lib/src/models/guild/audit_log.dart | 182 ++++++------------ lib/src/models/guild/auto_moderation.dart | 64 +++--- lib/src/models/guild/guild.dart | 92 +++++---- lib/src/models/guild/integration.dart | 13 +- lib/src/models/guild/onboarding.dart | 13 +- lib/src/models/guild/scheduled_event.dart | 32 ++- lib/src/models/interaction.dart | 48 ++--- lib/src/models/invite/invite.dart | 13 +- lib/src/models/locale.dart | 110 ++++------- lib/src/models/message/activity.dart | 17 +- lib/src/models/message/component.dart | 72 ++++--- lib/src/models/message/message.dart | 119 ++++-------- lib/src/models/message/poll.dart | 11 +- lib/src/models/permission_overwrite.dart | 13 +- lib/src/models/presence.dart | 37 ++-- lib/src/models/sku.dart | 22 +-- lib/src/models/sticker/sticker.dart | 30 ++- lib/src/models/team.dart | 27 ++- lib/src/models/user/connection.dart | 94 ++++----- lib/src/models/user/user.dart | 17 +- lib/src/models/webhook.dart | 15 +- lib/src/utils/enum_like.dart | 16 +- 34 files changed, 536 insertions(+), 805 deletions(-) diff --git a/lib/src/gateway/gateway.dart b/lib/src/gateway/gateway.dart index ed0aea300..657dfb7c4 100644 --- a/lib/src/gateway/gateway.dart +++ b/lib/src/gateway/gateway.dart @@ -1019,8 +1019,6 @@ class Gateway extends GatewayManager with EventParser { InteractionCreateEvent> parseInteractionCreate(Map raw) { final interaction = client.interactions.parse(raw); - assert(!interaction.type.isUnknown, 'Unknown interaction type: ${interaction.type}'); - // Needed to get proper type promotion. return switch (interaction.type) { InteractionType.ping => InteractionCreateEvent(gateway: this, interaction: interaction as PingInteraction), @@ -1031,8 +1029,7 @@ class Gateway extends GatewayManager with EventParser { InteractionType.modalSubmit => InteractionCreateEvent(gateway: this, interaction: interaction as ModalSubmitInteraction), InteractionType.applicationCommandAutocomplete => InteractionCreateEvent(gateway: this, interaction: interaction as ApplicationCommandAutocompleteInteraction), - // stub, should never be reached - InteractionType() => null, + InteractionType() => throw StateError('Unknown interaction type: ${interaction.type}'), } as InteractionCreateEvent>; } diff --git a/lib/src/http/managers/interaction_manager.dart b/lib/src/http/managers/interaction_manager.dart index 324c10438..3d18afc23 100644 --- a/lib/src/http/managers/interaction_manager.dart +++ b/lib/src/http/managers/interaction_manager.dart @@ -35,8 +35,6 @@ class InteractionManager { Interaction parse(Map raw) { final type = InteractionType.parse(raw['type'] as int); - assert(!type.isUnknown, 'Unknown interaction type: $type'); - final guildId = maybeParse(raw['guild_id'], Snowflake.parse); final channelId = maybeParse(raw['channel_id'], Snowflake.parse); final id = Snowflake.parse(raw['id']!); @@ -170,8 +168,7 @@ class InteractionManager { authorizingIntegrationOwners: authorizingIntegrationOwners, context: context, ), - // stub, should never be reached - InteractionType() => null, + InteractionType() => throw StateError('Unknown interaction type: $type'), } as Interaction; } diff --git a/lib/src/http/managers/message_manager.dart b/lib/src/http/managers/message_manager.dart index 4995da639..54601349f 100644 --- a/lib/src/http/managers/message_manager.dart +++ b/lib/src/http/managers/message_manager.dart @@ -247,8 +247,6 @@ class MessageManager extends Manager { MessageComponent parseMessageComponent(Map raw) { final type = MessageComponentType.parse(raw['type'] as int); - assert(!type.isUnknown, 'Unknown message component type: $type'); - return switch (type) { MessageComponentType.actionRow => ActionRowComponent( components: parseMany(raw['components'] as List, parseMessageComponent), diff --git a/lib/src/models/application.dart b/lib/src/models/application.dart index 3ab730e01..5f0b1216c 100644 --- a/lib/src/models/application.dart +++ b/lib/src/models/application.dart @@ -184,18 +184,17 @@ class Application extends PartialApplication { ); } -final class ApplicationIntegrationType extends EnumLike { +final class ApplicationIntegrationType extends EnumLike { /// App is installable to servers. - static const guildInstall = ApplicationIntegrationType._(0); + static const guildInstall = ApplicationIntegrationType(0); /// App is installable to users. - static const userInstall = ApplicationIntegrationType._(1); + static const userInstall = ApplicationIntegrationType(1); - static const values = [guildInstall, userInstall]; - - factory ApplicationIntegrationType.parse(int value) => parseEnum(values, value); + /// @nodoc + const ApplicationIntegrationType(super.value); - const ApplicationIntegrationType._(super.value); + ApplicationIntegrationType.parse(int value) : this(value); } /// Flags for an [Application]. @@ -317,28 +316,18 @@ class ApplicationRoleConnectionMetadata with ToStringHelper { } /// The type of an [ApplicationRoleConnectionMetadata]. -final class ConnectionMetadataType extends EnumLike { - static const integerLessThanOrEqual = ConnectionMetadataType._(1); - static const integerGreaterThanOrEqual = ConnectionMetadataType._(2); - static const integerEqual = ConnectionMetadataType._(3); - static const integerNotEqual = ConnectionMetadataType._(4); - static const dateTimeLessThanOrEqual = ConnectionMetadataType._(5); - static const dateTimeGreaterThanOrEqual = ConnectionMetadataType._(6); - static const booleanEqual = ConnectionMetadataType._(7); - static const booleanNotEqual = ConnectionMetadataType._(8); - - static const values = [ - integerLessThanOrEqual, - integerGreaterThanOrEqual, - integerEqual, - integerNotEqual, - dateTimeLessThanOrEqual, - dateTimeGreaterThanOrEqual, - booleanEqual, - booleanNotEqual, - ]; - - const ConnectionMetadataType._(super.value); - - factory ConnectionMetadataType.parse(int value) => parseEnum(values, value); +final class ConnectionMetadataType extends EnumLike { + static const integerLessThanOrEqual = ConnectionMetadataType(1); + static const integerGreaterThanOrEqual = ConnectionMetadataType(2); + static const integerEqual = ConnectionMetadataType(3); + static const integerNotEqual = ConnectionMetadataType(4); + static const dateTimeLessThanOrEqual = ConnectionMetadataType(5); + static const dateTimeGreaterThanOrEqual = ConnectionMetadataType(6); + static const booleanEqual = ConnectionMetadataType(7); + static const booleanNotEqual = ConnectionMetadataType(8); + + /// @nodoc + const ConnectionMetadataType(super.value); + + ConnectionMetadataType.parse(int value) : this(value); } diff --git a/lib/src/models/channel/channel.dart b/lib/src/models/channel/channel.dart index 63eb7034d..6268ca847 100644 --- a/lib/src/models/channel/channel.dart +++ b/lib/src/models/channel/channel.dart @@ -49,65 +49,50 @@ abstract class Channel extends PartialChannel { } /// The type of a channel. -final class ChannelType extends EnumLike { +final class ChannelType extends EnumLike { /// A text channel in a [Guild]. - static const guildText = ChannelType._(0); + static const guildText = ChannelType(0); /// A DM channel with a single other recipient. - static const dm = ChannelType._(1); + static const dm = ChannelType(1); /// A voice channel in a [Guild]. - static const guildVoice = ChannelType._(2); + static const guildVoice = ChannelType(2); /// A DM channel with multiple recipients. - static const groupDm = ChannelType._(3); + static const groupDm = ChannelType(3); /// A category in a [Guild]. - static const guildCategory = ChannelType._(4); + static const guildCategory = ChannelType(4); /// An announcement channel in a [Guild]. - static const guildAnnouncement = ChannelType._(5); + static const guildAnnouncement = ChannelType(5); /// A [Thread] in an announcement channel. - static const announcementThread = ChannelType._(10); + static const announcementThread = ChannelType(10); /// A public thread. - static const publicThread = ChannelType._(11); + static const publicThread = ChannelType(11); /// A private thread. - static const privateThread = ChannelType._(12); + static const privateThread = ChannelType(12); /// A stage channel in a [Guild]. - static const guildStageVoice = ChannelType._(13); + static const guildStageVoice = ChannelType(13); /// A [Guild] directory. - static const guildDirectory = ChannelType._(14); + static const guildDirectory = ChannelType(14); /// A forum channel in a [Guild]. - static const guildForum = ChannelType._(15); + static const guildForum = ChannelType(15); /// A media channel in a [Guild]. - static const guildMedia = ChannelType._(16); - - static const List values = [ - guildText, - dm, - guildVoice, - groupDm, - guildCategory, - guildAnnouncement, - announcementThread, - publicThread, - privateThread, - guildStageVoice, - guildDirectory, - guildForum, - guildMedia, - ]; - - const ChannelType._(super.value); - - factory ChannelType.parse(int value) => parseEnum(values, value); + static const guildMedia = ChannelType(16); + + /// @nodoc + const ChannelType(super.value); + + ChannelType.parse(int value) : this(value); } /// A set of flags applied to channels. diff --git a/lib/src/models/channel/stage_instance.dart b/lib/src/models/channel/stage_instance.dart index 1ef595093..352585f25 100644 --- a/lib/src/models/channel/stage_instance.dart +++ b/lib/src/models/channel/stage_instance.dart @@ -59,13 +59,12 @@ class StageInstance extends SnowflakeEntity { } /// The privacy level of a [StageInstance]. -final class PrivacyLevel extends EnumLike { - static const public = PrivacyLevel._(1); - static const guildOnly = PrivacyLevel._(2); +final class PrivacyLevel extends EnumLike { + static const public = PrivacyLevel(1); + static const guildOnly = PrivacyLevel(2); - static const List values = [public, guildOnly]; - - const PrivacyLevel._(super.value); + /// @nodoc + const PrivacyLevel(super.value); - factory PrivacyLevel.parse(int value) => parseEnum(values, value); + PrivacyLevel.parse(int value) : this(value); } diff --git a/lib/src/models/channel/types/forum.dart b/lib/src/models/channel/types/forum.dart index e4724266d..3b198999e 100644 --- a/lib/src/models/channel/types/forum.dart +++ b/lib/src/models/channel/types/forum.dart @@ -185,26 +185,24 @@ class DefaultReaction with ToStringHelper { } /// The sorting order in a [ForumChannel]. -final class ForumSort extends EnumLike { - static const latestActivity = ForumSort._(0); - static const creationDate = ForumSort._(1); +final class ForumSort extends EnumLike { + static const latestActivity = ForumSort(0); + static const creationDate = ForumSort(1); - static const values = [latestActivity, creationDate]; - - const ForumSort._(super.value); + /// @nodoc + const ForumSort(super.value); - factory ForumSort.parse(int value) => parseEnum(values, value); + ForumSort.parse(int value) : this(value); } /// The layout in a [ForumChannel]. -final class ForumLayout extends EnumLike { - static const notSet = ForumLayout._(0); - static const listView = ForumLayout._(1); - static const galleryView = ForumLayout._(2); +final class ForumLayout extends EnumLike { + static const notSet = ForumLayout(0); + static const listView = ForumLayout(1); + static const galleryView = ForumLayout(2); - static const values = [notSet, listView, galleryView]; - - const ForumLayout._(super.value); + /// @nodoc + const ForumLayout(super.value); - factory ForumLayout.parse(int value) => parseEnum(values, value); + ForumLayout.parse(int value) : this(value); } diff --git a/lib/src/models/channel/voice_channel.dart b/lib/src/models/channel/voice_channel.dart index d297ca3f1..63129fea6 100644 --- a/lib/src/models/channel/voice_channel.dart +++ b/lib/src/models/channel/voice_channel.dart @@ -17,16 +17,15 @@ abstract class VoiceChannel implements Channel { } /// The quality mode of cameras in a [VoiceChannel]. -final class VideoQualityMode extends EnumLike { +final class VideoQualityMode extends EnumLike { /// Automatic. - static const VideoQualityMode auto = VideoQualityMode._(1); + static const auto = VideoQualityMode(1); /// 720p. - static const VideoQualityMode full = VideoQualityMode._(2); + static const full = VideoQualityMode(2); - static const List values = [auto, full]; + /// @nodoc + const VideoQualityMode(super.value); - const VideoQualityMode._(super.value); - - factory VideoQualityMode.parse(int value) => parseEnum(values, value); + VideoQualityMode.parse(int value) : this(value); } diff --git a/lib/src/models/commands/application_command.dart b/lib/src/models/commands/application_command.dart index e34a22071..29ff000d8 100644 --- a/lib/src/models/commands/application_command.dart +++ b/lib/src/models/commands/application_command.dart @@ -101,19 +101,18 @@ class ApplicationCommand extends PartialApplicationCommand { } /// The type of an [ApplicationCommand]. -final class ApplicationCommandType extends EnumLike { +final class ApplicationCommandType extends EnumLike { /// A chat input command. - static const ApplicationCommandType chatInput = ApplicationCommandType._(1); + static const chatInput = ApplicationCommandType(1); /// A user command. - static const ApplicationCommandType user = ApplicationCommandType._(2); + static const user = ApplicationCommandType(2); /// A message command. - static const ApplicationCommandType message = ApplicationCommandType._(3); + static const message = ApplicationCommandType(3); - static const List values = [chatInput, user, message]; - - const ApplicationCommandType._(super.value); + /// @nodoc + const ApplicationCommandType(super.value); - factory ApplicationCommandType.parse(int value) => parseEnum(values, value); + ApplicationCommandType.parse(int value) : this(value); } diff --git a/lib/src/models/commands/application_command_option.dart b/lib/src/models/commands/application_command_option.dart index 59605cbaf..202024f4f 100644 --- a/lib/src/models/commands/application_command_option.dart +++ b/lib/src/models/commands/application_command_option.dart @@ -71,36 +71,23 @@ class CommandOption with ToStringHelper { } /// The type of a [CommandOption]. -final class CommandOptionType extends EnumLike { - static const subCommand = CommandOptionType._(1); - static const subCommandGroup = CommandOptionType._(2); - static const string = CommandOptionType._(3); - static const integer = CommandOptionType._(4); - static const boolean = CommandOptionType._(5); - static const user = CommandOptionType._(6); - static const channel = CommandOptionType._(7); - static const role = CommandOptionType._(8); - static const mentionable = CommandOptionType._(9); - static const number = CommandOptionType._(10); - static const attachment = CommandOptionType._(11); - - static const values = [ - subCommand, - subCommandGroup, - string, - integer, - boolean, - user, - channel, - role, - mentionable, - number, - attachment, - ]; - - const CommandOptionType._(super.value); - - factory CommandOptionType.parse(int value) => parseEnum(values, value); +final class CommandOptionType extends EnumLike { + static const subCommand = CommandOptionType(1); + static const subCommandGroup = CommandOptionType(2); + static const string = CommandOptionType(3); + static const integer = CommandOptionType(4); + static const boolean = CommandOptionType(5); + static const user = CommandOptionType(6); + static const channel = CommandOptionType(7); + static const role = CommandOptionType(8); + static const mentionable = CommandOptionType(9); + static const number = CommandOptionType(10); + static const attachment = CommandOptionType(11); + + /// @nodoc + const CommandOptionType(super.value); + + CommandOptionType.parse(int value) : this(value); } /// {@template command_option_choice} diff --git a/lib/src/models/commands/application_command_permissions.dart b/lib/src/models/commands/application_command_permissions.dart index e7d880606..98d88e977 100644 --- a/lib/src/models/commands/application_command_permissions.dart +++ b/lib/src/models/commands/application_command_permissions.dart @@ -75,19 +75,18 @@ class CommandPermission with ToStringHelper { } /// The type of a [CommandPermission]. -final class CommandPermissionType extends EnumLike { +final class CommandPermissionType extends EnumLike { /// The permission applies to a role. - static const CommandPermissionType role = CommandPermissionType._(1); + static const role = CommandPermissionType(1); /// The permission applies to a user. - static const CommandPermissionType user = CommandPermissionType._(2); + static const user = CommandPermissionType(2); /// The permission applies to a channel. - static const CommandPermissionType channel = CommandPermissionType._(3); + static const channel = CommandPermissionType(3); - static const List values = [role, user, channel]; - - const CommandPermissionType._(super.value); + /// @nodoc + const CommandPermissionType(super.value); - factory CommandPermissionType.parse(int value) => parseEnum(values, value); + CommandPermissionType.parse(int value) : this(value); } diff --git a/lib/src/models/entitlement.dart b/lib/src/models/entitlement.dart index bbb62bfb7..142997850 100644 --- a/lib/src/models/entitlement.dart +++ b/lib/src/models/entitlement.dart @@ -77,43 +77,32 @@ class Entitlement extends PartialEntitlement { } /// The type of an [Entitlement]. -final class EntitlementType extends EnumLike { +final class EntitlementType extends EnumLike { /// Entitlement was purchased by user. - static const EntitlementType purchase = EntitlementType._(1); + static const EntitlementType purchase = EntitlementType(1); /// Entitlement was granted by Discord Nitro subscription. - static const EntitlementType premiumSubscription = EntitlementType._(2); + static const EntitlementType premiumSubscription = EntitlementType(2); /// Entitlement was gifted by developer. - static const EntitlementType developerGift = EntitlementType._(3); + static const EntitlementType developerGift = EntitlementType(3); /// Entitlement was purchased by a dev in application test mode. - static const EntitlementType testModePurchase = EntitlementType._(4); + static const EntitlementType testModePurchase = EntitlementType(4); /// Entitlement was granted when the SKU was free. - static const EntitlementType freePurchase = EntitlementType._(5); + static const EntitlementType freePurchase = EntitlementType(5); /// Entitlement was gifted by another user. - static const EntitlementType userGift = EntitlementType._(6); + static const EntitlementType userGift = EntitlementType(6); /// Entitlement was claimed by user for free as a Nitro Subscriber. - static const EntitlementType premiumPurchase = EntitlementType._(7); + static const EntitlementType premiumPurchase = EntitlementType(7); /// Entitlement was purchased as an app subscription. - static const EntitlementType applicationSubscription = EntitlementType._(8); - - static const List values = [ - purchase, - premiumSubscription, - developerGift, - testModePurchase, - freePurchase, - userGift, - premiumPurchase, - applicationSubscription, - ]; - - const EntitlementType._(super.value); - - factory EntitlementType.parse(int value) => parseEnum(values, value); + static const EntitlementType applicationSubscription = EntitlementType(8); + + const EntitlementType(super.value); + + EntitlementType.parse(int value) : this(value); } diff --git a/lib/src/models/guild/audit_log.dart b/lib/src/models/guild/audit_log.dart index f1c8aa0c0..c8a3ea086 100644 --- a/lib/src/models/guild/audit_log.dart +++ b/lib/src/models/guild/audit_log.dart @@ -82,126 +82,68 @@ class AuditLogChange with ToStringHelper { } /// The type of event an [AuditLogEntry] represents. -final class AuditLogEvent extends EnumLike { - static const guildUpdate = AuditLogEvent._(1); - static const channelCreate = AuditLogEvent._(10); - static const channelUpdate = AuditLogEvent._(11); - static const channelDelete = AuditLogEvent._(12); - static const channelOverwriteCreate = AuditLogEvent._(13); - static const channelOverwriteUpdate = AuditLogEvent._(14); - static const channelOverwriteDelete = AuditLogEvent._(15); - static const memberKick = AuditLogEvent._(20); - static const memberPrune = AuditLogEvent._(21); - static const memberBanAdd = AuditLogEvent._(22); - static const memberBanRemove = AuditLogEvent._(23); - static const memberUpdate = AuditLogEvent._(24); - static const memberRoleUpdate = AuditLogEvent._(25); - static const memberMove = AuditLogEvent._(26); - static const memberDisconnect = AuditLogEvent._(27); - static const botAdd = AuditLogEvent._(28); - static const roleCreate = AuditLogEvent._(30); - static const roleUpdate = AuditLogEvent._(31); - static const roleDelete = AuditLogEvent._(32); - static const inviteCreate = AuditLogEvent._(40); - static const inviteUpdate = AuditLogEvent._(41); - static const inviteDelete = AuditLogEvent._(42); - static const webhookCreate = AuditLogEvent._(50); - static const webhookUpdate = AuditLogEvent._(51); - static const webhookDelete = AuditLogEvent._(52); - static const emojiCreate = AuditLogEvent._(60); - static const emojiUpdate = AuditLogEvent._(61); - static const emojiDelete = AuditLogEvent._(62); - static const messageDelete = AuditLogEvent._(72); - static const messageBulkDelete = AuditLogEvent._(73); - static const messagePin = AuditLogEvent._(74); - static const messageUnpin = AuditLogEvent._(75); - static const integrationCreate = AuditLogEvent._(80); - static const integrationUpdate = AuditLogEvent._(81); - static const integrationDelete = AuditLogEvent._(82); - static const stageInstanceCreate = AuditLogEvent._(83); - static const stageInstanceUpdate = AuditLogEvent._(84); - static const stageInstanceDelete = AuditLogEvent._(85); - static const stickerCreate = AuditLogEvent._(90); - static const stickerUpdate = AuditLogEvent._(91); - static const stickerDelete = AuditLogEvent._(92); - static const guildScheduledEventCreate = AuditLogEvent._(100); - static const guildScheduledEventUpdate = AuditLogEvent._(101); - static const guildScheduledEventDelete = AuditLogEvent._(102); - static const threadCreate = AuditLogEvent._(110); - static const threadUpdate = AuditLogEvent._(111); - static const threadDelete = AuditLogEvent._(112); - static const applicationCommandPermissionUpdate = AuditLogEvent._(121); - static const autoModerationRuleCreate = AuditLogEvent._(140); - static const autoModerationRuleUpdate = AuditLogEvent._(141); - static const autoModerationRuleDelete = AuditLogEvent._(142); - static const autoModerationBlockMessage = AuditLogEvent._(143); - static const autoModerationFlagToChannel = AuditLogEvent._(144); - static const autoModerationUserCommunicationDisabled = AuditLogEvent._(145); - static const creatorMonetizationRequestCreated = AuditLogEvent._(150); - static const creatorMonetizationTermsAccepted = AuditLogEvent._(151); - - static const values = [ - guildUpdate, - channelCreate, - channelUpdate, - channelDelete, - channelOverwriteCreate, - channelOverwriteUpdate, - channelOverwriteDelete, - memberKick, - memberPrune, - memberBanAdd, - memberBanRemove, - memberUpdate, - memberRoleUpdate, - memberMove, - memberDisconnect, - botAdd, - roleCreate, - roleUpdate, - roleDelete, - inviteCreate, - inviteUpdate, - inviteDelete, - webhookCreate, - webhookUpdate, - webhookDelete, - emojiCreate, - emojiUpdate, - emojiDelete, - messageDelete, - messageBulkDelete, - messagePin, - messageUnpin, - integrationCreate, - integrationUpdate, - integrationDelete, - stageInstanceCreate, - stageInstanceUpdate, - stageInstanceDelete, - stickerCreate, - stickerUpdate, - stickerDelete, - guildScheduledEventCreate, - guildScheduledEventUpdate, - guildScheduledEventDelete, - threadCreate, - threadUpdate, - threadDelete, - applicationCommandPermissionUpdate, - autoModerationRuleCreate, - autoModerationRuleUpdate, - autoModerationRuleDelete, - autoModerationBlockMessage, - autoModerationFlagToChannel, - autoModerationUserCommunicationDisabled, - creatorMonetizationRequestCreated, - creatorMonetizationTermsAccepted, - ]; - - const AuditLogEvent._(super.value); - - factory AuditLogEvent.parse(int value) => parseEnum(values, value); +final class AuditLogEvent extends EnumLike { + static const guildUpdate = AuditLogEvent(1); + static const channelCreate = AuditLogEvent(10); + static const channelUpdate = AuditLogEvent(11); + static const channelDelete = AuditLogEvent(12); + static const channelOverwriteCreate = AuditLogEvent(13); + static const channelOverwriteUpdate = AuditLogEvent(14); + static const channelOverwriteDelete = AuditLogEvent(15); + static const memberKick = AuditLogEvent(20); + static const memberPrune = AuditLogEvent(21); + static const memberBanAdd = AuditLogEvent(22); + static const memberBanRemove = AuditLogEvent(23); + static const memberUpdate = AuditLogEvent(24); + static const memberRoleUpdate = AuditLogEvent(25); + static const memberMove = AuditLogEvent(26); + static const memberDisconnect = AuditLogEvent(27); + static const botAdd = AuditLogEvent(28); + static const roleCreate = AuditLogEvent(30); + static const roleUpdate = AuditLogEvent(31); + static const roleDelete = AuditLogEvent(32); + static const inviteCreate = AuditLogEvent(40); + static const inviteUpdate = AuditLogEvent(41); + static const inviteDelete = AuditLogEvent(42); + static const webhookCreate = AuditLogEvent(50); + static const webhookUpdate = AuditLogEvent(51); + static const webhookDelete = AuditLogEvent(52); + static const emojiCreate = AuditLogEvent(60); + static const emojiUpdate = AuditLogEvent(61); + static const emojiDelete = AuditLogEvent(62); + static const messageDelete = AuditLogEvent(72); + static const messageBulkDelete = AuditLogEvent(73); + static const messagePin = AuditLogEvent(74); + static const messageUnpin = AuditLogEvent(75); + static const integrationCreate = AuditLogEvent(80); + static const integrationUpdate = AuditLogEvent(81); + static const integrationDelete = AuditLogEvent(82); + static const stageInstanceCreate = AuditLogEvent(83); + static const stageInstanceUpdate = AuditLogEvent(84); + static const stageInstanceDelete = AuditLogEvent(85); + static const stickerCreate = AuditLogEvent(90); + static const stickerUpdate = AuditLogEvent(91); + static const stickerDelete = AuditLogEvent(92); + static const guildScheduledEventCreate = AuditLogEvent(100); + static const guildScheduledEventUpdate = AuditLogEvent(101); + static const guildScheduledEventDelete = AuditLogEvent(102); + static const threadCreate = AuditLogEvent(110); + static const threadUpdate = AuditLogEvent(111); + static const threadDelete = AuditLogEvent(112); + static const applicationCommandPermissionUpdate = AuditLogEvent(121); + static const autoModerationRuleCreate = AuditLogEvent(140); + static const autoModerationRuleUpdate = AuditLogEvent(141); + static const autoModerationRuleDelete = AuditLogEvent(142); + static const autoModerationBlockMessage = AuditLogEvent(143); + static const autoModerationFlagToChannel = AuditLogEvent(144); + static const autoModerationUserCommunicationDisabled = AuditLogEvent(145); + static const creatorMonetizationRequestCreated = AuditLogEvent(150); + static const creatorMonetizationTermsAccepted = AuditLogEvent(151); + + /// @nodoc + const AuditLogEvent(super.value); + + AuditLogEvent.parse(int value) : this(value); } /// {@template audit_log_entry_info} diff --git a/lib/src/models/guild/auto_moderation.dart b/lib/src/models/guild/auto_moderation.dart index c856ac150..822a8a3c2 100644 --- a/lib/src/models/guild/auto_moderation.dart +++ b/lib/src/models/guild/auto_moderation.dart @@ -84,42 +84,40 @@ class AutoModerationRule extends PartialAutoModerationRule { } /// The type of event on which an [AutoModerationRule] triggers. -final class AutoModerationEventType extends EnumLike { +final class AutoModerationEventType extends EnumLike { /// When a member sends or edits a message in the guild. - static const AutoModerationEventType messageSend = AutoModerationEventType._(1); + static const messageSend = AutoModerationEventType(1); /// When a member edits their profile. - static const AutoModerationEventType memberUpdate = AutoModerationEventType._(2); + static const memberUpdate = AutoModerationEventType(2); - static const List values = [messageSend, memberUpdate]; - - const AutoModerationEventType._(super.value); + /// @nodoc + const AutoModerationEventType(super.value); - factory AutoModerationEventType.parse(int value) => parseEnum(values, value); + AutoModerationEventType.parse(int value) : this(value); } /// The type of a trigger for an [AutoModerationRule] -final class TriggerType extends EnumLike { +final class TriggerType extends EnumLike { /// Check if content contains words from a user defined list of keywords. - static const TriggerType keyword = TriggerType._(1); + static const keyword = TriggerType(1); /// Check if content represents generic spam. - static const TriggerType spam = TriggerType._(3); + static const spam = TriggerType(3); /// Check if content contains words from internal pre-defined wordsets. - static const TriggerType keywordPreset = TriggerType._(4); + static const keywordPreset = TriggerType(4); /// Check if content contains more unique mentions than allowed. - static const TriggerType mentionSpam = TriggerType._(5); + static const mentionSpam = TriggerType(5); /// Check if member profile contains words from a user defined list of keywords. - static const TriggerType memberProfile = TriggerType._(6); + static const memberProfile = TriggerType(6); - static const List values = [keyword, spam, keywordPreset, mentionSpam, memberProfile]; - - const TriggerType._(super.value); + /// @nodoc + const TriggerType(super.value); - factory TriggerType.parse(int value) => parseEnum(values, value); + TriggerType.parse(int value) : this(value); } /// {@template trigger_metadata} @@ -170,16 +168,15 @@ class TriggerMetadata with ToStringHelper implements TriggerMetadataBuilder { } /// A preset list of trigger keywords for an [AutoModerationRule]. -final class KeywordPresetType extends EnumLike { - static const profanity = KeywordPresetType._(1); - static const sexualContent = KeywordPresetType._(2); - static const slurs = KeywordPresetType._(3); +final class KeywordPresetType extends EnumLike { + static const profanity = KeywordPresetType(1); + static const sexualContent = KeywordPresetType(2); + static const slurs = KeywordPresetType(3); - static const values = [profanity, sexualContent, slurs]; - - const KeywordPresetType._(super.value); + /// @nodoc + const KeywordPresetType(super.value); - factory KeywordPresetType.parse(int value) => parseEnum(values, value); + KeywordPresetType.parse(int value) : this(value); } /// {@template auto_moderation_action} @@ -209,17 +206,16 @@ class AutoModerationAction with ToStringHelper implements AutoModerationActionBu } /// The type of action for an [AutoModerationAction]. -final class ActionType extends EnumLike { - static const blockMessage = ActionType._(1); - static const sendAlertMessage = ActionType._(2); - static const timeout = ActionType._(3); - static const blockMemberInteraction = ActionType._(4); +final class ActionType extends EnumLike { + static const blockMessage = ActionType(1); + static const sendAlertMessage = ActionType(2); + static const timeout = ActionType(3); + static const blockMemberInteraction = ActionType(4); - static const values = [blockMessage, sendAlertMessage, timeout, blockMemberInteraction]; - - const ActionType._(super.value); + /// @nodoc + const ActionType(super.value); - factory ActionType.parse(int value) => parseEnum(values, value); + ActionType.parse(int value) : this(value); } /// {@template action_metadata} diff --git a/lib/src/models/guild/guild.dart b/lib/src/models/guild/guild.dart index 305c46294..ec8d94e2a 100644 --- a/lib/src/models/guild/guild.dart +++ b/lib/src/models/guild/guild.dart @@ -467,43 +467,40 @@ class Guild extends UserGuild { } /// The verification level for a guild. -final class VerificationLevel extends EnumLike { - static const none = VerificationLevel._(0); - static const low = VerificationLevel._(1); - static const medium = VerificationLevel._(2); - static const high = VerificationLevel._(3); - static const veryHigh = VerificationLevel._(4); +final class VerificationLevel extends EnumLike { + static const none = VerificationLevel(0); + static const low = VerificationLevel(1); + static const medium = VerificationLevel(2); + static const high = VerificationLevel(3); + static const veryHigh = VerificationLevel(4); - static const values = [none, low, medium, high, veryHigh]; - - const VerificationLevel._(super.value); + /// @nodoc + const VerificationLevel(super.value); - factory VerificationLevel.parse(int value) => parseEnum(values, value); + VerificationLevel.parse(int value) : this(value); } /// The level at which message notifications are sent in a guild. -final class MessageNotificationLevel extends EnumLike { - static const allMessages = MessageNotificationLevel._(0); - static const onlyMentions = MessageNotificationLevel._(1); - - static const values = [allMessages, onlyMentions]; +final class MessageNotificationLevel extends EnumLike { + static const allMessages = MessageNotificationLevel(0); + static const onlyMentions = MessageNotificationLevel(1); - const MessageNotificationLevel._(super.value); + /// @nodoc + const MessageNotificationLevel(super.value); - factory MessageNotificationLevel.parse(int value) => parseEnum(values, value); + MessageNotificationLevel.parse(int value) : this(value); } /// The level of explicit content filtering in a guild. -final class ExplicitContentFilterLevel extends EnumLike { - static const disabled = ExplicitContentFilterLevel._(0); - static const membersWithoutRoles = ExplicitContentFilterLevel._(1); - static const allMembers = ExplicitContentFilterLevel._(2); - - static const values = [disabled, membersWithoutRoles, allMembers]; +final class ExplicitContentFilterLevel extends EnumLike { + static const disabled = ExplicitContentFilterLevel(0); + static const membersWithoutRoles = ExplicitContentFilterLevel(1); + static const allMembers = ExplicitContentFilterLevel(2); - const ExplicitContentFilterLevel._(super.value); + /// @nodoc + const ExplicitContentFilterLevel(super.value); - factory ExplicitContentFilterLevel.parse(int value) => parseEnum(values, value); + ExplicitContentFilterLevel.parse(int value) : this(value); } /// Features that can be enabled in certain guilds. @@ -676,13 +673,14 @@ class GuildFeatures extends Flags { } /// The MFA level required for moderators of a guild. -final class MfaLevel extends EnumLike { - static const none = MfaLevel._(0); - static const elevated = MfaLevel._(1); +final class MfaLevel extends EnumLike { + static const none = MfaLevel(0); + static const elevated = MfaLevel(1); - const MfaLevel._(super.value); + /// @nodoc + const MfaLevel(super.value); - factory MfaLevel.parse(int value) => parseEnum([none, elevated], value); + MfaLevel.parse(int value) : this(value); } /// The configuration of a guild's system channel. @@ -728,29 +726,27 @@ class SystemChannelFlags extends Flags { } /// The premium tier of a guild. -final class PremiumTier extends EnumLike { - static const none = PremiumTier._(0); - static const one = PremiumTier._(1); - static const two = PremiumTier._(2); - static const three = PremiumTier._(3); +final class PremiumTier extends EnumLike { + static const none = PremiumTier(0); + static const one = PremiumTier(1); + static const two = PremiumTier(2); + static const three = PremiumTier(3); - static const values = [none, one, two, three]; + /// nodoc + const PremiumTier(super.value); - const PremiumTier._(super.value); - - factory PremiumTier.parse(int value) => parseEnum(values, value); + PremiumTier.parse(int value) : this(value); } /// The NSFW level of a guild. -final class NsfwLevel extends EnumLike { - static const unset = NsfwLevel._(0); - static const explicit = NsfwLevel._(1); - static const safe = NsfwLevel._(2); - static const ageRestricted = NsfwLevel._(3); - - static const values = [unset, explicit, safe, ageRestricted]; +final class NsfwLevel extends EnumLike { + static const unset = NsfwLevel(0); + static const explicit = NsfwLevel(1); + static const safe = NsfwLevel(2); + static const ageRestricted = NsfwLevel(3); - const NsfwLevel._(super.value); + /// nodoc + const NsfwLevel(super.value); - factory NsfwLevel.parse(int value) => parseEnum(values, value); + NsfwLevel.parse(int value) : this(value); } diff --git a/lib/src/models/guild/integration.dart b/lib/src/models/guild/integration.dart index f2d1f2fff..18f6eadc4 100644 --- a/lib/src/models/guild/integration.dart +++ b/lib/src/models/guild/integration.dart @@ -95,15 +95,14 @@ class Integration extends PartialIntegration { } /// The behavior of an integration when a member's subscription expires. -final class IntegrationExpireBehavior extends EnumLike { - static const removeRole = IntegrationExpireBehavior._(0); - static const kick = IntegrationExpireBehavior._(1); +final class IntegrationExpireBehavior extends EnumLike { + static const removeRole = IntegrationExpireBehavior(0); + static const kick = IntegrationExpireBehavior(1); - static const List values = [removeRole, kick]; - - const IntegrationExpireBehavior._(super.value); + /// @nodoc + const IntegrationExpireBehavior(super.value); - factory IntegrationExpireBehavior.parse(int value) => parseEnum(values, value); + IntegrationExpireBehavior.parse(int value) : this(value); } /// {@template integration_account} diff --git a/lib/src/models/guild/onboarding.dart b/lib/src/models/guild/onboarding.dart index effe387c3..00938154f 100644 --- a/lib/src/models/guild/onboarding.dart +++ b/lib/src/models/guild/onboarding.dart @@ -83,15 +83,14 @@ class OnboardingPrompt with ToStringHelper { } /// The type of an [Onboarding] prompt. -final class OnboardingPromptType extends EnumLike { - static const multipleChoice = OnboardingPromptType._(0); - static const dropdown = OnboardingPromptType._(1); +final class OnboardingPromptType extends EnumLike { + static const multipleChoice = OnboardingPromptType(0); + static const dropdown = OnboardingPromptType(1); - static const List values = [multipleChoice, dropdown]; - - const OnboardingPromptType._(super.value); + /// @nodoc + const OnboardingPromptType(super.value); - factory OnboardingPromptType.parse(int value) => parseEnum(values, value); + OnboardingPromptType.parse(int value) : this(value); } /// {@template onboarding_prompt_option} diff --git a/lib/src/models/guild/scheduled_event.dart b/lib/src/models/guild/scheduled_event.dart index 3d22c7627..5ce11417f 100644 --- a/lib/src/models/guild/scheduled_event.dart +++ b/lib/src/models/guild/scheduled_event.dart @@ -118,30 +118,28 @@ class ScheduledEvent extends PartialScheduledEvent { } /// The status of a [ScheduledEvent]. -final class EventStatus extends EnumLike { - static const scheduled = EventStatus._(1); - static const active = EventStatus._(2); - static const completed = EventStatus._(3); - static const cancelled = EventStatus._(4); +final class EventStatus extends EnumLike { + static const scheduled = EventStatus(1); + static const active = EventStatus(2); + static const completed = EventStatus(3); + static const cancelled = EventStatus(4); - static const List values = [scheduled, active, completed, cancelled]; - - const EventStatus._(super.value); + /// @nodoc + const EventStatus(super.value); - factory EventStatus.parse(int value) => parseEnum(values, value); + EventStatus.parse(int value) : this(value); } /// The type of the entity associated with a [ScheduledEvent]. -final class ScheduledEntityType extends EnumLike { - static const stageInstance = ScheduledEntityType._(1); - static const voice = ScheduledEntityType._(2); - static const external = ScheduledEntityType._(3); +final class ScheduledEntityType extends EnumLike { + static const stageInstance = ScheduledEntityType(1); + static const voice = ScheduledEntityType(2); + static const external = ScheduledEntityType(3); - static const List values = [stageInstance, voice, external]; - - const ScheduledEntityType._(super.value); + /// @nodoc + const ScheduledEntityType(super.value); - factory ScheduledEntityType.parse(int value) => parseEnum(values, value); + ScheduledEntityType.parse(int value) : this(value); } /// {@template entity_metadata} diff --git a/lib/src/models/interaction.dart b/lib/src/models/interaction.dart index 1c782a9dc..148eb00de 100644 --- a/lib/src/models/interaction.dart +++ b/lib/src/models/interaction.dart @@ -26,25 +26,20 @@ import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-context-types -final class InteractionContextType extends EnumLike { +final class InteractionContextType extends EnumLike { /// Interaction can be used within servers. - static const InteractionContextType guild = InteractionContextType._(0); + static const InteractionContextType guild = InteractionContextType(0); /// Interaction can be used within DMs with the app's bot user. - static const InteractionContextType botDm = InteractionContextType._(1); + static const InteractionContextType botDm = InteractionContextType(1); /// Interaction can be used within Group DMs and DMs other than the app's bot user. - static const InteractionContextType privateChannel = InteractionContextType._(2); + static const InteractionContextType privateChannel = InteractionContextType(2); - static const List values = [ - guild, - botDm, - privateChannel, - ]; - - const InteractionContextType._(super.value); + /// @nodoc + const InteractionContextType(super.value); - factory InteractionContextType.parse(int value) => parseEnum(values, value); + InteractionContextType.parse(int value) : this(value); } /// {@template interaction} @@ -422,24 +417,17 @@ class ApplicationCommandAutocompleteInteraction extends Interaction { - static const InteractionType ping = InteractionType._(1); - static const InteractionType applicationCommand = InteractionType._(2); - static const InteractionType messageComponent = InteractionType._(3); - static const InteractionType applicationCommandAutocomplete = InteractionType._(4); - static const InteractionType modalSubmit = InteractionType._(5); - - static const List values = [ - ping, - applicationCommand, - messageComponent, - applicationCommandAutocomplete, - modalSubmit, - ]; - - const InteractionType._(super.value); - - factory InteractionType.parse(int value) => parseEnum(values, value); +final class InteractionType extends EnumLike { + static const ping = InteractionType(1); + static const applicationCommand = InteractionType(2); + static const messageComponent = InteractionType(3); + static const applicationCommandAutocomplete = InteractionType(4); + static const modalSubmit = InteractionType(5); + + /// @nodoc + const InteractionType(super.value); + + InteractionType.parse(int value) : this(value); } /// {@template application_command_interaction_data} diff --git a/lib/src/models/invite/invite.dart b/lib/src/models/invite/invite.dart index 50d218334..18bad562f 100644 --- a/lib/src/models/invite/invite.dart +++ b/lib/src/models/invite/invite.dart @@ -70,13 +70,12 @@ class Invite with ToStringHelper { } /// The type of an [Invite]'s target. -final class TargetType extends EnumLike { - static const stream = TargetType._(1); - static const embeddedApplication = TargetType._(2); +final class TargetType extends EnumLike { + static const stream = TargetType(1); + static const embeddedApplication = TargetType(2); - static const List values = [stream, embeddedApplication]; - - const TargetType._(super.value); + /// @nodoc + const TargetType(super.value); - factory TargetType.parse(int value) => parseEnum(values, value); + TargetType.parse(int value) : this(value); } diff --git a/lib/src/models/locale.dart b/lib/src/models/locale.dart index c5f337f00..fde9b2623 100644 --- a/lib/src/models/locale.dart +++ b/lib/src/models/locale.dart @@ -1,77 +1,40 @@ -import 'package:nyxx/src/utils/enum_like.dart'; - /// A language locale available in the Discord client. /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/reference#locales -final class Locale extends EnumLike { - static const Locale id = Locale._('id', 'Indonesian', 'Bahasa Indonesia'); - static const Locale da = Locale._('da', 'Danish', 'Dansk'); - static const Locale de = Locale._('de', 'German', 'Deutsch'); - static const Locale enGb = Locale._('en-GB', 'English, UK', 'English, UK'); - static const Locale enUs = Locale._('en-US', 'English, US', 'English, US'); - static const Locale esEs = Locale._('es-ES', 'Spanish', 'Español'); - static const Locale es419 = Locale._('es-419', 'Spanish, LATAM', 'Español, LATAM'); - static const Locale fr = Locale._('fr', 'French', 'Français'); - static const Locale hr = Locale._('hr', 'Croatian', 'Hrvatski'); - static const Locale it = Locale._('it', 'Italian', 'Italiano'); - static const Locale lt = Locale._('lt', 'Lithuanian', 'Lietuviškai'); - static const Locale hu = Locale._('hu', 'Hungarian', 'Magyar'); - static const Locale nl = Locale._('nl', 'Dutch', 'Nederlands'); - static const Locale no = Locale._('no', 'Norwegian', 'Norsk'); - static const Locale pl = Locale._('pl', 'Polish', 'Polski'); - static const Locale ptBr = Locale._('pt-BR', 'Portuguese, Brazilian', 'Português do Brasil'); - static const Locale ro = Locale._('ro', 'Romanian, Romania', 'Română'); - static const Locale fi = Locale._('fi', 'Finnish', 'Suomi'); - static const Locale svSe = Locale._('sv-SE', 'Swedish', 'Svenska'); - static const Locale vi = Locale._('vi', 'Vietnamese', 'Tiếng Việt'); - static const Locale tr = Locale._('tr', 'Turkish', 'Türkçe'); - static const Locale cs = Locale._('cs', 'Czech', 'Čeština'); - static const Locale el = Locale._('el', 'Greek', 'Ελληνικά'); - static const Locale bg = Locale._('bg', 'Bulgarian', 'български'); - static const Locale ru = Locale._('ru', 'Russian', 'Pусский'); - static const Locale uk = Locale._('uk', 'Ukrainian', 'Українська'); - static const Locale hi = Locale._('hi', 'Hindi', 'हिन्दी'); - static const Locale th = Locale._('th', 'Thai', 'ไทย'); - static const Locale zhCn = Locale._('zh-CN', 'Chinese, China', '中文'); - static const Locale ja = Locale._('ja', 'Japanese', '日本語'); - static const Locale zhTw = Locale._('zh-TW', 'Chinese, Taiwan', '繁體中文'); - static const Locale ko = Locale._('ko', 'Korean', '한국어'); - - static const List values = [ - id, - da, - de, - enGb, - enUs, - esEs, - es419, - fr, - hr, - it, - lt, - hu, - nl, - no, - pl, - ptBr, - ro, - fi, - svSe, - vi, - tr, - cs, - el, - bg, - ru, - uk, - hi, - th, - zhCn, - ja, - zhTw, - ko, - ]; +enum Locale { + id._('id', 'Indonesian', 'Bahasa Indonesia'), + da._('da', 'Danish', 'Dansk'), + de._('de', 'German', 'Deutsch'), + enGb._('en-GB', 'English, UK', 'English, UK'), + enUs._('en-US', 'English, US', 'English, US'), + esEs._('es-ES', 'Spanish', 'Español'), + es419._('es-419', 'Spanish, LATAM', 'Español, LATAM'), + fr._('fr', 'French', 'Français'), + hr._('hr', 'Croatian', 'Hrvatski'), + it._('it', 'Italian', 'Italiano'), + lt._('lt', 'Lithuanian', 'Lietuviškai'), + hu._('hu', 'Hungarian', 'Magyar'), + nl._('nl', 'Dutch', 'Nederlands'), + no._('no', 'Norwegian', 'Norsk'), + pl._('pl', 'Polish', 'Polski'), + ptBr._('pt-BR', 'Portuguese, Brazilian', 'Português do Brasil'), + ro._('ro', 'Romanian, Romania', 'Română'), + fi._('fi', 'Finnish', 'Suomi'), + svSe._('sv-SE', 'Swedish', 'Svenska'), + vi._('vi', 'Vietnamese', 'Tiếng Việt'), + tr._('tr', 'Turkish', 'Türkçe'), + cs._('cs', 'Czech', 'Čeština'), + el._('el', 'Greek', 'Ελληνικά'), + bg._('bg', 'Bulgarian', 'български'), + ru._('ru', 'Russian', 'Pусский'), + uk._('uk', 'Ukrainian', 'Українська'), + hi._('hi', 'Hindi', 'हिन्दी'), + th._('th', 'Thai', 'ไทย'), + zhCn._('zh-CN', 'Chinese, China', '中文'), + ja._('ja', 'Japanese', '日本語'), + zhTw._('zh-TW', 'Chinese, Taiwan', '繁體中文'), + ko._('ko', 'Korean', '한국어'); /// The identifier for this locale. final String identifier; @@ -82,11 +45,14 @@ final class Locale extends EnumLike { /// The native name of this locale. final String nativeName; - const Locale._(this.identifier, this.name, this.nativeName) : super(identifier); + const Locale._(this.identifier, this.name, this.nativeName); /// Parse a string into a locale. /// /// [identifier] must be a string containing an identifier matching [Locale.identifier] for one of /// the listed locales. - factory Locale.parse(String identifier) => parseEnum(values, identifier); + factory Locale.parse(String identifier) => values.firstWhere( + (loc) => loc.identifier == identifier, + orElse: () => throw FormatException('Unknown Locale', identifier), + ); } diff --git a/lib/src/models/message/activity.dart b/lib/src/models/message/activity.dart index 32ae94856..1d7cd2ded 100644 --- a/lib/src/models/message/activity.dart +++ b/lib/src/models/message/activity.dart @@ -26,15 +26,14 @@ class MessageActivity with ToStringHelper { /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/channel#message-object-message-activity-types -final class MessageActivityType extends EnumLike { - static const join = MessageActivityType._(1); - static const spectate = MessageActivityType._(2); - static const listen = MessageActivityType._(3); - static const joinRequest = MessageActivityType._(5); +final class MessageActivityType extends EnumLike { + static const join = MessageActivityType(1); + static const spectate = MessageActivityType(2); + static const listen = MessageActivityType(3); + static const joinRequest = MessageActivityType(5); - static const List values = [join, spectate, listen, joinRequest]; - - const MessageActivityType._(super.value); + /// @nodoc + const MessageActivityType(super.value); - factory MessageActivityType.parse(int value) => parseEnum(values, value); + MessageActivityType.parse(int value) : this(value); } diff --git a/lib/src/models/message/component.dart b/lib/src/models/message/component.dart index 49ff67cb5..d7fc87960 100644 --- a/lib/src/models/message/component.dart +++ b/lib/src/models/message/component.dart @@ -5,21 +5,20 @@ import 'package:nyxx/src/utils/enum_like.dart'; import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// The type of a [MessageComponent]. -final class MessageComponentType extends EnumLike { - static const actionRow = MessageComponentType._(1); - static const button = MessageComponentType._(2); - static const stringSelect = MessageComponentType._(3); - static const textInput = MessageComponentType._(4); - static const userSelect = MessageComponentType._(5); - static const roleSelect = MessageComponentType._(6); - static const mentionableSelect = MessageComponentType._(7); - static const channelSelect = MessageComponentType._(8); +final class MessageComponentType extends EnumLike { + static const actionRow = MessageComponentType(1); + static const button = MessageComponentType(2); + static const stringSelect = MessageComponentType(3); + static const textInput = MessageComponentType(4); + static const userSelect = MessageComponentType(5); + static const roleSelect = MessageComponentType(6); + static const mentionableSelect = MessageComponentType(7); + static const channelSelect = MessageComponentType(8); - static const List values = [actionRow, button, stringSelect, textInput, userSelect, roleSelect, mentionableSelect, channelSelect]; - - const MessageComponentType._(super.value); + /// @nodoc + const MessageComponentType(super.value); - factory MessageComponentType.parse(int value) => parseEnum(values, value); + MessageComponentType.parse(int value) : this(value); } /// A component in a [Message]. @@ -77,18 +76,17 @@ class ButtonComponent extends MessageComponent { } /// The style of a [ButtonComponent]. -final class ButtonStyle extends EnumLike { - static const primary = ButtonStyle._(1); - static const secondary = ButtonStyle._(2); - static const success = ButtonStyle._(3); - static const danger = ButtonStyle._(4); - static const link = ButtonStyle._(5); +final class ButtonStyle extends EnumLike { + static const primary = ButtonStyle(1); + static const secondary = ButtonStyle(2); + static const success = ButtonStyle(3); + static const danger = ButtonStyle(4); + static const link = ButtonStyle(5); - static const List values = [primary, secondary, success, danger, link]; - - const ButtonStyle._(super.value); + /// @nodoc + const ButtonStyle(super.value); - factory ButtonStyle.parse(int value) => parseEnum(values, value); + ButtonStyle.parse(int value) : this(value); } /// A dropdown menu in which users can select from on or more choices. @@ -140,16 +138,15 @@ class SelectMenuComponent extends MessageComponent { } /// The type of a [SelectMenuDefaultValue]. -final class SelectMenuDefaultValueType extends EnumLike { - static const user = SelectMenuDefaultValueType._('user'); - static const role = SelectMenuDefaultValueType._('role'); - static const channel = SelectMenuDefaultValueType._('channel'); +final class SelectMenuDefaultValueType extends EnumLike { + static const user = SelectMenuDefaultValueType('user'); + static const role = SelectMenuDefaultValueType('role'); + static const channel = SelectMenuDefaultValueType('channel'); - static const List values = [user, role, channel]; - - const SelectMenuDefaultValueType._(super.value); + /// @nodoc + const SelectMenuDefaultValueType(super.value); - factory SelectMenuDefaultValueType.parse(String value) => parseEnum(values, value); + SelectMenuDefaultValueType.parse(String value) : this(value); } /// A default value in a [SelectMenuComponent]. @@ -237,13 +234,12 @@ class TextInputComponent extends MessageComponent { } /// The type of a [TextInputComponent]. -final class TextInputStyle extends EnumLike { - static const short = TextInputStyle._(1); - static const paragraph = TextInputStyle._(2); +final class TextInputStyle extends EnumLike { + static const short = TextInputStyle(1); + static const paragraph = TextInputStyle(2); - static const List values = [short, paragraph]; - - const TextInputStyle._(super.value); + /// @nodoc + const TextInputStyle(super.value); - factory TextInputStyle.parse(int value) => parseEnum(values, value); + TextInputStyle.parse(int value) : this(value); } diff --git a/lib/src/models/message/message.dart b/lib/src/models/message/message.dart index 3e5d9e157..85d7d3e8b 100644 --- a/lib/src/models/message/message.dart +++ b/lib/src/models/message/message.dart @@ -253,84 +253,47 @@ class Message extends PartialMessage { /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/channel#message-object-message-types -final class MessageType extends EnumLike { - static const normal = MessageType._(0); - static const recipientAdd = MessageType._(1); - static const recipientRemove = MessageType._(2); - static const call = MessageType._(3); - static const channelNameChange = MessageType._(4); - static const channelIconChange = MessageType._(5); - static const channelPinnedMessage = MessageType._(6); - static const userJoin = MessageType._(7); - static const guildBoost = MessageType._(8); - static const guildBoostTier1 = MessageType._(9); - static const guildBoostTier2 = MessageType._(10); - static const guildBoostTier3 = MessageType._(11); - static const channelFollowAdd = MessageType._(12); - static const guildDiscoveryDisqualified = MessageType._(14); - static const guildDiscoveryRequalified = MessageType._(15); - static const guildDiscoveryGracePeriodInitialWarning = MessageType._(16); - static const guildDiscoveryGracePeriodFinalWarning = MessageType._(17); - static const threadCreated = MessageType._(18); - static const reply = MessageType._(19); - static const chatInputCommand = MessageType._(20); - static const threadStarterMessage = MessageType._(21); - static const guildInviteReminder = MessageType._(22); - static const contextMenuCommand = MessageType._(23); - static const autoModerationAction = MessageType._(24); - static const roleSubscriptionPurchase = MessageType._(25); - static const interactionPremiumUpsell = MessageType._(26); - static const stageStart = MessageType._(27); - static const stageEnd = MessageType._(28); - static const stageSpeaker = MessageType._(29); - static const stageTopic = MessageType._(31); - static const guildApplicationPremiumSubscription = MessageType._(32); - static const guildIncidentAlertModeEnabled = MessageType._(36); - static const guildIncidentAlertModeDisabled = MessageType._(37); - static const guildIncidentReportRaid = MessageType._(38); - static const guildIncidentReportFalseAlarm = MessageType._(39); - - static const List values = [ - normal, - recipientAdd, - recipientRemove, - call, - channelNameChange, - channelIconChange, - channelPinnedMessage, - userJoin, - guildBoost, - guildBoostTier1, - guildBoostTier2, - guildBoostTier3, - channelFollowAdd, - guildDiscoveryDisqualified, - guildDiscoveryRequalified, - guildDiscoveryGracePeriodInitialWarning, - guildDiscoveryGracePeriodFinalWarning, - threadCreated, - reply, - chatInputCommand, - threadStarterMessage, - guildInviteReminder, - contextMenuCommand, - autoModerationAction, - roleSubscriptionPurchase, - interactionPremiumUpsell, - stageStart, - stageEnd, - stageSpeaker, - stageTopic, - guildApplicationPremiumSubscription, - guildIncidentAlertModeEnabled, - guildIncidentAlertModeDisabled, - guildIncidentReportRaid, - guildIncidentReportFalseAlarm, - ]; - - const MessageType._(super.value); - - factory MessageType.parse(int value) => parseEnum(values, value); +final class MessageType extends EnumLike { + static const normal = MessageType(0); + static const recipientAdd = MessageType(1); + static const recipientRemove = MessageType(2); + static const call = MessageType(3); + static const channelNameChange = MessageType(4); + static const channelIconChange = MessageType(5); + static const channelPinnedMessage = MessageType(6); + static const userJoin = MessageType(7); + static const guildBoost = MessageType(8); + static const guildBoostTier1 = MessageType(9); + static const guildBoostTier2 = MessageType(10); + static const guildBoostTier3 = MessageType(11); + static const channelFollowAdd = MessageType(12); + static const guildDiscoveryDisqualified = MessageType(14); + static const guildDiscoveryRequalified = MessageType(15); + static const guildDiscoveryGracePeriodInitialWarning = MessageType(16); + static const guildDiscoveryGracePeriodFinalWarning = MessageType(17); + static const threadCreated = MessageType(18); + static const reply = MessageType(19); + static const chatInputCommand = MessageType(20); + static const threadStarterMessage = MessageType(21); + static const guildInviteReminder = MessageType(22); + static const contextMenuCommand = MessageType(23); + static const autoModerationAction = MessageType(24); + static const roleSubscriptionPurchase = MessageType(25); + static const interactionPremiumUpsell = MessageType(26); + static const stageStart = MessageType(27); + static const stageEnd = MessageType(28); + static const stageSpeaker = MessageType(29); + static const stageTopic = MessageType(31); + static const guildApplicationPremiumSubscription = MessageType(32); + static const guildIncidentAlertModeEnabled = MessageType(36); + static const guildIncidentAlertModeDisabled = MessageType(37); + static const guildIncidentReportRaid = MessageType(38); + static const guildIncidentReportFalseAlarm = MessageType(39); + + /// @nodoc + const MessageType(super.value); + + MessageType.parse(int value) : this(value); } /// Flags that can be applied to a [Message]. diff --git a/lib/src/models/message/poll.dart b/lib/src/models/message/poll.dart index 40b2debc2..a421737a6 100644 --- a/lib/src/models/message/poll.dart +++ b/lib/src/models/message/poll.dart @@ -6,15 +6,14 @@ import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart'; /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/poll#layout-type -final class PollLayoutType extends EnumLike { +final class PollLayoutType extends EnumLike { /// The default layout type. - static const defaultLayout = PollLayoutType._(1); + static const defaultLayout = PollLayoutType(1); - static const List values = [defaultLayout]; - - const PollLayoutType._(super.value); + /// @nodoc + const PollLayoutType(super.value); - factory PollLayoutType.parse(int value) => parseEnum(values, value); + PollLayoutType.parse(int value) : this(value); } /// {@template poll_media} diff --git a/lib/src/models/permission_overwrite.dart b/lib/src/models/permission_overwrite.dart index d163bd853..af1b378b1 100644 --- a/lib/src/models/permission_overwrite.dart +++ b/lib/src/models/permission_overwrite.dart @@ -43,16 +43,15 @@ class PermissionOverwrite with ToStringHelper { } /// The type of a permission overwrite. -final class PermissionOverwriteType extends EnumLike { +final class PermissionOverwriteType extends EnumLike { /// The overwrite applies to a [Role]'s permissions. - static const role = PermissionOverwriteType._(0); + static const role = PermissionOverwriteType(0); /// The overwrite applies to a [Member]'s permissions. - static const member = PermissionOverwriteType._(1); + static const member = PermissionOverwriteType(1); - static const values = [role, member]; - - const PermissionOverwriteType._(super.value); + /// @nodoc + const PermissionOverwriteType(super.value); - factory PermissionOverwriteType.parse(int value) => parseEnum(values, value); + PermissionOverwriteType.parse(int value) : this(value); } diff --git a/lib/src/models/presence.dart b/lib/src/models/presence.dart index 892e97be2..8b3a55d3c 100644 --- a/lib/src/models/presence.dart +++ b/lib/src/models/presence.dart @@ -23,17 +23,16 @@ class ClientStatus with ToStringHelper { } /// The status of a client. -final class UserStatus extends EnumLike { - static const online = UserStatus._('online'); - static const dnd = UserStatus._('dnd'); - static const idle = UserStatus._('idle'); - static const offline = UserStatus._('offline'); +final class UserStatus extends EnumLike { + static const online = UserStatus('online'); + static const dnd = UserStatus('dnd'); + static const idle = UserStatus('idle'); + static const offline = UserStatus('offline'); - static const values = [online, dnd, idle, offline]; - - const UserStatus._(super.value); + /// @nodoc + const UserStatus(super.value); - factory UserStatus.parse(String value) => parseEnum(values, value); + UserStatus.parse(String value) : this(value); } /// {@template activity} @@ -107,19 +106,17 @@ class Activity with ToStringHelper { } /// The type of an activity. -final class ActivityType extends EnumLike { - static const game = ActivityType._(0); - static const streaming = ActivityType._(1); - static const listening = ActivityType._(2); - static const watching = ActivityType._(3); - static const custom = ActivityType._(4); - static const competing = ActivityType._(5); - - static const values = [game, streaming, listening, watching, custom, competing]; +final class ActivityType extends EnumLike { + static const game = ActivityType(0); + static const streaming = ActivityType(1); + static const listening = ActivityType(2); + static const watching = ActivityType(3); + static const custom = ActivityType(4); + static const competing = ActivityType(5); - const ActivityType._(super.value); + const ActivityType(super.value); - factory ActivityType.parse(int value) => parseEnum(values, value); + ActivityType.parse(int value) : this(value); } /// {@template activity_timestamps} diff --git a/lib/src/models/sku.dart b/lib/src/models/sku.dart index dd712adcd..798ad1b5b 100644 --- a/lib/src/models/sku.dart +++ b/lib/src/models/sku.dart @@ -47,29 +47,23 @@ class Sku with ToStringHelper { } /// The type of an [Sku]. -final class SkuType extends EnumLike { +final class SkuType extends EnumLike { /// Durable one-time purchase. - static const SkuType durable = SkuType._(2); + static const durable = SkuType(2); /// Consumable one-time purchase. - static const SkuType consumable = SkuType._(3); + static const consumable = SkuType(3); /// Subscription. - static const SkuType subscription = SkuType._(5); + static const subscription = SkuType(5); /// Subscription group. - static const SkuType subscriptionGroup = SkuType._(6); + static const subscriptionGroup = SkuType(6); - static const List values = [ - durable, - consumable, - subscription, - subscriptionGroup, - ]; - - const SkuType._(super.value); + /// @nodoc + const SkuType(super.value); - factory SkuType.parse(int value) => parseEnum(values, value); + SkuType.parse(int value) : this(value); } /// Flags applied to an [Sku]. diff --git a/lib/src/models/sticker/sticker.dart b/lib/src/models/sticker/sticker.dart index 450c5fc21..ddb64380d 100644 --- a/lib/src/models/sticker/sticker.dart +++ b/lib/src/models/sticker/sticker.dart @@ -2,28 +2,26 @@ import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart'; import 'package:nyxx/src/models/user/user.dart'; import 'package:nyxx/src/utils/enum_like.dart'; -final class StickerType extends EnumLike { - static const standard = StickerType._(1); - static const guild = StickerType._(2); +final class StickerType extends EnumLike { + static const standard = StickerType(1); + static const guild = StickerType(2); - static const List values = [standard, guild]; - - const StickerType._(super.value); + /// @nodoc + const StickerType(super.value); - factory StickerType.parse(int value) => parseEnum(values, value); + StickerType.parse(int value) : this(value); } -final class StickerFormatType extends EnumLike { - static const png = StickerFormatType._(1); - static const apng = StickerFormatType._(2); - static const lottie = StickerFormatType._(3); - static const gif = StickerFormatType._(4); +final class StickerFormatType extends EnumLike { + static const png = StickerFormatType(1); + static const apng = StickerFormatType(2); + static const lottie = StickerFormatType(3); + static const gif = StickerFormatType(4); - static const List values = [png, apng, lottie, gif]; - - const StickerFormatType._(super.value); + /// @nodoc + const StickerFormatType(super.value); - factory StickerFormatType.parse(int value) => parseEnum(values, value); + StickerFormatType.parse(int value) : this(value); } /// Mixin with shared properties with stickers diff --git a/lib/src/models/team.dart b/lib/src/models/team.dart index 98e100d9a..ac434cb6c 100644 --- a/lib/src/models/team.dart +++ b/lib/src/models/team.dart @@ -82,29 +82,26 @@ class TeamMember with ToStringHelper { } /// The status of a member in a [Team]. -final class TeamMembershipState extends EnumLike { +final class TeamMembershipState extends EnumLike { /// The user has been invited to the team. - static const TeamMembershipState invited = TeamMembershipState._(1); + static const invited = TeamMembershipState(1); /// The user has accepted the invitation to the team. - static const TeamMembershipState accepted = TeamMembershipState._(2); + static const accepted = TeamMembershipState(2); - static const List values = [invited, accepted]; - - const TeamMembershipState._(super.value); + /// @nodoc + const TeamMembershipState(super.value); - factory TeamMembershipState.parse(int value) => parseEnum(values, value); + TeamMembershipState.parse(int value) : this(value); } /// The role of a [TeamMember]. -final class TeamMemberRole extends EnumLike { - static const admin = TeamMemberRole._('admin'); - static const developer = TeamMemberRole._('developer'); - static const readOnly = TeamMemberRole._('read_only'); - - static const List values = [admin, developer, readOnly]; +final class TeamMemberRole extends EnumLike { + static const admin = TeamMemberRole('admin'); + static const developer = TeamMemberRole('developer'); + static const readOnly = TeamMemberRole('read_only'); - const TeamMemberRole._(super.value); + const TeamMemberRole(super.value); - factory TeamMemberRole.parse(String value) => parseEnum(values, value); + TeamMemberRole.parse(String value) : this(value); } diff --git a/lib/src/models/user/connection.dart b/lib/src/models/user/connection.dart index f207b34bc..53f14f914 100644 --- a/lib/src/models/user/connection.dart +++ b/lib/src/models/user/connection.dart @@ -57,77 +57,57 @@ class Connection with ToStringHelper { /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/user#connection-object-services -final class ConnectionType extends EnumLike { - static const battleNet = ConnectionType._('battlenet', 'Battle.net'); - static const bungieNet = ConnectionType._('bungie', 'Bungie.net'); - static const domain = ConnectionType._('domain', 'Domain'); - static const ebay = ConnectionType._('ebay', 'eBay'); - static const epicGames = ConnectionType._('epicgames', 'Epic Games'); - static const facebook = ConnectionType._('facebook', 'Facebook'); - static const github = ConnectionType._('github', 'GitHub'); - static const instagram = ConnectionType._('instagram', 'Instagram'); - static const leagueOfLegends = ConnectionType._('leagueoflegends', 'League of Legends'); - static const paypal = ConnectionType._('paypal', 'PayPal'); - static const playstation = ConnectionType._('playstation', 'PlayStation Network'); - static const reddit = ConnectionType._('reddit', 'Reddit'); - static const riotGames = ConnectionType._('riotgames', 'Riot Games'); - static const roblox = ConnectionType._('roblox', 'ROBLOX'); - static const spotify = ConnectionType._('spotify', 'Spotify'); - static const skype = ConnectionType._('skype', 'Skype'); - static const steam = ConnectionType._('steam', 'Steam'); - static const tikTok = ConnectionType._('tiktok', 'TikTok'); - static const twitch = ConnectionType._('twitch', 'Twitch'); - static const twitter = ConnectionType._('twitter', 'Twitter'); - static const xbox = ConnectionType._('xbox', 'Xbox'); - static const youtube = ConnectionType._('youtube', 'YouTube'); - - static const List values = [ - battleNet, - bungieNet, - domain, - ebay, - epicGames, - facebook, - github, - instagram, - leagueOfLegends, - paypal, - playstation, - reddit, - riotGames, - roblox, - spotify, - skype, - steam, - tikTok, - twitch, - twitter, - xbox, - youtube, - ]; +enum ConnectionType { + battleNet._('battlenet', 'Battle.net'), + bungieNet._('bungie', 'Bungie.net'), + domain._('domain', 'Domain'), + ebay._('ebay', 'eBay'), + epicGames._('epicgames', 'Epic Games'), + facebook._('facebook', 'Facebook'), + github._('github', 'GitHub'), + instagram._('instagram', 'Instagram'), + leagueOfLegends._('leagueoflegends', 'League of Legends'), + paypal._('paypal', 'PayPal'), + playstation._('playstation', 'PlayStation Network'), + reddit._('reddit', 'Reddit'), + riotGames._('riotgames', 'Riot Games'), + roblox._('roblox', 'ROBLOX'), + spotify._('spotify', 'Spotify'), + skype._('skype', 'Skype'), + steam._('steam', 'Steam'), + tikTok._('tiktok', 'TikTok'), + twitch._('twitch', 'Twitch'), + twitter._('twitter', 'Twitter'), + xbox._('xbox', 'Xbox'), + youtube._('youtube', 'YouTube'); + + /// The value of this connection type. + final String value; /// A human-readable name for this connection type. final String name; - const ConnectionType._(super.value, this.name); + const ConnectionType._(this.value, this.name); /// Parse a string to a [ConnectionType]. /// /// The [value] must be a string containing a valid [ConnectionType.value]. - factory ConnectionType.parse(String value) => parseEnum(values, value); + factory ConnectionType.parse(String value) => values.firstWhere( + (type) => type.value == value, + orElse: () => throw FormatException('Unknown ConnectionType', value), + ); } /// The visibility level of a connection. /// /// External references: /// * Discord API Reference: https://discord.com/developers/docs/resources/user#connection-object-visibility-types -final class ConnectionVisibility extends EnumLike { - static const none = ConnectionVisibility._(0); - static const everyone = ConnectionVisibility._(1); +final class ConnectionVisibility extends EnumLike { + static const none = ConnectionVisibility(0); + static const everyone = ConnectionVisibility(1); - static const List values = [none, everyone]; - - const ConnectionVisibility._(super.value); + /// @nodoc + const ConnectionVisibility(super.value); - factory ConnectionVisibility.parse(int value) => parseEnum(values, value); + ConnectionVisibility.parse(int value) : this(value); } diff --git a/lib/src/models/user/user.dart b/lib/src/models/user/user.dart index de5988031..3ce2f014f 100644 --- a/lib/src/models/user/user.dart +++ b/lib/src/models/user/user.dart @@ -228,15 +228,14 @@ class UserFlags extends Flags { } /// The types of Discord Nitro subscription a user can have. -final class NitroType extends EnumLike { - static const none = NitroType._(0); - static const classic = NitroType._(1); - static const nitro = NitroType._(2); - static const basic = NitroType._(3); +final class NitroType extends EnumLike { + static const none = NitroType(0); + static const classic = NitroType(1); + static const nitro = NitroType(2); + static const basic = NitroType(3); - static const List values = [none, classic, nitro, basic]; - - const NitroType._(super.value); + /// @nodoc + const NitroType(super.value); - factory NitroType.parse(int value) => parseEnum(values, value); + NitroType.parse(int value) : this(value); } diff --git a/lib/src/models/webhook.dart b/lib/src/models/webhook.dart index d8677dda0..3b8c59811 100644 --- a/lib/src/models/webhook.dart +++ b/lib/src/models/webhook.dart @@ -174,19 +174,18 @@ class Webhook extends PartialWebhook { } /// The type of a [Webhook]. -final class WebhookType extends EnumLike { +final class WebhookType extends EnumLike { /// A webhook which sends messages to a channel using a [Webhook.token]. - static const WebhookType incoming = WebhookType._(1); + static const incoming = WebhookType(1); /// An internal webhook used to manage Channel Followers. - static const WebhookType channelFollower = WebhookType._(2); + static const channelFollower = WebhookType(2); /// A webhook for use with interactions. - static const WebhookType application = WebhookType._(3); + static const application = WebhookType(3); - static const List values = [incoming, channelFollower, application]; - - const WebhookType._(super.value); + /// @nodoc + const WebhookType(super.value); - factory WebhookType.parse(int value) => parseEnum(values, value); + WebhookType.parse(int value) : this(value); } diff --git a/lib/src/utils/enum_like.dart b/lib/src/utils/enum_like.dart index af33d9344..f16c7b45b 100644 --- a/lib/src/utils/enum_like.dart +++ b/lib/src/utils/enum_like.dart @@ -1,12 +1,9 @@ -base class EnumLike { +base class EnumLike> { /// The value this enum-like holds. final T value; - /// Whether this enum-like is unknown. - final bool isUnknown; - - //@nodoc - const EnumLike(this.value, [this.isUnknown = false]); + /// @nodoc + const EnumLike(this.value); @override String toString() => '$runtimeType($value)'; @@ -15,10 +12,5 @@ base class EnumLike { int get hashCode => value.hashCode; @override - bool operator ==(Object? other) => (other is EnumLike && other.value == value) || (other is T && other == value); -} - -R parseEnum>(List> values, T value) { - final enumValue = values.firstWhere((enumValue) => enumValue.value == value, orElse: () => EnumLike(value, true)); - return enumValue as R; + bool operator ==(Object other) => identical(this, other) || (other is U && other.value == value); } From 24f4916835f6dd57bc7558454d6eda52755a6e78 Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Fri, 14 Jun 2024 16:41:31 +0200 Subject: [PATCH 5/7] fix: covariant `.==` --- lib/src/utils/enum_like.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/utils/enum_like.dart b/lib/src/utils/enum_like.dart index f16c7b45b..1f941c025 100644 --- a/lib/src/utils/enum_like.dart +++ b/lib/src/utils/enum_like.dart @@ -12,5 +12,5 @@ base class EnumLike> { int get hashCode => value.hashCode; @override - bool operator ==(Object other) => identical(this, other) || (other is U && other.value == value); + bool operator ==(covariant EnumLike other) => identical(this, other) || (other is U && other.value == value); } From 7b8ef85ce71139c66206a9cd3ab70e5bd1ac84c8 Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Mon, 24 Jun 2024 14:43:25 +0200 Subject: [PATCH 6/7] apply suggestions Co-authored-by: Abitofevrything --- lib/src/gateway/gateway.dart | 4 ++-- .../managers/application_command_manager.dart | 12 +++++----- .../http/managers/application_manager.dart | 8 +++---- lib/src/http/managers/audit_log_manager.dart | 4 ++-- .../managers/auto_moderation_manager.dart | 8 +++---- lib/src/http/managers/channel_manager.dart | 16 ++++++------- .../http/managers/entitlement_manager.dart | 2 +- lib/src/http/managers/gateway_manager.dart | 8 +++---- lib/src/http/managers/guild_manager.dart | 16 ++++++------- .../http/managers/integration_manager.dart | 2 +- .../http/managers/interaction_manager.dart | 12 +++++----- lib/src/http/managers/invite_manager.dart | 2 +- lib/src/http/managers/message_manager.dart | 24 +++++++++---------- .../managers/scheduled_event_manager.dart | 6 ++--- lib/src/http/managers/sticker_manager.dart | 6 ++--- lib/src/http/managers/user_manager.dart | 4 ++-- lib/src/http/managers/webhook_manager.dart | 2 +- lib/src/models/application.dart | 2 ++ lib/src/models/channel/channel.dart | 1 + lib/src/models/channel/stage_instance.dart | 1 + lib/src/models/channel/types/forum.dart | 2 ++ lib/src/models/channel/voice_channel.dart | 1 + .../models/commands/application_command.dart | 1 + .../commands/application_command_option.dart | 1 + .../application_command_permissions.dart | 1 + lib/src/models/entitlement.dart | 1 + lib/src/models/guild/audit_log.dart | 1 + lib/src/models/guild/auto_moderation.dart | 4 ++++ lib/src/models/guild/guild.dart | 6 +++++ lib/src/models/guild/integration.dart | 1 + lib/src/models/guild/onboarding.dart | 1 + lib/src/models/guild/scheduled_event.dart | 2 ++ lib/src/models/interaction.dart | 2 ++ lib/src/models/invite/invite.dart | 1 + lib/src/models/message/activity.dart | 1 + lib/src/models/message/component.dart | 4 ++++ lib/src/models/message/message.dart | 1 + lib/src/models/message/poll.dart | 1 + lib/src/models/permission_overwrite.dart | 1 + lib/src/models/presence.dart | 2 ++ lib/src/models/sku.dart | 1 + lib/src/models/sticker/sticker.dart | 1 + lib/src/models/team.dart | 1 + lib/src/models/user/connection.dart | 1 + lib/src/models/user/user.dart | 1 + lib/src/models/webhook.dart | 1 + lib/src/utils/enum_like.dart | 2 +- 47 files changed, 114 insertions(+), 69 deletions(-) diff --git a/lib/src/gateway/gateway.dart b/lib/src/gateway/gateway.dart index 657dfb7c4..226a8ca0c 100644 --- a/lib/src/gateway/gateway.dart +++ b/lib/src/gateway/gateway.dart @@ -382,7 +382,7 @@ class Gateway extends GatewayManager with EventParser { guildId: guildId, action: client.guilds[guildId].autoModerationRules.parseAutoModerationAction(raw['action'] as Map), ruleId: Snowflake.parse(raw['rule_id']!), - triggerType: TriggerType.parse(raw['rule_trigger_type'] as int), + triggerType: TriggerType(raw['rule_trigger_type'] as int), userId: Snowflake.parse(raw['user_id']!), channelId: maybeParse(raw['channel_id'], Snowflake.parse), messageId: maybeParse(raw['message_id'], Snowflake.parse), @@ -951,7 +951,7 @@ class Gateway extends GatewayManager with EventParser { (Map raw) => PartialUser(id: Snowflake.parse(raw['id']!), manager: client.users), ), guildId: maybeParse(raw['guild_id'], Snowflake.parse), - status: maybeParse(raw['status'], UserStatus.parse), + status: maybeParse(raw['status'], UserStatus.new), activities: maybeParseMany(raw['activities'], parseActivity), clientStatus: maybeParse(raw['client_status'], parseClientStatus), ); diff --git a/lib/src/http/managers/application_command_manager.dart b/lib/src/http/managers/application_command_manager.dart index 2b9013e0a..e79a70d7c 100644 --- a/lib/src/http/managers/application_command_manager.dart +++ b/lib/src/http/managers/application_command_manager.dart @@ -38,7 +38,7 @@ abstract class ApplicationCommandManager extends Manager { return ApplicationCommand( id: Snowflake.parse(raw['id']!), manager: this, - type: ApplicationCommandType.parse(raw['type'] as int? ?? 1), + type: ApplicationCommandType(raw['type'] as int? ?? 1), applicationId: Snowflake.parse(raw['application_id']!), guildId: maybeParse(raw['guild_id'], Snowflake.parse), name: raw['name'] as String, @@ -59,8 +59,8 @@ abstract class ApplicationCommandManager extends Manager { defaultMemberPermissions: maybeParse(raw['default_member_permissions'], (String raw) => Permissions(int.parse(raw))), hasDmPermission: raw['dm_permission'] as bool?, isNsfw: raw['nsfw'] as bool?, - integrationTypes: maybeParseMany(raw['integration_types'], ApplicationIntegrationType.parse) ?? [ApplicationIntegrationType.guildInstall], - contexts: maybeParseMany(raw['contexts'], InteractionContextType.parse), + integrationTypes: maybeParseMany(raw['integration_types'], ApplicationIntegrationType.new) ?? [ApplicationIntegrationType.guildInstall], + contexts: maybeParseMany(raw['contexts'], InteractionContextType.new), version: Snowflake.parse(raw['version']!), ); } @@ -68,7 +68,7 @@ abstract class ApplicationCommandManager extends Manager { /// Parse a [CommandOption] from [raw]. CommandOption parseApplicationCommandOption(Map raw) { return CommandOption( - type: CommandOptionType.parse(raw['type'] as int), + type: CommandOptionType(raw['type'] as int), name: raw['name'] as String, nameLocalizations: maybeParse( raw['name_localizations'], @@ -86,7 +86,7 @@ abstract class ApplicationCommandManager extends Manager { isRequired: raw['required'] as bool?, choices: maybeParseMany(raw['choices'], parseOptionChoice), options: maybeParseMany(raw['options'], parseApplicationCommandOption), - channelTypes: maybeParseMany(raw['channel_types'], ChannelType.parse), + channelTypes: maybeParseMany(raw['channel_types'], ChannelType.new), minValue: raw['min_value'] as num?, maxValue: raw['max_value'] as num?, minLength: raw['min_length'] as int?, @@ -234,7 +234,7 @@ class GuildApplicationCommandManager extends ApplicationCommandManager { CommandPermission parseCommandPermission(Map raw) { return CommandPermission( id: Snowflake.parse(raw['id']!), - type: CommandPermissionType.parse(raw['type'] as int), + type: CommandPermissionType(raw['type'] as int), hasPermission: raw['permission'] as bool, ); } diff --git a/lib/src/http/managers/application_manager.dart b/lib/src/http/managers/application_manager.dart index f2e373178..018fe87ff 100644 --- a/lib/src/http/managers/application_manager.dart +++ b/lib/src/http/managers/application_manager.dart @@ -65,7 +65,7 @@ class ApplicationManager { raw['integration_types_config'], (Map config) => { for (final MapEntry(:key, :value) in config.entries) - ApplicationIntegrationType.parse(int.parse(key)): parseApplicationIntegrationTypeConfiguration(value as Map) + ApplicationIntegrationType(int.parse(key)): parseApplicationIntegrationTypeConfiguration(value as Map) }, ), roleConnectionsVerificationUrl: maybeParse(raw['role_connections_verification_url'], Uri.parse), @@ -87,7 +87,7 @@ class ApplicationManager { /// Parse a [TeamMember] from [raw]. TeamMember parseTeamMember(Map raw) { return TeamMember( - membershipState: TeamMembershipState.parse(raw['membership_state'] as int), + membershipState: TeamMembershipState(raw['membership_state'] as int), teamId: Snowflake.parse(raw['team_id']!), user: PartialUser(id: Snowflake.parse((raw['user'] as Map)['id']!), manager: client.users), role: TeamMemberRole.parse(raw['role'] as String), @@ -112,7 +112,7 @@ class ApplicationManager { /// Parse a [ApplicationRoleConnectionMetadata] from [raw]. ApplicationRoleConnectionMetadata parseApplicationRoleConnectionMetadata(Map raw) { return ApplicationRoleConnectionMetadata( - type: ConnectionMetadataType.parse(raw['type'] as int), + type: ConnectionMetadataType(raw['type'] as int), key: raw['key'] as String, name: raw['name'] as String, localizedNames: maybeParse( @@ -132,7 +132,7 @@ class ApplicationManager { return Sku( manager: this, id: Snowflake.parse(raw['id']!), - type: SkuType.parse(raw['type'] as int), + type: SkuType(raw['type'] as int), applicationId: Snowflake.parse(raw['application_id']!), name: raw['name'] as String, slug: raw['slug'] as String, diff --git a/lib/src/http/managers/audit_log_manager.dart b/lib/src/http/managers/audit_log_manager.dart index 39cf5faab..ae8d533c2 100644 --- a/lib/src/http/managers/audit_log_manager.dart +++ b/lib/src/http/managers/audit_log_manager.dart @@ -24,7 +24,7 @@ class AuditLogManager extends ReadOnlyManager { targetId: maybeParse(raw['target_id'], Snowflake.parse), changes: maybeParseMany(raw['changes'], parseAuditLogChange), userId: maybeParse(raw['user_id'], Snowflake.parse), - actionType: AuditLogEvent.parse(raw['action_type'] as int), + actionType: AuditLogEvent(raw['action_type'] as int), options: maybeParse(raw['options'], parseAuditLogEntryInfo), reason: raw['reason'] as String?, ); @@ -53,7 +53,7 @@ class AuditLogManager extends ReadOnlyManager { membersRemoved: raw['members_removed'] as String?, messageId: maybeParse(raw['message_id'], Snowflake.parse), roleName: raw['role_name'] as String?, - overwriteType: maybeParse(raw['type'], (String raw) => PermissionOverwriteType.parse(int.parse(raw))), + overwriteType: maybeParse(raw['type'], (String raw) => PermissionOverwriteType(int.parse(raw))), integrationType: raw['integration_type'] as String?, ); } diff --git a/lib/src/http/managers/auto_moderation_manager.dart b/lib/src/http/managers/auto_moderation_manager.dart index 58f9acc89..1e074b9d2 100644 --- a/lib/src/http/managers/auto_moderation_manager.dart +++ b/lib/src/http/managers/auto_moderation_manager.dart @@ -25,8 +25,8 @@ class AutoModerationManager extends Manager { guildId: Snowflake.parse(raw['guild_id']!), name: raw['name'] as String, creatorId: Snowflake.parse(raw['creator_id']!), - eventType: AutoModerationEventType.parse(raw['event_type'] as int), - triggerType: TriggerType.parse(raw['trigger_type'] as int), + eventType: AutoModerationEventType(raw['event_type'] as int), + triggerType: TriggerType(raw['trigger_type'] as int), metadata: parseTriggerMetadata(raw['trigger_metadata'] as Map), actions: parseMany(raw['actions'] as List, parseAutoModerationAction), isEnabled: raw['enabled'] as bool, @@ -40,7 +40,7 @@ class AutoModerationManager extends Manager { return TriggerMetadata( keywordFilter: maybeParseMany(raw['keyword_filter']), regexPatterns: maybeParseMany(raw['regex_patterns']), - presets: maybeParseMany(raw['presets'], KeywordPresetType.parse), + presets: maybeParseMany(raw['presets'], KeywordPresetType.new), allowList: maybeParseMany(raw['allow_list']), mentionTotalLimit: raw['mention_total_limit'] as int?, isMentionRaidProtectionEnabled: raw['mention_raid_protection_enabled'] as bool?, @@ -50,7 +50,7 @@ class AutoModerationManager extends Manager { /// Parse a [AutoModerationAction] from [raw]. AutoModerationAction parseAutoModerationAction(Map raw) { return AutoModerationAction( - type: ActionType.parse(raw['type'] as int), + type: ActionType(raw['type'] as int), metadata: maybeParse(raw['metadata'], parseActionMetadata), ); } diff --git a/lib/src/http/managers/channel_manager.dart b/lib/src/http/managers/channel_manager.dart index f1bdff46e..79a327ac2 100644 --- a/lib/src/http/managers/channel_manager.dart +++ b/lib/src/http/managers/channel_manager.dart @@ -64,7 +64,7 @@ class ChannelManager extends ReadOnlyManager { @override Channel parse(Map raw, {Snowflake? guildId}) { - final type = ChannelType.parse(raw['type'] as int); + final type = ChannelType(raw['type'] as int); final parsers = { ChannelType.guildText: parseGuildTextChannel, @@ -139,7 +139,7 @@ class ChannelManager extends ReadOnlyManager { rateLimitPerUser: maybeParse(raw['rate_limit_per_user'], (value) => value == 0 ? null : Duration(seconds: value)), rtcRegion: raw['rtc_region'] as String?, userLimit: raw['user_limit'] == 0 ? null : raw['user_limit'] as int?, - videoQualityMode: maybeParse(raw['video_quality_mode'], VideoQualityMode.parse) ?? VideoQualityMode.auto, + videoQualityMode: maybeParse(raw['video_quality_mode'], VideoQualityMode.new) ?? VideoQualityMode.auto, ); } @@ -305,7 +305,7 @@ class ChannelManager extends ReadOnlyManager { rateLimitPerUser: maybeParse(raw['rate_limit_per_user'], (value) => value == 0 ? null : Duration(seconds: value)), rtcRegion: raw['rtc_region'] as String?, userLimit: raw['user_limit'] == 0 ? null : raw['user_limit'] as int?, - videoQualityMode: maybeParse(raw['video_quality_mode'], VideoQualityMode.parse) ?? VideoQualityMode.auto, + videoQualityMode: maybeParse(raw['video_quality_mode'], VideoQualityMode.new) ?? VideoQualityMode.auto, ); } @@ -324,7 +324,7 @@ class ChannelManager extends ReadOnlyManager { return ForumChannel( id: Snowflake.parse(raw['id']!), manager: this, - defaultLayout: maybeParse(raw['default_forum_layout'], ForumLayout.parse), + defaultLayout: maybeParse(raw['default_forum_layout'], ForumLayout.new), topic: raw['topic'] as String?, rateLimitPerUser: maybeParse(raw['rate_limit_per_user'], (value) => value == 0 ? null : Duration(seconds: value)), lastThreadId: maybeParse(raw['last_message_id'], Snowflake.parse), @@ -332,7 +332,7 @@ class ChannelManager extends ReadOnlyManager { flags: ChannelFlags(raw['flags'] as int), availableTags: parseMany(raw['available_tags'] as List, parseForumTag), defaultReaction: maybeParse(raw['default_reaction_emoji'], parseDefaultReaction), - defaultSortOrder: maybeParse(raw['default_sort_order'], ForumSort.parse), + defaultSortOrder: maybeParse(raw['default_sort_order'], ForumSort.new), // Discord doesn't seem to include this field if the default 3 day expiration is used (3 days = 4320 minutes) defaultAutoArchiveDuration: Duration(minutes: raw['default_auto_archive_duration'] as int? ?? 4320), defaultThreadRateLimitPerUser: @@ -359,7 +359,7 @@ class ChannelManager extends ReadOnlyManager { flags: ChannelFlags(raw['flags'] as int), availableTags: parseMany(raw['available_tags'] as List, parseForumTag), defaultReaction: maybeParse(raw['default_reaction_emoji'], parseDefaultReaction), - defaultSortOrder: maybeParse(raw['default_sort_order'], ForumSort.parse), + defaultSortOrder: maybeParse(raw['default_sort_order'], ForumSort.new), // Discord doesn't seem to include this field if the default 3 day expiration is used (3 days = 4320 minutes) defaultAutoArchiveDuration: Duration(minutes: raw['default_auto_archive_duration'] as int? ?? 4320), defaultThreadRateLimitPerUser: @@ -376,7 +376,7 @@ class ChannelManager extends ReadOnlyManager { PermissionOverwrite parsePermissionOverwrite(Map raw) { return PermissionOverwrite( id: Snowflake.parse(raw['id']!), - type: PermissionOverwriteType.parse(raw['type'] as int), + type: PermissionOverwriteType(raw['type'] as int), allow: Permissions(int.parse(raw['allow'] as String)), deny: Permissions(int.parse(raw['deny'] as String)), ); @@ -436,7 +436,7 @@ class ChannelManager extends ReadOnlyManager { guildId: Snowflake.parse(raw['guild_id']!), channelId: Snowflake.parse(raw['channel_id']!), topic: raw['topic'] as String, - privacyLevel: PrivacyLevel.parse(raw['privacy_level'] as int), + privacyLevel: PrivacyLevel(raw['privacy_level'] as int), scheduledEventId: maybeParse(raw['guild_scheduled_event_id'], Snowflake.parse), ); } diff --git a/lib/src/http/managers/entitlement_manager.dart b/lib/src/http/managers/entitlement_manager.dart index 9eb211884..44b6c4ac4 100644 --- a/lib/src/http/managers/entitlement_manager.dart +++ b/lib/src/http/managers/entitlement_manager.dart @@ -30,7 +30,7 @@ class EntitlementManager extends ReadOnlyManager { userId: maybeParse(raw['user_id'], Snowflake.parse), guildId: maybeParse(raw['guild_id'], Snowflake.parse), applicationId: Snowflake.parse(raw['application_id']!), - type: EntitlementType.parse(raw['type'] as int), + type: EntitlementType(raw['type'] as int), isConsumed: raw['consumed'] as bool? ?? false, isDeleted: raw['deleted'] as bool? ?? false, startsAt: maybeParse(raw['starts_at'], DateTime.parse), diff --git a/lib/src/http/managers/gateway_manager.dart b/lib/src/http/managers/gateway_manager.dart index 2ee4ad8a1..84e776c1e 100644 --- a/lib/src/http/managers/gateway_manager.dart +++ b/lib/src/http/managers/gateway_manager.dart @@ -45,7 +45,7 @@ abstract class GatewayManager { // No fields are validated server-side. Expect errors. return Activity( name: raw['name'] as String, - type: ActivityType.parse(raw['type'] as int), + type: ActivityType(raw['type'] as int), url: tryParse(raw['url'], Uri.parse), createdAt: tryParse(raw['created_at'], DateTime.fromMillisecondsSinceEpoch), timestamps: tryParse(raw['timestamps'], parseActivityTimestamps), @@ -103,9 +103,9 @@ abstract class GatewayManager { ClientStatus parseClientStatus(Map raw) { return ClientStatus( - desktop: maybeParse(raw['desktop'], UserStatus.parse), - mobile: maybeParse(raw['mobile'], UserStatus.parse), - web: maybeParse(raw['web'], UserStatus.parse), + desktop: maybeParse(raw['desktop'], UserStatus.new), + mobile: maybeParse(raw['mobile'], UserStatus.new), + web: maybeParse(raw['web'], UserStatus.new), ); } diff --git a/lib/src/http/managers/guild_manager.dart b/lib/src/http/managers/guild_manager.dart index 20a75f981..d93aa1b23 100644 --- a/lib/src/http/managers/guild_manager.dart +++ b/lib/src/http/managers/guild_manager.dart @@ -59,12 +59,12 @@ class GuildManager extends Manager { afkTimeout: Duration(seconds: raw['afk_timeout'] as int), isWidgetEnabled: raw['widget_enabled'] as bool? ?? false, widgetChannelId: maybeParse(raw['widget_channel_id'], Snowflake.parse), - verificationLevel: VerificationLevel.parse(raw['verification_level'] as int), - defaultMessageNotificationLevel: MessageNotificationLevel.parse(raw['default_message_notifications'] as int), - explicitContentFilterLevel: ExplicitContentFilterLevel.parse(raw['explicit_content_filter'] as int), + verificationLevel: VerificationLevel(raw['verification_level'] as int), + defaultMessageNotificationLevel: MessageNotificationLevel(raw['default_message_notifications'] as int), + explicitContentFilterLevel: ExplicitContentFilterLevel(raw['explicit_content_filter'] as int), roleList: parseMany(raw['roles'] as List, this[id].roles.parse), features: parseGuildFeatures(raw['features'] as List), - mfaLevel: MfaLevel.parse(raw['mfa_level'] as int), + mfaLevel: MfaLevel(raw['mfa_level'] as int), applicationId: maybeParse(raw['application_id'], Snowflake.parse), systemChannelId: maybeParse(raw['system_channel_id'], Snowflake.parse), systemChannelFlags: SystemChannelFlags(raw['system_channel_flags'] as int), @@ -74,7 +74,7 @@ class GuildManager extends Manager { vanityUrlCode: raw['vanity_url_code'] as String?, description: raw['description'] as String?, bannerHash: raw['banner'] as String?, - premiumTier: PremiumTier.parse(raw['premium_tier'] as int), + premiumTier: PremiumTier(raw['premium_tier'] as int), premiumSubscriptionCount: raw['premium_subscription_count'] as int?, preferredLocale: Locale.parse(raw['preferred_locale'] as String), publicUpdatesChannelId: maybeParse(raw['public_updates_channel_id'], Snowflake.parse), @@ -83,7 +83,7 @@ class GuildManager extends Manager { approximateMemberCount: raw['approximate_member_count'] as int?, approximatePresenceCount: raw['approximate_presence_count'] as int?, welcomeScreen: maybeParse(raw['welcome_screen'], parseWelcomeScreen), - nsfwLevel: NsfwLevel.parse(raw['nsfw_level'] as int), + nsfwLevel: NsfwLevel(raw['nsfw_level'] as int), hasPremiumProgressBarEnabled: raw['premium_progress_bar_enabled'] as bool, emojiList: parseMany(raw['emojis'] as List, this[id].emojis.parse), stickerList: parseMany(raw['stickers'] as List? ?? [], this[id].stickers.parse), @@ -263,7 +263,7 @@ class GuildManager extends Manager { OnboardingPrompt parseOnboardingPrompt(Map raw, {Snowflake? guildId}) { return OnboardingPrompt( id: Snowflake.parse(raw['id']!), - type: OnboardingPromptType.parse(raw['type'] as int), + type: OnboardingPromptType(raw['type'] as int), options: parseMany(raw['options'] as List, (Map raw) => parseOnboardingPromptOption(raw, guildId: guildId)), title: raw['title'] as String, isSingleSelect: raw['single_select'] as bool, @@ -534,7 +534,7 @@ class GuildManager extends Manager { ); final response = await client.httpHandler.executeSafe(request); - return MfaLevel.parse((response.jsonBody as Map)['level'] as int); + return MfaLevel((response.jsonBody as Map)['level'] as int); } /// Fetch the prune count in a guild. diff --git a/lib/src/http/managers/integration_manager.dart b/lib/src/http/managers/integration_manager.dart index f3aacc721..51b3fe69c 100644 --- a/lib/src/http/managers/integration_manager.dart +++ b/lib/src/http/managers/integration_manager.dart @@ -29,7 +29,7 @@ class IntegrationManager extends ReadOnlyManager { isSyncing: raw['syncing'] as bool?, roleId: maybeParse(raw['role_id'], Snowflake.parse), enableEmoticons: raw['enable_emoticons'] as bool?, - expireBehavior: maybeParse(raw['expire_behavior'], IntegrationExpireBehavior.parse), + expireBehavior: maybeParse(raw['expire_behavior'], IntegrationExpireBehavior.new), expireGracePeriod: maybeParse(raw['expire_grace_period'], (int value) => Duration(days: value)), user: maybeParse(raw['user'], client.users.parse), account: parseIntegrationAccount(raw['account'] as Map), diff --git a/lib/src/http/managers/interaction_manager.dart b/lib/src/http/managers/interaction_manager.dart index 3d18afc23..d6ecb89f8 100644 --- a/lib/src/http/managers/interaction_manager.dart +++ b/lib/src/http/managers/interaction_manager.dart @@ -33,7 +33,7 @@ class InteractionManager { InteractionManager(this.client, {required this.applicationId}); Interaction parse(Map raw) { - final type = InteractionType.parse(raw['type'] as int); + final type = InteractionType(raw['type'] as int); final guildId = maybeParse(raw['guild_id'], Snowflake.parse); final channelId = maybeParse(raw['channel_id'], Snowflake.parse); @@ -58,10 +58,10 @@ class InteractionManager { final authorizingIntegrationOwners = maybeParse( raw['authorizing_integration_owners'], (Map map) => { - for (final MapEntry(:key, :value) in map.entries) ApplicationIntegrationType.parse(int.parse(key)): Snowflake.parse(value!), + for (final MapEntry(:key, :value) in map.entries) ApplicationIntegrationType(int.parse(key)): Snowflake.parse(value!), }, ); - final context = maybeParse(raw['context'], InteractionContextType.parse); + final context = maybeParse(raw['context'], InteractionContextType.new); return switch (type) { InteractionType.ping => PingInteraction( @@ -176,7 +176,7 @@ class InteractionManager { return ApplicationCommandInteractionData( id: Snowflake.parse(raw['id']!), name: raw['name'] as String, - type: ApplicationCommandType.parse(raw['type'] as int), + type: ApplicationCommandType(raw['type'] as int), resolved: maybeParse(raw['resolved'], (Map raw) => parseResolvedData(raw, guildId: guildId, channelId: channelId)), options: maybeParseMany(raw['options'], parseInteractionOption), // This guild_id is the ID of the guild the command is registered in, so it may be null even if the command was executed in a guild. @@ -241,7 +241,7 @@ class InteractionManager { InteractionOption parseInteractionOption(Map raw) { return InteractionOption( name: raw['name'] as String, - type: CommandOptionType.parse(raw['type'] as int), + type: CommandOptionType(raw['type'] as int), value: raw['value'], options: maybeParseMany(raw['options'], parseInteractionOption), isFocused: raw['focused'] as bool?, @@ -251,7 +251,7 @@ class InteractionManager { MessageComponentInteractionData parseMessageComponentInteractionData(Map raw, {Snowflake? guildId, Snowflake? channelId}) { return MessageComponentInteractionData( customId: raw['custom_id'] as String, - type: MessageComponentType.parse(raw['component_type'] as int), + type: MessageComponentType(raw['component_type'] as int), values: maybeParseMany(raw['values']), resolved: maybeParse(raw['resolved'], (Map raw) => parseResolvedData(raw, guildId: guildId, channelId: channelId)), ); diff --git a/lib/src/http/managers/invite_manager.dart b/lib/src/http/managers/invite_manager.dart index cc74bc37f..6bfd1cab2 100644 --- a/lib/src/http/managers/invite_manager.dart +++ b/lib/src/http/managers/invite_manager.dart @@ -30,7 +30,7 @@ class InviteManager { guild: guild, channel: PartialChannel(id: Snowflake.parse((raw['channel'] as Map)['id']!), manager: client.channels), inviter: maybeParse(raw['inviter'], client.users.parse), - targetType: maybeParse(raw['target_type'], TargetType.parse), + targetType: maybeParse(raw['target_type'], TargetType.new), targetUser: maybeParse(raw['target_user'], client.users.parse), targetApplication: maybeParse( raw['target_application'], diff --git a/lib/src/http/managers/message_manager.dart b/lib/src/http/managers/message_manager.dart index 54601349f..2b02b1ffe 100644 --- a/lib/src/http/managers/message_manager.dart +++ b/lib/src/http/managers/message_manager.dart @@ -68,7 +68,7 @@ class MessageManager extends Manager { nonce: raw['nonce'] /* as int | String */, isPinned: raw['pinned'] as bool, webhookId: webhookId, - type: MessageType.parse(raw['type'] as int), + type: MessageType(raw['type'] as int), activity: maybeParse(raw['activity'], parseMessageActivity), application: maybeParse( raw['application'], @@ -101,7 +101,7 @@ class MessageManager extends Manager { id: Snowflake.parse(raw['id']!), manager: client.channels, guildId: Snowflake.parse(raw['guild_id']!), - type: ChannelType.parse(raw['type'] as int), + type: ChannelType(raw['type'] as int), name: raw['name'] as String, ); } @@ -221,7 +221,7 @@ class MessageManager extends Manager { MessageActivity parseMessageActivity(Map raw) { return MessageActivity( - type: MessageActivityType.parse(raw['type'] as int), + type: MessageActivityType(raw['type'] as int), partyId: raw['party_id'] as String?, ); } @@ -245,14 +245,14 @@ class MessageManager extends Manager { } MessageComponent parseMessageComponent(Map raw) { - final type = MessageComponentType.parse(raw['type'] as int); + final type = MessageComponentType(raw['type'] as int); return switch (type) { MessageComponentType.actionRow => ActionRowComponent( components: parseMany(raw['components'] as List, parseMessageComponent), ), MessageComponentType.button => ButtonComponent( - style: ButtonStyle.parse(raw['style'] as int), + style: ButtonStyle(raw['style'] as int), label: raw['label'] as String?, emoji: maybeParse(raw['emoji'], client.guilds[Snowflake.zero].emojis.parse), customId: raw['custom_id'] as String?, @@ -261,7 +261,7 @@ class MessageManager extends Manager { ), MessageComponentType.textInput => TextInputComponent( customId: raw['custom_id'] as String, - style: maybeParse(raw['style'], TextInputStyle.parse), + style: maybeParse(raw['style'], TextInputStyle.new), label: raw['label'] as String?, minLength: raw['min_length'] as int?, maxLength: raw['max_length'] as int?, @@ -278,7 +278,7 @@ class MessageManager extends Manager { type: type, customId: raw['custom_id'] as String, options: maybeParseMany(raw['options'], parseSelectMenuOption), - channelTypes: maybeParseMany(raw['channel_types'], ChannelType.parse), + channelTypes: maybeParseMany(raw['channel_types'], ChannelType.new), placeholder: raw['placeholder'] as String?, defaultValues: maybeParseMany(raw['default_values'], parseSelectMenuDefaultValue), minValues: raw['min_values'] as int?, @@ -302,7 +302,7 @@ class MessageManager extends Manager { SelectMenuDefaultValue parseSelectMenuDefaultValue(Map raw) { return SelectMenuDefaultValue( id: Snowflake.parse(raw['id']!), - type: SelectMenuDefaultValueType.parse(raw['type'] as String), + type: SelectMenuDefaultValueType(raw['type'] as String), ); } @@ -313,7 +313,7 @@ class MessageManager extends Manager { // ignore: deprecated_member_use_from_same_package return MessageInteraction( id: Snowflake.parse(raw['id']!), - type: InteractionType.parse(raw['type'] as int), + type: InteractionType(raw['type'] as int), name: raw['name'] as String, user: user, member: maybeParse( @@ -326,11 +326,11 @@ class MessageManager extends Manager { MessageInteractionMetadata parseMessageInteractionMetadata(Map raw) { return MessageInteractionMetadata( id: Snowflake.parse(raw['id']!), - type: InteractionType.parse(raw['type'] as int), + type: InteractionType(raw['type'] as int), userId: Snowflake.parse(raw['user_id']!), authorizingIntegrationOwners: { for (final MapEntry(:key, :value) in (raw['authorizing_integration_owners'] as Map).entries) - ApplicationIntegrationType.parse(int.parse(key)): Snowflake.parse(value!), + ApplicationIntegrationType(int.parse(key)): Snowflake.parse(value!), }, originalResponseMessageId: maybeParse(raw['original_response_message_id'], Snowflake.parse), interactedMessageId: maybeParse(raw['interacted_message_id'], Snowflake.parse), @@ -373,7 +373,7 @@ class MessageManager extends Manager { answers: parseMany(raw['answers'] as List, parsePollAnswer), endsAt: maybeParse(raw['expiry'] as String?, DateTime.parse), allowsMultiselect: raw['allow_multiselect'] as bool, - layoutType: PollLayoutType.parse(raw['layout_type'] as int), + layoutType: PollLayoutType(raw['layout_type'] as int), results: maybeParse(raw['results'], parsePollResults), ); } diff --git a/lib/src/http/managers/scheduled_event_manager.dart b/lib/src/http/managers/scheduled_event_manager.dart index 24fddda85..a0611945a 100644 --- a/lib/src/http/managers/scheduled_event_manager.dart +++ b/lib/src/http/managers/scheduled_event_manager.dart @@ -32,9 +32,9 @@ class ScheduledEventManager extends Manager { description: raw['description'] as String?, scheduledStartTime: DateTime.parse(raw['scheduled_start_time'] as String), scheduledEndTime: maybeParse(raw['scheduled_end_time'], DateTime.parse), - privacyLevel: PrivacyLevel.parse(raw['privacy_level'] as int), - status: EventStatus.parse(raw['status'] as int), - type: ScheduledEntityType.parse(raw['entity_type'] as int), + privacyLevel: PrivacyLevel(raw['privacy_level'] as int), + status: EventStatus(raw['status'] as int), + type: ScheduledEntityType(raw['entity_type'] as int), entityId: maybeParse(raw['entity_id'], Snowflake.parse), metadata: maybeParse(raw['entity_metadata'], parseEntityMetadata), creator: maybeParse(raw['creator'], client.users.parse), diff --git a/lib/src/http/managers/sticker_manager.dart b/lib/src/http/managers/sticker_manager.dart index 3762c46d8..e4dd15c10 100644 --- a/lib/src/http/managers/sticker_manager.dart +++ b/lib/src/http/managers/sticker_manager.dart @@ -30,7 +30,7 @@ class GuildStickerManager extends Manager { description: raw['description'] as String?, tags: raw['tags'] as String, type: StickerType.parse(raw['type'] as int), - formatType: StickerFormatType.parse(raw['format_type'] as int), + formatType: StickerFormatType(raw['format_type'] as int), available: raw['available'] as bool? ?? false, guildId: Snowflake.parse(raw['guild_id']!), user: ((raw['user'] ?? {}) as Map)['id'] != null ? client.users[Snowflake.parse((raw['user'] as Map)['id']!)] : null, @@ -127,7 +127,7 @@ class GlobalStickerManager extends ReadOnlyManager { description: raw['description'] as String?, tags: raw['tags'] as String, type: StickerType.parse(raw['type'] as int), - formatType: StickerFormatType.parse(raw['format_type'] as int), + formatType: StickerFormatType(raw['format_type'] as int), available: raw['available'] as bool? ?? false, user: ((raw['user'] ?? {}) as Map)['id'] != null ? client.users[Snowflake.parse((raw['user'] as Map)['id']!)] : null, sortValue: raw['sort_value'] as int?, @@ -138,7 +138,7 @@ class GlobalStickerManager extends ReadOnlyManager { return StickerItem( id: Snowflake.parse(raw['id']!), name: raw['name'] as String, - formatType: StickerFormatType.parse(raw['format_type'] as int), + formatType: StickerFormatType(raw['format_type'] as int), ); } diff --git a/lib/src/http/managers/user_manager.dart b/lib/src/http/managers/user_manager.dart index 5115fd108..ce903a67b 100644 --- a/lib/src/http/managers/user_manager.dart +++ b/lib/src/http/managers/user_manager.dart @@ -52,7 +52,7 @@ class UserManager extends ReadOnlyManager { accentColor: hasAccentColor ? DiscordColor(raw['accent_color'] as int) : null, locale: hasLocale ? Locale.parse(raw['locale'] as String) : null, flags: hasFlags ? UserFlags(raw['flags'] as int) : null, - nitroType: hasPremiumType ? NitroType.parse(raw['premium_type'] as int) : NitroType.none, + nitroType: hasPremiumType ? NitroType(raw['premium_type'] as int) : NitroType.none, publicFlags: hasPublicFlags ? UserFlags(raw['public_flags'] as int) : null, avatarDecorationHash: raw['avatar_decoration'] as String?, ); @@ -77,7 +77,7 @@ class UserManager extends ReadOnlyManager { isFriendSyncEnabled: raw['friend_sync'] as bool, showActivity: raw['show_activity'] as bool, isTwoWayLink: raw['two_way_link'] as bool, - visibility: ConnectionVisibility.parse(raw['visibility'] as int), + visibility: ConnectionVisibility(raw['visibility'] as int), ); } diff --git a/lib/src/http/managers/webhook_manager.dart b/lib/src/http/managers/webhook_manager.dart index 24b15855e..e24bae0c1 100644 --- a/lib/src/http/managers/webhook_manager.dart +++ b/lib/src/http/managers/webhook_manager.dart @@ -30,7 +30,7 @@ class WebhookManager extends Manager { return Webhook( id: Snowflake.parse(raw['id']!), manager: this, - type: WebhookType.parse(raw['type'] as int), + type: WebhookType(raw['type'] as int), guildId: maybeParse(raw['guild_id'], Snowflake.parse), channelId: maybeParse(raw['channel_id'], Snowflake.parse), user: maybeParse(raw['user'], client.users.parse), diff --git a/lib/src/models/application.dart b/lib/src/models/application.dart index 5f0b1216c..809ac57a0 100644 --- a/lib/src/models/application.dart +++ b/lib/src/models/application.dart @@ -194,6 +194,7 @@ final class ApplicationIntegrationType extends EnumLike /// @nodoc const ConnectionMetadataType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ConnectionMetadataType.parse(int value) : this(value); } diff --git a/lib/src/models/channel/channel.dart b/lib/src/models/channel/channel.dart index 6268ca847..1928300a1 100644 --- a/lib/src/models/channel/channel.dart +++ b/lib/src/models/channel/channel.dart @@ -92,6 +92,7 @@ final class ChannelType extends EnumLike { /// @nodoc const ChannelType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ChannelType.parse(int value) : this(value); } diff --git a/lib/src/models/channel/stage_instance.dart b/lib/src/models/channel/stage_instance.dart index 352585f25..a67170738 100644 --- a/lib/src/models/channel/stage_instance.dart +++ b/lib/src/models/channel/stage_instance.dart @@ -66,5 +66,6 @@ final class PrivacyLevel extends EnumLike { /// @nodoc const PrivacyLevel(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') PrivacyLevel.parse(int value) : this(value); } diff --git a/lib/src/models/channel/types/forum.dart b/lib/src/models/channel/types/forum.dart index 3b198999e..57a1c3f56 100644 --- a/lib/src/models/channel/types/forum.dart +++ b/lib/src/models/channel/types/forum.dart @@ -192,6 +192,7 @@ final class ForumSort extends EnumLike { /// @nodoc const ForumSort(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ForumSort.parse(int value) : this(value); } @@ -204,5 +205,6 @@ final class ForumLayout extends EnumLike { /// @nodoc const ForumLayout(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ForumLayout.parse(int value) : this(value); } diff --git a/lib/src/models/channel/voice_channel.dart b/lib/src/models/channel/voice_channel.dart index 63129fea6..e83202ced 100644 --- a/lib/src/models/channel/voice_channel.dart +++ b/lib/src/models/channel/voice_channel.dart @@ -27,5 +27,6 @@ final class VideoQualityMode extends EnumLike { /// @nodoc const VideoQualityMode(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') VideoQualityMode.parse(int value) : this(value); } diff --git a/lib/src/models/commands/application_command.dart b/lib/src/models/commands/application_command.dart index 29ff000d8..878c2cd50 100644 --- a/lib/src/models/commands/application_command.dart +++ b/lib/src/models/commands/application_command.dart @@ -114,5 +114,6 @@ final class ApplicationCommandType extends EnumLike /// @nodoc const ApplicationCommandType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ApplicationCommandType.parse(int value) : this(value); } diff --git a/lib/src/models/commands/application_command_option.dart b/lib/src/models/commands/application_command_option.dart index 202024f4f..4b7931068 100644 --- a/lib/src/models/commands/application_command_option.dart +++ b/lib/src/models/commands/application_command_option.dart @@ -87,6 +87,7 @@ final class CommandOptionType extends EnumLike { /// @nodoc const CommandOptionType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') CommandOptionType.parse(int value) : this(value); } diff --git a/lib/src/models/commands/application_command_permissions.dart b/lib/src/models/commands/application_command_permissions.dart index 98d88e977..d149a54dc 100644 --- a/lib/src/models/commands/application_command_permissions.dart +++ b/lib/src/models/commands/application_command_permissions.dart @@ -88,5 +88,6 @@ final class CommandPermissionType extends EnumLike { /// @nodoc const CommandPermissionType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') CommandPermissionType.parse(int value) : this(value); } diff --git a/lib/src/models/entitlement.dart b/lib/src/models/entitlement.dart index 142997850..0bd042ba9 100644 --- a/lib/src/models/entitlement.dart +++ b/lib/src/models/entitlement.dart @@ -104,5 +104,6 @@ final class EntitlementType extends EnumLike { const EntitlementType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') EntitlementType.parse(int value) : this(value); } diff --git a/lib/src/models/guild/audit_log.dart b/lib/src/models/guild/audit_log.dart index c8a3ea086..431f703b4 100644 --- a/lib/src/models/guild/audit_log.dart +++ b/lib/src/models/guild/audit_log.dart @@ -143,6 +143,7 @@ final class AuditLogEvent extends EnumLike { /// @nodoc const AuditLogEvent(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') AuditLogEvent.parse(int value) : this(value); } diff --git a/lib/src/models/guild/auto_moderation.dart b/lib/src/models/guild/auto_moderation.dart index 822a8a3c2..99a9156eb 100644 --- a/lib/src/models/guild/auto_moderation.dart +++ b/lib/src/models/guild/auto_moderation.dart @@ -94,6 +94,7 @@ final class AutoModerationEventType extends EnumLike { /// @nodoc const TriggerType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') TriggerType.parse(int value) : this(value); } @@ -176,6 +178,7 @@ final class KeywordPresetType extends EnumLike { /// @nodoc const KeywordPresetType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') KeywordPresetType.parse(int value) : this(value); } @@ -215,6 +218,7 @@ final class ActionType extends EnumLike { /// @nodoc const ActionType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ActionType.parse(int value) : this(value); } diff --git a/lib/src/models/guild/guild.dart b/lib/src/models/guild/guild.dart index ec8d94e2a..d6af7f0a9 100644 --- a/lib/src/models/guild/guild.dart +++ b/lib/src/models/guild/guild.dart @@ -477,6 +477,7 @@ final class VerificationLevel extends EnumLike { /// @nodoc const VerificationLevel(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') VerificationLevel.parse(int value) : this(value); } @@ -488,6 +489,7 @@ final class MessageNotificationLevel extends EnumLike { /// @nodoc const MfaLevel(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') MfaLevel.parse(int value) : this(value); } @@ -735,6 +739,7 @@ final class PremiumTier extends EnumLike { /// nodoc const PremiumTier(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') PremiumTier.parse(int value) : this(value); } @@ -748,5 +753,6 @@ final class NsfwLevel extends EnumLike { /// nodoc const NsfwLevel(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') NsfwLevel.parse(int value) : this(value); } diff --git a/lib/src/models/guild/integration.dart b/lib/src/models/guild/integration.dart index 18f6eadc4..29dfc504f 100644 --- a/lib/src/models/guild/integration.dart +++ b/lib/src/models/guild/integration.dart @@ -102,6 +102,7 @@ final class IntegrationExpireBehavior extends EnumLike { /// @nodoc const OnboardingPromptType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') OnboardingPromptType.parse(int value) : this(value); } diff --git a/lib/src/models/guild/scheduled_event.dart b/lib/src/models/guild/scheduled_event.dart index 5ce11417f..3f0fd0031 100644 --- a/lib/src/models/guild/scheduled_event.dart +++ b/lib/src/models/guild/scheduled_event.dart @@ -127,6 +127,7 @@ final class EventStatus extends EnumLike { /// @nodoc const EventStatus(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') EventStatus.parse(int value) : this(value); } @@ -139,6 +140,7 @@ final class ScheduledEntityType extends EnumLike { /// @nodoc const ScheduledEntityType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ScheduledEntityType.parse(int value) : this(value); } diff --git a/lib/src/models/interaction.dart b/lib/src/models/interaction.dart index 148eb00de..c52246087 100644 --- a/lib/src/models/interaction.dart +++ b/lib/src/models/interaction.dart @@ -39,6 +39,7 @@ final class InteractionContextType extends EnumLike /// @nodoc const InteractionContextType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') InteractionContextType.parse(int value) : this(value); } @@ -427,6 +428,7 @@ final class InteractionType extends EnumLike { /// @nodoc const InteractionType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') InteractionType.parse(int value) : this(value); } diff --git a/lib/src/models/invite/invite.dart b/lib/src/models/invite/invite.dart index 18bad562f..9582d7dc4 100644 --- a/lib/src/models/invite/invite.dart +++ b/lib/src/models/invite/invite.dart @@ -77,5 +77,6 @@ final class TargetType extends EnumLike { /// @nodoc const TargetType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') TargetType.parse(int value) : this(value); } diff --git a/lib/src/models/message/activity.dart b/lib/src/models/message/activity.dart index 1d7cd2ded..173e823be 100644 --- a/lib/src/models/message/activity.dart +++ b/lib/src/models/message/activity.dart @@ -35,5 +35,6 @@ final class MessageActivityType extends EnumLike { /// @nodoc const MessageActivityType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') MessageActivityType.parse(int value) : this(value); } diff --git a/lib/src/models/message/component.dart b/lib/src/models/message/component.dart index d7fc87960..1f27edab2 100644 --- a/lib/src/models/message/component.dart +++ b/lib/src/models/message/component.dart @@ -18,6 +18,7 @@ final class MessageComponentType extends EnumLike { /// @nodoc const MessageComponentType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') MessageComponentType.parse(int value) : this(value); } @@ -86,6 +87,7 @@ final class ButtonStyle extends EnumLike { /// @nodoc const ButtonStyle(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ButtonStyle.parse(int value) : this(value); } @@ -146,6 +148,7 @@ final class SelectMenuDefaultValueType extends EnumLike { /// @nodoc const TextInputStyle(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') TextInputStyle.parse(int value) : this(value); } diff --git a/lib/src/models/message/message.dart b/lib/src/models/message/message.dart index 85d7d3e8b..12ba221e2 100644 --- a/lib/src/models/message/message.dart +++ b/lib/src/models/message/message.dart @@ -293,6 +293,7 @@ final class MessageType extends EnumLike { /// @nodoc const MessageType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') MessageType.parse(int value) : this(value); } diff --git a/lib/src/models/message/poll.dart b/lib/src/models/message/poll.dart index a421737a6..b2cfa8653 100644 --- a/lib/src/models/message/poll.dart +++ b/lib/src/models/message/poll.dart @@ -13,6 +13,7 @@ final class PollLayoutType extends EnumLike { /// @nodoc const PollLayoutType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') PollLayoutType.parse(int value) : this(value); } diff --git a/lib/src/models/permission_overwrite.dart b/lib/src/models/permission_overwrite.dart index af1b378b1..a77638130 100644 --- a/lib/src/models/permission_overwrite.dart +++ b/lib/src/models/permission_overwrite.dart @@ -53,5 +53,6 @@ final class PermissionOverwriteType extends EnumLike { /// @nodoc const UserStatus(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') UserStatus.parse(String value) : this(value); } @@ -116,6 +117,7 @@ final class ActivityType extends EnumLike { const ActivityType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ActivityType.parse(int value) : this(value); } diff --git a/lib/src/models/sku.dart b/lib/src/models/sku.dart index 798ad1b5b..2d7ea3952 100644 --- a/lib/src/models/sku.dart +++ b/lib/src/models/sku.dart @@ -63,6 +63,7 @@ final class SkuType extends EnumLike { /// @nodoc const SkuType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') SkuType.parse(int value) : this(value); } diff --git a/lib/src/models/sticker/sticker.dart b/lib/src/models/sticker/sticker.dart index ddb64380d..be4e35571 100644 --- a/lib/src/models/sticker/sticker.dart +++ b/lib/src/models/sticker/sticker.dart @@ -21,6 +21,7 @@ final class StickerFormatType extends EnumLike { /// @nodoc const StickerFormatType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') StickerFormatType.parse(int value) : this(value); } diff --git a/lib/src/models/team.dart b/lib/src/models/team.dart index ac434cb6c..24a176a7a 100644 --- a/lib/src/models/team.dart +++ b/lib/src/models/team.dart @@ -92,6 +92,7 @@ final class TeamMembershipState extends EnumLike { /// @nodoc const TeamMembershipState(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') TeamMembershipState.parse(int value) : this(value); } diff --git a/lib/src/models/user/connection.dart b/lib/src/models/user/connection.dart index 53f14f914..97c8e7008 100644 --- a/lib/src/models/user/connection.dart +++ b/lib/src/models/user/connection.dart @@ -109,5 +109,6 @@ final class ConnectionVisibility extends EnumLike { /// @nodoc const ConnectionVisibility(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') ConnectionVisibility.parse(int value) : this(value); } diff --git a/lib/src/models/user/user.dart b/lib/src/models/user/user.dart index 3ce2f014f..3d19f2fc6 100644 --- a/lib/src/models/user/user.dart +++ b/lib/src/models/user/user.dart @@ -237,5 +237,6 @@ final class NitroType extends EnumLike { /// @nodoc const NitroType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') NitroType.parse(int value) : this(value); } diff --git a/lib/src/models/webhook.dart b/lib/src/models/webhook.dart index 3b8c59811..81ae36e20 100644 --- a/lib/src/models/webhook.dart +++ b/lib/src/models/webhook.dart @@ -187,5 +187,6 @@ final class WebhookType extends EnumLike { /// @nodoc const WebhookType(super.value); + @Deprecated('The .parse() constructor is deprecated. Use the unnamed constructor instead.') WebhookType.parse(int value) : this(value); } diff --git a/lib/src/utils/enum_like.dart b/lib/src/utils/enum_like.dart index 1f941c025..f16c7b45b 100644 --- a/lib/src/utils/enum_like.dart +++ b/lib/src/utils/enum_like.dart @@ -12,5 +12,5 @@ base class EnumLike> { int get hashCode => value.hashCode; @override - bool operator ==(covariant EnumLike other) => identical(this, other) || (other is U && other.value == value); + bool operator ==(Object other) => identical(this, other) || (other is U && other.value == value); } From f1211817dda7d04f64ea16436f8c6572c5291846 Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Mon, 24 Jun 2024 14:52:55 +0200 Subject: [PATCH 7/7] format --- lib/src/models/message/component.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/models/message/component.dart b/lib/src/models/message/component.dart index d3c63eefb..ae7698a92 100644 --- a/lib/src/models/message/component.dart +++ b/lib/src/models/message/component.dart @@ -88,7 +88,7 @@ final class ButtonStyle extends EnumLike { static const danger = ButtonStyle(4); static const link = ButtonStyle(5); static const premium = ButtonStyle(6); - + /// @nodoc const ButtonStyle(super.value);