Beispiel #1
0
 private void AdminTaskerTimerElapsed(object sender, ElapsedEventArgs e)
 {
     TasksTimer.Stop();
     if (Tasks.Count > 0)
     {
         AdminTask task = Tasks.Dequeue();
         if (task.DelayUntil < DateTime.UtcNow)
         {
             PerformTask(task);
         }
         else
         {
             Enqueue(task);
         }
     }
     TasksTimer.Start();
 }
Beispiel #2
0
        private void PerformTask(AdminTask task)
        {
            try
            {
                switch (task.TaskType)
                {
                case AdminTaskType.ChatlinkChange:
                    (task as ChangeChatLinkAdminTask).SetChatLink(Bot.ExportChatInviteLinkAsync(task.ChatId).Result);
                    break;

                case AdminTaskType.ChatmemberCount:
                    (task as ChatmemberCountAdminTask).SetCount(Bot.GetChatMemberCountAsync(task.ChatId).Result);
                    break;

                case AdminTaskType.ChatmemberInfo:
                {
                    var cm = Bot.GetChatMemberAsync(task.ChatId, (task as ChatmemberInfoAdminTask).UserId).Result;
                    (task as ChatmemberInfoAdminTask).FillUserInfo(cm.User.FirstName, cm.User.LastName, cm.User.Username, cm.Status);
                }
                break;

                case AdminTaskType.ChatmemberKick:
                    Bot.BanChatMemberAsync(task.ChatId, (task as ChatmemberKickAdminTask).UserId);
                    break;

                case AdminTaskType.ChatmemberStatus:
                {
                    var cm = Bot.GetChatMemberAsync(task.ChatId, (task as ChatmemberMembershipAdminTask).UserId).Result;
                    if (cm.Status == ChatMemberStatus.Administrator ||
                        cm.Status == ChatMemberStatus.Creator ||
                        cm.Status == ChatMemberStatus.Member ||
                        (cm.Status == ChatMemberStatus.Restricted && (cm as ChatMemberRestricted).IsMember))
                    {
                        (task as ChatmemberMembershipAdminTask).SetMember();
                    }
                }
                break;

                case AdminTaskType.UnrestrictChatmember:
                    Bot.UnbanChatMemberAsync(task.ChatId, (task as UnrestrictChatmemberAdminTask).UserId);
                    break;

                case AdminTaskType.ChatInfo:
                {
                    Chat c = Bot.GetChatAsync(task.ChatId).Result;
                    (task as ChatInfoAdminTask).SetChatInfo(c.Title, c.InviteLink, c.Username);
                }
                break;

                case AdminTaskType.GetChatAdmins:
                {
                    ChatMember[] Members   = Bot.GetChatAdministratorsAsync(task.ChatId).Result;
                    List <long>  AdminList = new List <long>();
                    foreach (ChatMember member in Members)
                    {
                        AdminList.Add(member.User.Id);
                    }
                    (task as GetChatAdminsAdminTask).SetAdmins(AdminList);
                }
                break;
                }
                task.SetComplete();
            }
            catch
            (Exception err)
            {
                task.SetError();
                BotUtils.LogException(err);
            }
            {
                EventHandler <AdminTaskCompleteEventArgs> handler = AdminTaskComplete;
                handler.Invoke(this, new AdminTaskCompleteEventArgs(task));
            }
        }
Beispiel #3
0
 public void Enqueue(AdminTask task)
 {
     Tasks.Enqueue(task);
 }