|
| 1 | +import 'package:nyxx/nyxx.dart'; |
| 2 | +import 'package:nyxx_extensions/src/extensions/guild.dart'; |
| 3 | +import 'package:nyxx_extensions/src/extensions/managers/channel_manager.dart'; |
| 4 | +import 'package:nyxx_extensions/src/utils/formatters.dart'; |
| 5 | +import 'package:nyxx_extensions/src/utils/permissions.dart'; |
| 6 | + |
| 7 | +/// Extensions on [PartialChannel]s. |
| 8 | +extension PartialChannelExtensions on PartialChannel { |
| 9 | + /// A mention of this channel. |
| 10 | + String get mention => channelMention(id); |
| 11 | +} |
| 12 | + |
| 13 | +/// Extensions on [Channel]s. |
| 14 | +extension ChannelExtensions on Channel { |
| 15 | + /// A URL clients can visit to navigate to this channel. |
| 16 | + Uri get url => Uri.https(manager.client.apiOptions.host, '/channels/${this is GuildChannel ? '${(this as GuildChannel).guildId}' : '@me'}/$id'); |
| 17 | +} |
| 18 | + |
| 19 | +/// Extensions on [GuildChannel]s. |
| 20 | +extension GuildChannelExtensions on GuildChannel { |
| 21 | + /// Compute [member]'s permissions in this channel. |
| 22 | + /// |
| 23 | + /// {@macro compute_permissions_detail} |
| 24 | + Future<Permissions> computePermissionsFor(PartialMember member) async => await computePermissions(this, await member.get()); |
| 25 | +} |
| 26 | + |
| 27 | +/// Extensions on all [GuildChannel] types |
| 28 | +extension GuildChannelsExtension<T extends GuildChannel> on T { |
| 29 | + /// Create a builder with the properties of this channel |
| 30 | + GuildChannelBuilder<T> toBuilder() => switch (this) { |
| 31 | + GuildTextChannel channel => GuildTextChannelBuilder( |
| 32 | + name: channel.name, |
| 33 | + defaultAutoArchiveDuration: channel.defaultAutoArchiveDuration, |
| 34 | + isNsfw: channel.isNsfw, |
| 35 | + parentId: channel.parentId, |
| 36 | + permissionOverwrites: channel.permissionOverwrites |
| 37 | + .map((e) => PermissionOverwriteBuilder( |
| 38 | + id: e.id, |
| 39 | + type: e.type, |
| 40 | + allow: e.allow, |
| 41 | + deny: e.deny, |
| 42 | + )) |
| 43 | + .toList(), |
| 44 | + position: channel.position, |
| 45 | + rateLimitPerUser: channel.rateLimitPerUser, |
| 46 | + topic: channel.topic, |
| 47 | + ) as GuildChannelBuilder<T>, |
| 48 | + GuildVoiceChannel channel => GuildVoiceChannelBuilder( |
| 49 | + name: channel.name, |
| 50 | + permissionOverwrites: |
| 51 | + channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), |
| 52 | + position: channel.position, |
| 53 | + isNsfw: channel.isNsfw, |
| 54 | + parentId: channel.parentId, |
| 55 | + bitRate: channel.bitrate, |
| 56 | + rtcRegion: channel.rtcRegion, |
| 57 | + userLimit: channel.userLimit, |
| 58 | + videoQualityMode: channel.videoQualityMode, |
| 59 | + ) as GuildChannelBuilder<T>, |
| 60 | + GuildStageChannel channel => GuildStageChannelBuilder( |
| 61 | + name: channel.name, |
| 62 | + bitRate: channel.bitrate, |
| 63 | + isNsfw: channel.isNsfw, |
| 64 | + parentId: channel.parentId, |
| 65 | + permissionOverwrites: |
| 66 | + channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), |
| 67 | + position: channel.position, |
| 68 | + rtcRegion: channel.rtcRegion, |
| 69 | + userLimit: channel.userLimit, |
| 70 | + videoQualityMode: channel.videoQualityMode, |
| 71 | + ) as GuildChannelBuilder<T>, |
| 72 | + PrivateThread thread => ThreadBuilder.privateThread( |
| 73 | + name: thread.name, |
| 74 | + autoArchiveDuration: thread.autoArchiveDuration, |
| 75 | + invitable: thread.isInvitable, |
| 76 | + rateLimitPerUser: thread.rateLimitPerUser, |
| 77 | + ) as GuildChannelBuilder<T>, |
| 78 | + PublicThread thread => ThreadBuilder.publicThread( |
| 79 | + name: thread.name, |
| 80 | + autoArchiveDuration: thread.autoArchiveDuration, |
| 81 | + rateLimitPerUser: thread.rateLimitPerUser, |
| 82 | + ) as GuildChannelBuilder<T>, |
| 83 | + GuildCategory category => GuildCategoryBuilder( |
| 84 | + name: category.name, |
| 85 | + permissionOverwrites: |
| 86 | + category.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), |
| 87 | + position: category.position, |
| 88 | + ) as GuildChannelBuilder<T>, |
| 89 | + GuildAnnouncementChannel channel => GuildAnnouncementChannelBuilder( |
| 90 | + name: channel.name, |
| 91 | + defaultAutoArchiveDuration: channel.defaultAutoArchiveDuration, |
| 92 | + isNsfw: channel.isNsfw, |
| 93 | + parentId: channel.parentId, |
| 94 | + permissionOverwrites: |
| 95 | + channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), |
| 96 | + position: channel.position, |
| 97 | + topic: channel.topic, |
| 98 | + ) as GuildChannelBuilder<T>, |
| 99 | + _ => GuildChannelBuilder( |
| 100 | + name: name, |
| 101 | + type: type, |
| 102 | + position: position, |
| 103 | + permissionOverwrites: permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), |
| 104 | + ), |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +/// Extensions on [Thread]s. |
| 109 | +extension ThreadExtensions on Thread { |
| 110 | + /// Same as [listThreadMembers], but has no limit on the number of members returned. |
| 111 | + /// |
| 112 | + /// {@macro paginated_endpoint_streaming_parameters} |
| 113 | + Stream<ThreadMember> streamThreadMembers({bool? withMembers, Snowflake? after, Snowflake? before, int? pageSize}) => |
| 114 | + manager.streamThreadMembers(id, withMembers: withMembers, after: after, before: before, pageSize: pageSize); |
| 115 | +} |
| 116 | + |
| 117 | +/// Extensions on [GuildCategory]s. |
| 118 | +extension GuildCategoryExtensions on GuildCategory { |
| 119 | + /// Fetch the channels in this category. |
| 120 | + Future<List<GuildChannel>> fetchChannels() async { |
| 121 | + final guildChannels = await guild.fetchChannels(); |
| 122 | + |
| 123 | + return guildChannels.where((element) => element.parentId == id).toList(); |
| 124 | + } |
| 125 | + |
| 126 | + /// Return a list of channels in the client's cache that are in this category. |
| 127 | + List<GuildChannel> get cachedChannels => guild.cachedChannels.where((element) => element.parentId == id).toList(); |
| 128 | +} |
0 commit comments