Browse Source

add blocking users

Tigran 4 years ago
parent
commit
158cd1d14f

+ 50 - 0
MafiaTelegramBot/Commands/ChatMember/MyChatMemberHandler.cs

@@ -0,0 +1,50 @@
+using System.Threading.Tasks;
+using MafiaTelegramBot.DataBase.EntityDao;
+using MafiaTelegramBot.Game;
+using Telegram.Bot.Types;
+using Telegram.Bot.Types.Enums;
+
+namespace MafiaTelegramBot.Commands.ChatMember
+{
+    public class MyChatMemberHandler : UpdateHandler
+    {
+        public override Task<Message> Execute(Update update)
+        {
+            switch (update.MyChatMember.NewChatMember.Status)
+            {
+                case ChatMemberStatus.Creator:
+                    User.Info.IsBlocked = true;
+                    break;
+                case ChatMemberStatus.Administrator:
+                    User.Info.IsBlocked = true;
+                    break;
+                case ChatMemberStatus.Member:
+                    User.Info.IsBlocked = false;
+                    break;
+                case ChatMemberStatus.Left:
+                    User.Info.IsBlocked = true;
+                    break;
+                case ChatMemberStatus.Kicked:
+                    User.Info.IsBlocked = true;
+                    break;
+                case ChatMemberStatus.Restricted:
+                    User.Info.IsBlocked = true;
+                    break;
+            }
+            return Task.FromResult(new Message());
+        }
+
+        public static async Task<UpdateHandler> Factory(User user)
+        {
+            // Объект пользователя или канала
+            var player = await UserDao.GetPlayerById(user.Id) ?? await UserDao.AddNew(user);
+
+            // Устанавливаем игроку время активности на текущее
+            player.SetActive();
+            
+            return new MyChatMemberHandler(player);
+        }
+        
+        public MyChatMemberHandler(Player user) : base(user) { }
+    }
+}

+ 70 - 8
MafiaTelegramBot/Controllers/MessageController.cs

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
 using MafiaTelegramBot.Commands;
 using MafiaTelegramBot.Commands.CallbackQueries;
 using MafiaTelegramBot.Commands.ChannelPost;
+using MafiaTelegramBot.Commands.ChatMember;
 using MafiaTelegramBot.Commands.Messages;
 using MafiaTelegramBot.DataBase.Entity;
 using Telegram.Bot;
@@ -44,7 +45,7 @@ namespace MafiaTelegramBot.Controllers
                         await UpdateHandler.Factory(update.CallbackQuery.From, update.CallbackQuery.Data, new CallbackQueryHandler(null)),
                     UpdateType.ChannelPost =>
                         await UpdateHandler.Factory(ChatToUser(update.ChannelPost.Chat), update.ChannelPost.Text, new ChannelPost(null)),
-                    // UpdateType.ChatMember => ChatMember.Update(update),
+                    UpdateType.MyChatMember => await MyChatMemberHandler.Factory(update.MyChatMember.From),
                     _ => await UnknownUpdateHandlerAsync(update)
                 };
                 var unused = await handler.Execute(update);
@@ -78,37 +79,98 @@ namespace MafiaTelegramBot.Controllers
 
         public static async Task<Message> SendText(UserEntity info, string message, IReplyMarkup keyboard = null)
         {
-            return await Bot.Client.SendTextMessageAsync(info.ChatId, message, replyMarkup: keyboard, disableNotification: true);
+            try
+            {
+                if (!info.IsBlocked)
+                    return await Bot.Client.SendTextMessageAsync(info.ChatId, message, replyMarkup: keyboard, disableNotification: true);
+            }
+            catch (Exception e)
+            {
+                LogOutWarning("Can't send text message " + e);
+            }
+            return new Message();
         }
         
         public static async Task<Message> SendTextWithHtml(UserEntity info, string message, IReplyMarkup keyboard = null)
         {
-            return await Bot.Client.SendTextMessageAsync(info.ChatId, message, ParseMode.Html, replyMarkup: keyboard, disableNotification: true);
+            try
+            {
+                if (!info.IsBlocked)
+                    return await Bot.Client.SendTextMessageAsync(info.ChatId, message, ParseMode.Html, replyMarkup: keyboard, disableNotification: true);
+            }
+            catch (Exception e)
+            {
+                LogOutWarning("Can't send text message with html " + e);
+            }
+            return new Message();
         }
         
         public static async Task<Message> SendSticker(UserEntity info, string fileId)
         {
-            return await Bot.Client.SendStickerAsync(info.ChatId, fileId, true);
+            try
+            {
+                if (!info.IsBlocked)
+                    return await Bot.Client.SendStickerAsync(info.ChatId, fileId, true);
+            }
+            catch (Exception e)
+            {
+                LogOutWarning("Can't send sticker " + e);
+            }
+            return new Message();
         }
 
         public static async Task<Message> EditMessage(UserEntity info, int messageId, string message, InlineKeyboardMarkup keyboard = null)
         {
-            return await Bot.Client.EditMessageTextAsync(info.ChatId, messageId, message, replyMarkup: keyboard);
+            try
+            {
+                if (!info.IsBlocked)
+                    return await Bot.Client.EditMessageTextAsync(info.ChatId, messageId, message, replyMarkup: keyboard);
+            }
+            catch (Exception e)
+            {
+                LogOutWarning("Can't edit message text " + e);
+            }
+            return new Message();
         }
 
         public static async Task<Message> EditReplyMarkup(UserEntity info, int messageId, InlineKeyboardMarkup keyboard)
         {
-            return await Bot.Client.EditMessageReplyMarkupAsync(info.ChatId, messageId, keyboard);
+            try
+            {
+                if (!info.IsBlocked)
+                    return await Bot.Client.EditMessageReplyMarkupAsync(info.ChatId, messageId, keyboard);
+            }
+            catch (Exception e)
+            {
+                LogOutWarning("Can't edit reply markup " + e);
+            }
+            return new Message();
         }
         
         public static async Task DeleteMessage(long chatId, int messageId)
         {
-            await Bot.Client.DeleteMessageAsync(chatId, messageId);
+            try
+            {
+                await Bot.Client.DeleteMessageAsync(chatId, messageId);
+            }
+            catch (Exception e)
+            {
+                LogOutWarning("Can't delete message " + e);
+            }
         }
 
         public static async Task<Message> SendImage(UserEntity info, InputOnlineFile inputOnlineFile, string message, InlineKeyboardMarkup replyMarkup)
         {
-            return await Bot.Client.SendPhotoAsync(info.ChatId, inputOnlineFile, message, replyMarkup: replyMarkup);
+            try
+            {
+                if (!info.IsBlocked)
+                    return await Bot.Client.SendPhotoAsync(info.ChatId, inputOnlineFile, message, replyMarkup: replyMarkup);
+            }
+            catch (Exception e)
+            {
+                LogOutWarning("Can't send photo " + e);
+            }
+            return new Message();
         }
     }
 }

+ 2 - 1
MafiaTelegramBot/DataBase/EntityDao/UserDao.cs

@@ -50,7 +50,8 @@ namespace MafiaTelegramBot.DataBase.EntityDao
                 Id = user.Id,
                 ChatId = user.Id,
                 Username = user.Username,
-                NickName = user.FirstName + (user.FirstName != "" && user.LastName != "" ? " " : "") + user.LastName
+                NickName = user.FirstName + (user.FirstName != "" && user.LastName != "" ? " " : "") + user.LastName,
+                IsBlocked = user.IsBot
             };
             await DataBase.Users.AddAsync(newUser);
             return await GetPlayerById(user.Id);