From 014f563b899c539678f512be7f5e1641e5c2f1d3 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Sat, 7 Jan 2023 13:22:40 +0100 Subject: [PATCH] added Channel.MainUsername helper | simplified GetAllChats example --- Examples/Program_GetAllChats.cs | 4 ++-- README.md | 16 +++------------- src/TL.Helpers.cs | 1 + 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Examples/Program_GetAllChats.cs b/Examples/Program_GetAllChats.cs index b39e31f..02fc287 100644 --- a/Examples/Program_GetAllChats.cs +++ b/Examples/Program_GetAllChats.cs @@ -34,10 +34,10 @@ namespace WTelegramClientTest foreach (var (id, chat) in chats.chats) switch (chat) { - case Chat smallgroup when (smallgroup.flags & Chat.Flags.deactivated) == 0: + case Chat smallgroup when smallgroup.IsActive: Console.WriteLine($"{id}: Small group: {smallgroup.title} with {smallgroup.participants_count} members"); break; - case Channel channel when (channel.flags & Channel.Flags.broadcast) != 0: + case Channel channel when channel.IsChannel: Console.WriteLine($"{id}: Channel {channel.username}: {channel.title}"); //Console.WriteLine($" → access_hash = {channel.access_hash:X}"); break; diff --git a/README.md b/README.md index 1c342a3..e40d404 100644 --- a/README.md +++ b/README.md @@ -129,18 +129,8 @@ using TL; var chats = await client.Messages_GetAllChats(); Console.WriteLine("This user has joined the following:"); foreach (var (id, chat) in chats.chats) - switch (chat) // example of downcasting to their real classes: - { - case Chat basicChat when basicChat.IsActive: - Console.WriteLine($"{id}: Basic chat: {basicChat.title}"); - break; - case Channel group when group.IsGroup: - Console.WriteLine($"{id}: Group {group.username}: {group.title}"); - break; - case Channel channel: - Console.WriteLine($"{id}: Channel {channel.username}: {channel.title}"); - break; - } + if (chat.IsActive) + Console.WriteLine($"{id,10}: {chat}"); Console.Write("Type a chat ID to send a message: "); long chatId = long.Parse(Console.ReadLine()); var target = chats.chats[chatId]; @@ -149,7 +139,7 @@ await client.SendMessageAsync(target, "Hello, World"); ``` ➡️ You can find lots of useful code snippets in [EXAMPLES](https://wiz0u.github.io/WTelegramClient/EXAMPLES) -and in the [Examples subdirectory](https://github.com/wiz0u/WTelegramClient/tree/master/Examples). +and more detailed programs in the [Examples subdirectory](https://github.com/wiz0u/WTelegramClient/tree/master/Examples). ➡️ Check [the FAQ](https://wiz0u.github.io/WTelegramClient/FAQ#compile) if example codes don't compile correctly on your machine, or other troubleshooting. diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index 625fdae..50689e1 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -216,6 +216,7 @@ namespace TL partial class Channel { public override bool IsActive => (flags & Flags.left) == 0; + public string MainUsername => username ?? usernames?.FirstOrDefault(u => u.flags.HasFlag(Username.Flags.active))?.username; public override ChatPhoto Photo => photo; public override bool IsBanned(ChatBannedRights.Flags flags = 0) => ((banned_rights?.flags ?? 0) & flags) != 0 || ((default_banned_rights?.flags ?? 0) & flags) != 0; public override InputPeer ToInputPeer() => new InputPeerChannel(id, access_hash);