Skip to content

Commit

Permalink
add helper for emojis, and rename update builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexedia committed Oct 11, 2024
1 parent c7f6a7d commit 0aa80bc
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lib/src/builders/channel/forum_tag.dart
Original file line number Diff line number Diff line change
@@ -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<ForumTag> {
String name;
Expand All @@ -17,7 +18,6 @@ class ForumTagBuilder extends CreateBuilder<ForumTag> {
Map<String, Object?> 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),
};
}
15 changes: 3 additions & 12 deletions lib/src/builders/channel/guild_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends GuildChannel> extends CreateBuilder<T> {
Expand Down Expand Up @@ -231,12 +232,7 @@ class ForumChannelBuilder extends GuildChannelBuilder<ForumChannel> {
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,
};
Expand Down Expand Up @@ -293,12 +289,7 @@ class ForumChannelUpdateBuilder extends GuildChannelUpdateBuilder<ForumChannel>
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,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/builders/guild/welcome_screen.dart
Original file line number Diff line number Diff line change
@@ -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<WelcomeScreen> {
bool? isEnabled;
Expand All @@ -20,8 +21,7 @@ class WelcomeScreenUpdateBuilder extends UpdateBuilder<WelcomeScreen> {
{
'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,
Expand Down
25 changes: 14 additions & 11 deletions lib/src/builders/soundboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> _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<SoundboardSound> {
String name;
Expand All @@ -18,30 +15,36 @@ class SoundboardSoundBuilder extends CreateBuilder<SoundboardSound> {

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<String, Object?> build() => {
'name': name,
'sound': sound.buildDataString(),
if (volume != null) 'volume': volume,
..._makeEmojiMap(emoji),
...makeEmojiMap(emojiId: emojiId, emojiName: emojiName),
};
}

class UpdateSoundboardSoundBuilder extends UpdateBuilder<SoundboardSound> {
class SoundboardSoundUpdateBuilder extends UpdateBuilder<SoundboardSound> {
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<String, Object?> 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),
};
}
4 changes: 2 additions & 2 deletions lib/src/http/managers/soundboard_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class GuildSoundboardManager extends SoundboardManager {
}

@override
Future<SoundboardSound> update(Snowflake id, UpdateSoundboardSoundBuilder builder, {String? auditLogReason}) async {
Future<SoundboardSound> update(Snowflake id, SoundboardSoundUpdateBuilder builder, {String? auditLogReason}) async {
final route = HttpRoute()
..guilds(id: guildId.toString())
..soundboardSounds(id: id.toString());
Expand Down Expand Up @@ -187,6 +187,6 @@ class GlobalSoundboardManager extends SoundboardManager implements ReadOnlyManag
Future<SoundboardSound> create(SoundboardSoundBuilder builder, {String? auditLogReason}) => throw UnsupportedError('Cannot create a global soundboard sound');

@override
Future<SoundboardSound> update(Snowflake id, UpdateSoundboardSoundBuilder builder, {String? auditLogReason}) =>
Future<SoundboardSound> update(Snowflake id, SoundboardSoundUpdateBuilder builder, {String? auditLogReason}) =>
throw UnsupportedError('Cannot update a global soundboard sound');
}
6 changes: 6 additions & 0 deletions lib/src/utils/building_helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:nyxx/nyxx.dart';

Map<String, String> makeEmojiMap({Snowflake? emojiId, String? emojiName}) => {
if (emojiName case final String emojiName?) 'emoji_name': emojiName,
if (emojiId case final Snowflake emojiId?) 'emoji_id': emojiId.toString(),
};
2 changes: 1 addition & 1 deletion test/integration/rest_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/http/managers/soundboard_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

0 comments on commit 0aa80bc

Please sign in to comment.