From 0aa80bc1c4193b3ca83cc6445f7751815c837324 Mon Sep 17 00:00:00 2001 From: Lexedia Date: Fri, 11 Oct 2024 14:51:07 +0200 Subject: [PATCH] add helper for emojis, and rename update builder --- lib/src/builders/channel/forum_tag.dart | 4 +-- lib/src/builders/channel/guild_channel.dart | 15 +++-------- lib/src/builders/guild/welcome_screen.dart | 4 +-- lib/src/builders/soundboard.dart | 25 +++++++++++-------- lib/src/http/managers/soundboard_manager.dart | 4 +-- lib/src/utils/building_helpers.dart | 6 +++++ test/integration/rest_integration_test.dart | 2 +- .../managers/soundboard_manager_test.dart | 4 +-- 8 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 lib/src/utils/building_helpers.dart diff --git a/lib/src/builders/channel/forum_tag.dart b/lib/src/builders/channel/forum_tag.dart index 3542fca88..a702e4ade 100644 --- a/lib/src/builders/channel/forum_tag.dart +++ b/lib/src/builders/channel/forum_tag.dart @@ -1,6 +1,7 @@ import 'package:nyxx/src/builders/builder.dart'; import 'package:nyxx/src/models/channel/types/forum.dart'; import 'package:nyxx/src/models/snowflake.dart'; +import 'package:nyxx/src/utils/building_helpers.dart'; class ForumTagBuilder extends CreateBuilder { String name; @@ -17,7 +18,6 @@ class ForumTagBuilder extends CreateBuilder { Map build() => { 'name': name, if (isModerated != null) 'moderated': isModerated, - if (emojiId != null) 'emoji_id': emojiId!.toString(), - if (emojiName != null) 'emoji_name': emojiName, + ...makeEmojiMap(emojiId: emojiId, emojiName: emojiName), }; } diff --git a/lib/src/builders/channel/guild_channel.dart b/lib/src/builders/channel/guild_channel.dart index 6016f440c..442f4fd6c 100644 --- a/lib/src/builders/channel/guild_channel.dart +++ b/lib/src/builders/channel/guild_channel.dart @@ -11,6 +11,7 @@ import 'package:nyxx/src/models/channel/types/guild_voice.dart'; import 'package:nyxx/src/models/channel/voice_channel.dart'; import 'package:nyxx/src/models/permission_overwrite.dart'; import 'package:nyxx/src/models/snowflake.dart'; +import 'package:nyxx/src/utils/building_helpers.dart'; import 'package:nyxx/src/utils/flags.dart'; class GuildChannelBuilder extends CreateBuilder { @@ -231,12 +232,7 @@ class ForumChannelBuilder extends GuildChannelBuilder { if (isNsfw != null) 'nsfw': isNsfw, if (defaultAutoArchiveDuration != null) 'default_auto_archive_duration': defaultAutoArchiveDuration!.inMinutes, if (!identical(defaultReaction, sentinelDefaultReaction)) - 'default_reaction_emoji': defaultReaction == null - ? null - : { - if (defaultReaction!.emojiId != null) 'emoji_id': defaultReaction!.emojiId!.toString(), - if (defaultReaction!.emojiName != null) 'emoji_name': defaultReaction!.emojiName, - }, + 'default_reaction_emoji': defaultReaction == null ? null : makeEmojiMap(emojiId: defaultReaction!.emojiId, emojiName: defaultReaction!.emojiName), if (tags != null) 'available_tags': tags!.map((e) => e.build()).toList(), if (defaultSortOrder != null) 'default_sort_order': defaultSortOrder!.value, }; @@ -293,12 +289,7 @@ class ForumChannelUpdateBuilder extends GuildChannelUpdateBuilder if (flags != null) 'flags': flags!.value, if (tags != null) 'available_tags': tags!.map((e) => e.build()).toList(), if (!identical(defaultReaction, sentinelDefaultReaction)) - 'default_reaction_emoji': defaultReaction == null - ? null - : { - if (defaultReaction!.emojiId != null) 'emoji_id': defaultReaction!.emojiId!.toString(), - if (defaultReaction!.emojiName != null) 'emoji_name': defaultReaction!.emojiName, - }, + 'default_reaction_emoji': defaultReaction == null ? null : makeEmojiMap(emojiId: defaultReaction!.emojiId, emojiName: defaultReaction!.emojiName), if (defaultThreadRateLimitPerUser != null) 'default_thread_rate_limit_per_user': defaultThreadRateLimitPerUser!.inSeconds, if (defaultSortOrder != null) 'default_sort_order': defaultSortOrder!.value, if (defaultLayout != null) 'default_forum_layout': defaultLayout!.value, diff --git a/lib/src/builders/guild/welcome_screen.dart b/lib/src/builders/guild/welcome_screen.dart index 8ea232fb0..f852d3feb 100644 --- a/lib/src/builders/guild/welcome_screen.dart +++ b/lib/src/builders/guild/welcome_screen.dart @@ -1,6 +1,7 @@ import 'package:nyxx/src/builders/builder.dart'; import 'package:nyxx/src/builders/sentinels.dart'; import 'package:nyxx/src/models/guild/welcome_screen.dart'; +import 'package:nyxx/src/utils/building_helpers.dart'; class WelcomeScreenUpdateBuilder extends UpdateBuilder { bool? isEnabled; @@ -20,8 +21,7 @@ class WelcomeScreenUpdateBuilder extends UpdateBuilder { { 'channel_id': channel.channelId.toString(), 'description': channel.description, - 'emoji_id': channel.emojiId?.toString(), - 'emoji_name': channel.emojiName, + ...makeEmojiMap(emojiId: channel.emojiId, emojiName: channel.emojiName), }, ], if (!identical(description, sentinelString)) 'description': description, diff --git a/lib/src/builders/soundboard.dart b/lib/src/builders/soundboard.dart index 4688b4282..107b33481 100644 --- a/lib/src/builders/soundboard.dart +++ b/lib/src/builders/soundboard.dart @@ -2,12 +2,9 @@ import 'package:nyxx/src/builders/builder.dart'; import 'package:nyxx/src/builders/sentinels.dart'; import 'package:nyxx/src/builders/sound.dart'; import 'package:nyxx/src/models/emoji.dart'; +import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/soundboard/soundboard.dart'; - -Map _makeEmojiMap(Emoji? emoji) => { - if (emoji case final TextEmoji emoji?) 'emoji_name': emoji.name, - if (emoji case final GuildEmoji emoji?) 'emoji_id': emoji.id.toString(), - }; +import 'package:nyxx/src/utils/building_helpers.dart'; class SoundboardSoundBuilder extends CreateBuilder { String name; @@ -18,30 +15,36 @@ class SoundboardSoundBuilder extends CreateBuilder { Emoji? emoji; - SoundboardSoundBuilder({required this.name, required this.sound, this.volume, this.emoji}); + String? emojiName; + + Snowflake? emojiId; + + SoundboardSoundBuilder({required this.name, required this.sound, this.volume, this.emojiName, this.emojiId}); @override Map build() => { 'name': name, 'sound': sound.buildDataString(), if (volume != null) 'volume': volume, - ..._makeEmojiMap(emoji), + ...makeEmojiMap(emojiId: emojiId, emojiName: emojiName), }; } -class UpdateSoundboardSoundBuilder extends UpdateBuilder { +class SoundboardSoundUpdateBuilder extends UpdateBuilder { String name; double? volume; - Emoji? emoji; + String? emojiName; + + Snowflake? emojiId; - UpdateSoundboardSoundBuilder({required this.name, this.volume = sentinelDouble, this.emoji = sentinelEmoji}); + SoundboardSoundUpdateBuilder({required this.name, this.volume = sentinelDouble, this.emojiName = sentinelString, this.emojiId = sentinelSnowflake}); @override Map build() => { 'name': name, if (volume != sentinelDouble) 'volume': volume, - if (!identical(emoji, sentinelEmoji)) ..._makeEmojiMap(emoji), + if (!(identical(emojiName, sentinelString) || identical(emojiId, sentinelSnowflake))) ...makeEmojiMap(emojiId: emojiId, emojiName: emojiName), }; } diff --git a/lib/src/http/managers/soundboard_manager.dart b/lib/src/http/managers/soundboard_manager.dart index b7dea33a0..013c41c62 100644 --- a/lib/src/http/managers/soundboard_manager.dart +++ b/lib/src/http/managers/soundboard_manager.dart @@ -102,7 +102,7 @@ class GuildSoundboardManager extends SoundboardManager { } @override - Future update(Snowflake id, UpdateSoundboardSoundBuilder builder, {String? auditLogReason}) async { + Future update(Snowflake id, SoundboardSoundUpdateBuilder builder, {String? auditLogReason}) async { final route = HttpRoute() ..guilds(id: guildId.toString()) ..soundboardSounds(id: id.toString()); @@ -187,6 +187,6 @@ class GlobalSoundboardManager extends SoundboardManager implements ReadOnlyManag Future create(SoundboardSoundBuilder builder, {String? auditLogReason}) => throw UnsupportedError('Cannot create a global soundboard sound'); @override - Future update(Snowflake id, UpdateSoundboardSoundBuilder builder, {String? auditLogReason}) => + Future update(Snowflake id, SoundboardSoundUpdateBuilder builder, {String? auditLogReason}) => throw UnsupportedError('Cannot update a global soundboard sound'); } diff --git a/lib/src/utils/building_helpers.dart b/lib/src/utils/building_helpers.dart new file mode 100644 index 000000000..f7db5894d --- /dev/null +++ b/lib/src/utils/building_helpers.dart @@ -0,0 +1,6 @@ +import 'package:nyxx/nyxx.dart'; + +Map makeEmojiMap({Snowflake? emojiId, String? emojiName}) => { + if (emojiName case final String emojiName?) 'emoji_name': emojiName, + if (emojiId case final Snowflake emojiId?) 'emoji_id': emojiId.toString(), + }; diff --git a/test/integration/rest_integration_test.dart b/test/integration/rest_integration_test.dart index b2bf916fa..5c462566f 100644 --- a/test/integration/rest_integration_test.dart +++ b/test/integration/rest_integration_test.dart @@ -454,7 +454,7 @@ void main() { completes, ); - await expectLater(sound.update(UpdateSoundboardSoundBuilder(name: 'New name')), completes); + await expectLater(sound.update(SoundboardSoundUpdateBuilder(name: 'New name')), completes); await expectLater(sound.delete(), completes); }); }); diff --git a/test/unit/http/managers/soundboard_manager_test.dart b/test/unit/http/managers/soundboard_manager_test.dart index 7dd6209bd..bbee65a8b 100644 --- a/test/unit/http/managers/soundboard_manager_test.dart +++ b/test/unit/http/managers/soundboard_manager_test.dart @@ -91,8 +91,8 @@ void main() { name: 'cool', sound: SoundBuilder.ogg([0, 1, 2, 3]), volume: .5, - emoji: TextEmoji(id: Snowflake.zero, manager: client.application.emojis, name: '😎'), + emojiName: '😎', ), - updateBuilder: UpdateSoundboardSoundBuilder(name: 'cooler', volume: .7), + updateBuilder: SoundboardSoundUpdateBuilder(name: 'cooler', volume: .7), ); }