From 8a567bc2cb53c803e99ac231f78eaf5335ed6edd Mon Sep 17 00:00:00 2001 From: doEggi <75576545+doEggi@users.noreply.github.com> Date: Wed, 10 Jul 2024 23:34:41 +0200 Subject: [PATCH] Add .toBuilder() method to GuildChannel (#39) Co-authored-by: Rapougnac --- lib/src/extensions/channel.dart | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/lib/src/extensions/channel.dart b/lib/src/extensions/channel.dart index fc30052..246228d 100644 --- a/lib/src/extensions/channel.dart +++ b/lib/src/extensions/channel.dart @@ -24,6 +24,87 @@ extension GuildChannelExtensions on GuildChannel { Future computePermissionsFor(PartialMember member) async => await computePermissions(this, await member.get()); } +/// Extensions on all [GuildChannel] types +extension GuildChannelsExtension on T { + /// Create a builder with the properties of this channel + GuildChannelBuilder toBuilder() => switch (this) { + GuildTextChannel channel => GuildTextChannelBuilder( + name: channel.name, + defaultAutoArchiveDuration: channel.defaultAutoArchiveDuration, + isNsfw: channel.isNsfw, + parentId: channel.parentId, + permissionOverwrites: channel.permissionOverwrites + .map((e) => PermissionOverwriteBuilder( + id: e.id, + type: e.type, + allow: e.allow, + deny: e.deny, + )) + .toList(), + position: channel.position, + rateLimitPerUser: channel.rateLimitPerUser, + topic: channel.topic, + ) as GuildChannelBuilder, + GuildVoiceChannel channel => GuildVoiceChannelBuilder( + name: channel.name, + permissionOverwrites: + channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), + position: channel.position, + isNsfw: channel.isNsfw, + parentId: channel.parentId, + bitRate: channel.bitrate, + rtcRegion: channel.rtcRegion, + userLimit: channel.userLimit, + videoQualityMode: channel.videoQualityMode, + ) as GuildChannelBuilder, + GuildStageChannel channel => GuildStageChannelBuilder( + name: channel.name, + bitRate: channel.bitrate, + isNsfw: channel.isNsfw, + parentId: channel.parentId, + permissionOverwrites: + channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), + position: channel.position, + rtcRegion: channel.rtcRegion, + userLimit: channel.userLimit, + videoQualityMode: channel.videoQualityMode, + ) as GuildChannelBuilder, + PrivateThread thread => ThreadBuilder.privateThread( + name: thread.name, + autoArchiveDuration: thread.autoArchiveDuration, + invitable: thread.isInvitable, + rateLimitPerUser: thread.rateLimitPerUser, + ) as GuildChannelBuilder, + PublicThread thread => ThreadBuilder.publicThread( + name: thread.name, + autoArchiveDuration: thread.autoArchiveDuration, + rateLimitPerUser: thread.rateLimitPerUser, + ) as GuildChannelBuilder, + GuildCategory category => GuildCategoryBuilder( + name: category.name, + permissionOverwrites: + category.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), + position: category.position, + ) as GuildChannelBuilder, + GuildAnnouncementChannel channel => GuildAnnouncementChannelBuilder( + name: channel.name, + defaultAutoArchiveDuration: channel.defaultAutoArchiveDuration, + isNsfw: channel.isNsfw, + parentId: channel.parentId, + permissionOverwrites: + channel.permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), + position: channel.position, + topic: channel.topic, + ) as GuildChannelBuilder, + _ => GuildChannelBuilder( + name: name, + type: type, + position: position, + permissionOverwrites: permissionOverwrites.map((e) => PermissionOverwriteBuilder(id: e.id, type: e.type, allow: e.allow, deny: e.deny)).toList(), + ), + }; +} + /// Extensions on [Thread]s. extension ThreadExtensions on Thread { /// Same as [listThreadMembers], but has no limit on the number of members returned.