From 8a8655b569f892dd1544eb13a6f7da2119997f84 Mon Sep 17 00:00:00 2001 From: DinoLeung Date: Sun, 31 Mar 2024 20:00:57 +1030 Subject: [PATCH] use enum in bot command scope --- CHANGELOG.md | 2 +- .../telegram/models/bot_command_scope.dart | 32 +++++++++++++------ ...ommand_scope_all_chats_administrators.dart | 4 +-- .../bot_command_scope_all_group_chats.dart | 4 +-- .../bot_command_scope_all_private_chats.dart | 4 +-- .../models/bot_command_scope_chat.dart | 4 +-- ...bot_command_scope_chat_administrators.dart | 4 +-- .../models/bot_command_scope_chat_member.dart | 4 +-- .../models/bot_command_scope_default.dart | 4 +-- 9 files changed, 38 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3556cab..2029c49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ - support Telegram API 6.9 - **Breaking** Updated function `telegram:Telegram.sendDice` and function `teledart:Message.replyDice` to accept enum of `DiceEmoji` instead of emoji string -- **Breaking** Updated class `Dice`, `InlineQueryResult`, `MenuButton`, `PassportElementError` to use enum for `type` +- **Breaking** Updated class `Dice`, `InlineQueryResult`, `MenuButton`, `PassportElementError`, `InputMedia`, `InputMediaWithThumbnail`, `BotCommandScope` to use enum for `type` ## 0.6.1 diff --git a/lib/src/telegram/models/bot_command_scope.dart b/lib/src/telegram/models/bot_command_scope.dart index 88775db..54eea2c 100644 --- a/lib/src/telegram/models/bot_command_scope.dart +++ b/lib/src/telegram/models/bot_command_scope.dart @@ -18,6 +18,28 @@ part of '../model.dart'; +@JsonEnum() +enum BotCommandScopeType { + @JsonValue('default') + defaultCommand, + @JsonValue('all_private_chats') + allPrivateChats, + @JsonValue('all_group_chats') + allGroupChats, + @JsonValue('all_chat_administrators') + allChatAdministrators, + @JsonValue('chat') + chat, + @JsonValue('chat_administrators') + chatAdministrators, + @JsonValue('chat_member') + chatMember, +} + +extension BotCommandScopeTypeExtenson on BotCommandScopeType { + String value() => _$BotCommandScopeTypeEnumMap[this]!; +} + /// This object represents the scope to which bot commands are applied. /// /// Currently, the following 7 scopes are supported: @@ -32,15 +54,7 @@ part of '../model.dart'; /// https://core.telegram.org/bots/api#botcommandscope @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScope { - static const typeDefault = 'default'; - static const typeAllPrivateChats = 'all_private_chats'; - static const typeAllGroupChats = 'all_group_chats'; - static const typeAllChatAdministrators = 'all_chat_administrators'; - static const typeChat = 'chat'; - static const typeChatAdministrators = 'chat_administrators'; - static const typeChatMember = 'chat_member'; - - String type; + BotCommandScopeType type; BotCommandScope({required this.type}); diff --git a/lib/src/telegram/models/bot_command_scope_all_chats_administrators.dart b/lib/src/telegram/models/bot_command_scope_all_chats_administrators.dart index e75a36d..9a30e49 100644 --- a/lib/src/telegram/models/bot_command_scope_all_chats_administrators.dart +++ b/lib/src/telegram/models/bot_command_scope_all_chats_administrators.dart @@ -26,10 +26,10 @@ part of '../model.dart'; @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScopeAllChatAdministrators implements BotCommandScope { @override - String type; + BotCommandScopeType type; BotCommandScopeAllChatAdministrators( - {this.type = BotCommandScope.typeAllChatAdministrators}); + {this.type = BotCommandScopeType.allChatAdministrators}); factory BotCommandScopeAllChatAdministrators.fromJson( Map json) => diff --git a/lib/src/telegram/models/bot_command_scope_all_group_chats.dart b/lib/src/telegram/models/bot_command_scope_all_group_chats.dart index 8d7ac74..f459ffe 100644 --- a/lib/src/telegram/models/bot_command_scope_all_group_chats.dart +++ b/lib/src/telegram/models/bot_command_scope_all_group_chats.dart @@ -26,9 +26,9 @@ part of '../model.dart'; @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScopeAllGroupChats implements BotCommandScope { @override - String type; + BotCommandScopeType type; - BotCommandScopeAllGroupChats({this.type = BotCommandScope.typeAllGroupChats}); + BotCommandScopeAllGroupChats({this.type = BotCommandScopeType.allGroupChats}); factory BotCommandScopeAllGroupChats.fromJson(Map json) => _$BotCommandScopeAllGroupChatsFromJson(json); diff --git a/lib/src/telegram/models/bot_command_scope_all_private_chats.dart b/lib/src/telegram/models/bot_command_scope_all_private_chats.dart index 905a205..5255d41 100644 --- a/lib/src/telegram/models/bot_command_scope_all_private_chats.dart +++ b/lib/src/telegram/models/bot_command_scope_all_private_chats.dart @@ -26,10 +26,10 @@ part of '../model.dart'; @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScopeAllPrivateChats implements BotCommandScope { @override - String type; + BotCommandScopeType type; BotCommandScopeAllPrivateChats( - {this.type = BotCommandScope.typeAllPrivateChats}); + {this.type = BotCommandScopeType.allPrivateChats}); factory BotCommandScopeAllPrivateChats.fromJson(Map json) => _$BotCommandScopeAllPrivateChatsFromJson(json); diff --git a/lib/src/telegram/models/bot_command_scope_chat.dart b/lib/src/telegram/models/bot_command_scope_chat.dart index 3298a8e..d2643f7 100644 --- a/lib/src/telegram/models/bot_command_scope_chat.dart +++ b/lib/src/telegram/models/bot_command_scope_chat.dart @@ -26,11 +26,11 @@ part of '../model.dart'; @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScopeChat implements BotCommandScope { @override - String type; + BotCommandScopeType type; dynamic chatId; BotCommandScopeChat({ - this.type = BotCommandScope.typeChat, + this.type = BotCommandScopeType.chat, required this.chatId, }); diff --git a/lib/src/telegram/models/bot_command_scope_chat_administrators.dart b/lib/src/telegram/models/bot_command_scope_chat_administrators.dart index 2e92dd9..2226d3d 100644 --- a/lib/src/telegram/models/bot_command_scope_chat_administrators.dart +++ b/lib/src/telegram/models/bot_command_scope_chat_administrators.dart @@ -26,11 +26,11 @@ part of '../model.dart'; @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScopeChatAdministrators implements BotCommandScope { @override - String type; + BotCommandScopeType type; dynamic chatId; BotCommandScopeChatAdministrators({ - this.type = BotCommandScope.typeAllChatAdministrators, + this.type = BotCommandScopeType.allChatAdministrators, required this.chatId, }); diff --git a/lib/src/telegram/models/bot_command_scope_chat_member.dart b/lib/src/telegram/models/bot_command_scope_chat_member.dart index a7eda4f..9f110f7 100644 --- a/lib/src/telegram/models/bot_command_scope_chat_member.dart +++ b/lib/src/telegram/models/bot_command_scope_chat_member.dart @@ -26,11 +26,11 @@ part of '../model.dart'; @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScopeChatMember implements BotCommandScope { @override - String type; + BotCommandScopeType type; dynamic chatId; BotCommandScopeChatMember({ - this.type = BotCommandScope.typeChatMember, + this.type = BotCommandScopeType.chatMember, required this.chatId, }); diff --git a/lib/src/telegram/models/bot_command_scope_default.dart b/lib/src/telegram/models/bot_command_scope_default.dart index 77ad5ee..4e74ef4 100644 --- a/lib/src/telegram/models/bot_command_scope_default.dart +++ b/lib/src/telegram/models/bot_command_scope_default.dart @@ -29,9 +29,9 @@ part of '../model.dart'; @JsonSerializable(fieldRename: FieldRename.snake) class BotCommandScopeDefault implements BotCommandScope { @override - String type; + BotCommandScopeType type; - BotCommandScopeDefault({this.type = BotCommandScope.typeDefault}); + BotCommandScopeDefault({this.type = BotCommandScopeType.defaultCommand}); factory BotCommandScopeDefault.fromJson(Map json) => _$BotCommandScopeDefaultFromJson(json);