Skip to content

Commit

Permalink
Merge branch 'dev' into fix/user-avatar-deco
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexedia authored Oct 22, 2024
2 parents 1d89380 + 4041b55 commit ffd315c
Show file tree
Hide file tree
Showing 33 changed files with 811 additions and 22 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/scheduled_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ScheduledEventUpdateBuilder extends UpdateBuilder<ScheduledEvent> {
this.type,
this.status,
this.image,
this.recurrenceRule,
this.recurrenceRule = sentinelRecurrenceRuleBuilder,
});

@override
Expand All @@ -137,7 +137,7 @@ class ScheduledEventUpdateBuilder extends UpdateBuilder<ScheduledEvent> {
if (type != null) 'entity_type': type!.value,
if (status != null) 'status': status!.value,
if (image != null) 'image': image!.buildDataString(),
if (recurrenceRule != null) 'recurrence_rule': recurrenceRule!.build(),
if (!identical(recurrenceRule, sentinelRecurrenceRuleBuilder)) 'recurrence_rule': recurrenceRule?.build(),
};
}

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
2 changes: 1 addition & 1 deletion lib/src/builders/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ImageBuilder {
ImageBuilder.gif(this.data) : format = 'gif';

static Future<ImageBuilder> fromFile(File file, {String? format}) async {
format ??= p.extension(file.path);
format ??= p.extension(file.path).substring(1);

const formats = {
'png': 'png',
Expand Down
5 changes: 5 additions & 0 deletions lib/src/builders/message/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ class MessageUpdateBuilder extends UpdateBuilder<Message> {

List<AttachmentBuilder>? attachments;

/// Can only be used when editing a deferred interaction.
PollBuilder? poll;

MessageUpdateBuilder({
this.content = sentinelString,
this.embeds = sentinelList,
this.suppressEmbeds,
this.allowedMentions,
this.components,
this.attachments = sentinelList,
this.poll,
});

@override
Expand All @@ -140,6 +144,7 @@ class MessageUpdateBuilder extends UpdateBuilder<Message> {
if (components != null) 'components': components!.map((e) => e.build()).toList(),
if (!identical(attachments, sentinelList)) 'attachments': attachments!.map((e) => e.build()).toList(),
if (suppressEmbeds != null) 'flags': (suppressEmbeds == true ? MessageFlags.suppressEmbeds.value : 0),
if (poll != null) 'poll': poll!.build(),
};
}

Expand Down
12 changes: 12 additions & 0 deletions lib/src/builders/sentinels.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:nyxx/src/builders/guild/scheduled_event.dart';
import 'package:nyxx/src/builders/image.dart';
import 'package:nyxx/src/models/channel/types/forum.dart';
import 'package:nyxx/src/models/emoji.dart';
Expand All @@ -8,6 +9,8 @@ import 'package:nyxx/src/utils/flags.dart';
// ASCII encoded "nyxx"
const sentinelInteger = 0x6E797878;

const sentinelDouble = sentinelInteger * 1.0;

// ESC-"nyxx"
const sentinelString = '\u{1B}nyxx';

Expand Down Expand Up @@ -109,3 +112,12 @@ class _SentinelEmoji implements Emoji {
}

const sentinelEmoji = _SentinelEmoji();

class _SentinelRecurrenceRuleBuilder implements RecurrenceRuleBuilder {
const _SentinelRecurrenceRuleBuilder();

@override
void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

const sentinelRecurrenceRuleBuilder = _SentinelRecurrenceRuleBuilder();
38 changes: 38 additions & 0 deletions lib/src/builders/sound.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as p;

class SoundBuilder {
List<int> data;
String format;

SoundBuilder({required this.data, required this.format});

SoundBuilder.mp3(this.data) : format = 'mpeg';

SoundBuilder.ogg(this.data) : format = 'ogg';

static Future<SoundBuilder> fromFile(File file, {String? format}) async {
format ??= p.extension(file.path).substring(1);

const formats = {
'mp3': 'mpeg',
'mpeg': 'mpeg',
'ogg': 'ogg',
};

final actualFormat = formats[format];

if (actualFormat == null) {
throw ArgumentError.value(format, 'format', 'Unsupported format');
}

final data = await file.readAsBytes();

return SoundBuilder(data: data, format: actualFormat);
}

List<int> buildRawData() => data;

String buildDataString() => 'data:audio/$format;base64,${base64Encode(data)}';
}
47 changes: 47 additions & 0 deletions lib/src/builders/soundboard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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/snowflake.dart';
import 'package:nyxx/src/models/soundboard/soundboard.dart';
import 'package:nyxx/src/utils/building_helpers.dart';

class SoundboardSoundBuilder extends CreateBuilder<SoundboardSound> {
String name;

SoundBuilder sound;

double? volume;

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(emojiId: emojiId, emojiName: emojiName),
};
}

class SoundboardSoundUpdateBuilder extends UpdateBuilder<SoundboardSound> {
String name;

double? volume;

String? emojiName;

Snowflake? emojiId;

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(emojiName, sentinelString) || identical(emojiId, sentinelSnowflake))) ...makeEmojiMap(emojiId: emojiId, emojiName: emojiName),
};
}
11 changes: 10 additions & 1 deletion lib/src/client_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:nyxx/src/models/guild/scheduled_event.dart';
import 'package:nyxx/src/models/message/message.dart';
import 'package:nyxx/src/models/role.dart';
import 'package:nyxx/src/models/sku.dart';
import 'package:nyxx/src/models/soundboard/soundboard.dart';
import 'package:nyxx/src/models/sticker/global_sticker.dart';
import 'package:nyxx/src/models/sticker/guild_sticker.dart';
import 'package:nyxx/src/models/subscription.dart';
Expand Down Expand Up @@ -143,9 +144,15 @@ class RestClientOptions extends ClientOptions {
/// The [CacheConfig] to use for the [Application.skus] manager.
final CacheConfig<Sku> skuConfig;

/// Tje [CacheConfig] to use for the [Sku.subscriptions] manager.
/// The [CacheConfig] to use for the [Sku.subscriptions] manager.
final CacheConfig<Subscription> subscriptionConfig;

/// The [CacheConfig] to use for the [NyxxRest.soundboard] manager.
final CacheConfig<SoundboardSound> globalSoundboardCacheConfig;

/// The [CacheConfig] to use for the [Guild.soundboard] manager.
final CacheConfig<SoundboardSound> soundboardCacheConfig;

/// Create a new [RestClientOptions].
const RestClientOptions({
super.plugins,
Expand Down Expand Up @@ -187,6 +194,8 @@ class RestClientOptions extends ClientOptions {
this.entitlementConfig = _defaultCacheConfig,
this.skuConfig = _defaultCacheConfig,
this.subscriptionConfig = _defaultCacheConfig,
this.globalSoundboardCacheConfig = _smallCacheConfig,
this.soundboardCacheConfig = _smallCacheConfig,
});
}

Expand Down
7 changes: 7 additions & 0 deletions lib/src/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,10 @@ class ClientClosedError extends Error {
@override
String toString() => 'Client is closed';
}

class SoundboardSoundNotFoundException extends NyxxException {
/// The ID of the sound.
final Snowflake soundId;

SoundboardSoundNotFoundException(this.soundId) : super('Soundboard sound $soundId not found');
}
16 changes: 16 additions & 0 deletions lib/src/event_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:nyxx/src/models/gateway/events/invite.dart';
import 'package:nyxx/src/models/gateway/events/message.dart';
import 'package:nyxx/src/models/gateway/events/presence.dart';
import 'package:nyxx/src/models/gateway/events/ready.dart';
import 'package:nyxx/src/models/gateway/events/soundboard.dart';
import 'package:nyxx/src/models/gateway/events/stage_instance.dart';
import 'package:nyxx/src/models/gateway/events/voice.dart';
import 'package:nyxx/src/models/gateway/events/webhook.dart';
Expand Down Expand Up @@ -261,4 +262,19 @@ mixin EventMixin implements Nyxx {

/// A [Stream] of [MessagePollVoteRemoveEvent]s received by this client.
Stream<MessagePollVoteRemoveEvent> get onMessagePollVoteRemove => onEvent.whereType<MessagePollVoteRemoveEvent>();

/// A [Stream] of [SoundboardSoundDeleteEvent]s received by this client.
Stream<SoundboardSoundDeleteEvent> get onSoundboardSoundDelete => onEvent.whereType<SoundboardSoundDeleteEvent>();

/// A [Stream] of [SoundboardSoundUpdateEvent]s received by this client.
Stream<SoundboardSoundUpdateEvent> get onSoundboardSoundUpdate => onEvent.whereType<SoundboardSoundUpdateEvent>();

/// A [Stream] of [SoundboardSoundCreateEvent]s received by this client.
Stream<SoundboardSoundCreateEvent> get onSoundboardSoundCreate => onEvent.whereType<SoundboardSoundCreateEvent>();

/// A [Stream] of [SoundboardSoundsUpdateEvent]s received by this client.
Stream<SoundboardSoundsUpdateEvent> get onSoundboardSoundsUpdate => onEvent.whereType<SoundboardSoundsUpdateEvent>();

/// A [Stream] of [VoiceChannelEffectSendEvent]s received by this client.
Stream<VoiceChannelEffectSendEvent> get onVoiceChannelEffectSend => onEvent.whereType<VoiceChannelEffectSendEvent>();
}
Loading

0 comments on commit ffd315c

Please sign in to comment.