示例#1
0
        public static bool CheckMemberPermission(long userID, long chatID, ChatMemberPerms perm)
        {
            ChatCache chat = ChatCaching.GetCache(chatID);

            if ((EpochTime() - ToEpoch(chat.LastUpdate)) >= Configs.RunningConfig.ChatCacheTimer)
            {
                ChatCaching.UpdateCache(chat);
            }

            if (chat.Admins.ContainsKey(userID))
            {
                ChatMember admin = chat.Admins[userID];
                if (admin.status == "creator")
                {
                    return(true);
                }
                if (admin.status == "administrator")
                {
                    return(PermCheck(admin, perm));
                }
            }
            else
            {
                Result <ChatMember> memberResult = Methods.getChatMember(chatID, userID);
                if (!memberResult.ok)
                {
                    Logger.LogWarn("There was an error while getting the permission for " + userID + " in chat " + chatID + "\nError: (" + memberResult.errorCode + ") Description: " + memberResult.description);
                    return(false);
                }
                else
                {
                    ChatMember member = memberResult.result;
                    if (!member.is_member)
                    {
                        return(false);
                    }
                    if (member.status == "left")
                    {
                        return(false);
                    }
                    if (member.status == "kicked")
                    {
                        return(false);
                    }
                    if (member.status == "restricted")
                    {
                        return(PermCheck(member, perm));
                    }
                    if (member.status == "member")
                    {
                        return(PermCheck(member, perm));
                    }
                }
            }
            return(false);
        }
示例#2
0
        public static bool isChatOwner(long userID, long chatID)
        {
            ChatCache cChat = ChatCaching.GetCache(chatID);

            if (cChat == null)
            {
                return(false);
            }
            if (cChat.owner_id == userID)
            {
                return(true);
            }
            return(false);
        }
示例#3
0
        public static bool isChatAdmin(long userID, long chatID)
        {
            ChatCache chat = ChatCaching.GetCache(chatID);

            if ((EpochTime() - ToEpoch(chat.LastUpdate)) >= Configs.RunningConfig.ChatCacheTimer)
            {
                ChatCaching.UpdateCache(chat);
            }

            if (chat.Admins.ContainsKey(userID))
            {
                ChatMember admin = chat.Admins[userID];
                if (admin.status == "creator" || admin.status == "administrator")
                {
                    return(true);
                }
            }
            return(false);
        }
示例#4
0
        internal static bool Commands(Message msg)
        {
            string[] cmd = msg.text.Split(' ');
            switch (isCommand(cmd[0]))
            {
            case "version":
            {
                TimeSpan t = TimeSpan.FromSeconds(Utilities.EpochTime() - MainClass.LauchTime);

                string answer = string.Format("{0:D3} Days, {1:D2} Hours, {2:D2} Minutes, {3:D2} Seconds", t.Days, t.Hours, t.Minutes, t.Seconds);
                Methods.sendReply(msg.chat.id, (int)msg.message_id, "Dread Bot " + Configs.Version + "\n\nUptime: " + answer);
                return(true);
            }

            case "updatecache":
            {
                if (Utilities.isChatAdmin(msg.from.id, msg.chat.id))
                {
                    ChatCache chat = ChatCaching.GetCache(msg.chat.id);
                    if ((Utilities.EpochTime() - Utilities.ToEpoch(chat.LastUpdate)) >= 3600)
                    {
                        ChatCaching.UpdateCache(chat);
                        chat.LastForcedUpdate = DateTime.Now;
                        ChatCaching.Save(chat);
                        Methods.sendReply(msg.chat.id, msg.message_id, "The Cache for the group has been updated. You will be unable to use this command again for atleast 1 hour.");
                    }
                    else
                    {
                        Methods.sendReply(msg.chat.id, msg.message_id, "You can use this command only once per hour.");
                    }
                }
                return(true);
            }

            case "adminlist":
            {
                if (msg.chat.type == "group" || msg.chat.type == "supergroup")
                {
                    ChatCache chat = ChatCaching.GetCache(msg.chat.id);
                    if ((Utilities.EpochTime() - Utilities.ToEpoch(chat.LastUpdate)) >= Configs.RunningConfig.ChatCacheTimer)
                    {
                        ChatCaching.UpdateCache(chat);
                    }

                    StringBuilder sb = new StringBuilder();
                    sb.Append("👑 Creator\n");
                    List <string> Admins = new List <string>();
                    int           i      = 0;
                    foreach (ChatMember admin in chat.Admins.Values)
                    {
                        if (admin.status == "creator")
                        {
                            sb.Append("└" + admin.user.first_name + "\n\n");
                        }
                        else
                        {
                            i++;
                            Admins.Add(admin.user.first_name);
                        }
                    }

                    sb.Append("👮‍♂️ Admins (" + i + ")\n");
                    int j = 0;
                    foreach (string name in Admins)
                    {
                        if (i == j)
                        {
                            sb.Append("└ " + name);
                        }
                        else
                        {
                            sb.Append("├" + name + "\n");
                        }
                        j++;
                    }
                    Methods.sendReply(msg.chat.id, msg.message_id, sb.ToString());
                }
                return(true);
            }

            case "token":
            {
                if (msg.chat.type != "private")
                {
                    return(false);
                }
                if (cmd.Length == 2)
                {
                    if (Utilities.OwnerToken == cmd[1])
                    {
                        StringBuilder sb = new StringBuilder();

                        Utilities.OwnerToken        = "";
                        Configs.RunningConfig.Owner = msg.from.id;

                        sb.Append("Ownership of this bot has been claimed by " + msg.from.id + "\n");

                        if (!String.IsNullOrEmpty(msg.from.username))         //username can be null
                        {
                            sb.Append("Username: @" + msg.from.username + "\n");
                        }
                        else
                        {
                            sb.Append("Username: -none-\n");
                        }

                        Methods.sendReply(msg.from.id, msg.message_id, "You have claimed ownership over this bot.\nPlease check out the [Wiki](http://dreadbot.net/wiki/) on getting me setup.\n\nFrom this point on, please use the admin command $adminmenu for DreadBot specific configuration.");

                        Configs.RunningConfig.GULimit   = 100;
                        Configs.RunningConfig.AdminChat = msg.from.id;

                        Logger.LogAdmin(sb.ToString());

                        Database.SaveConfig();
                    }

                    else if (Utilities.AdminTokens.Contains(cmd[1]))
                    {
                        StringBuilder sb = new StringBuilder();

                        if (Configs.RunningConfig.Admins.Contains(msg.from.id))
                        {
                            sb.Append("You are already an admin of this bot. No need to use a token.");
                            Methods.sendReply(msg.from.id, msg.message_id, sb.ToString());
                            Logger.LogAdmin("User tried to validate a token, despite already being an admin: " + msg.from.id);
                            return(true);
                        }
                        Utilities.AdminTokens.Remove(cmd[1]);
                        Configs.RunningConfig.Admins.Add(msg.from.id);
                        sb.Append(msg.from.first_name + " is now an admin of @" + Configs.Me.username);
                        Methods.sendReply(msg.from.id, msg.message_id, "You are now an admin. Welcome. :)");
                        Logger.LogAdmin(sb.ToString());
                        Methods.sendMessage(Configs.RunningConfig.AdminChat, sb.ToString());
                        return(true);
                    }

                    else
                    {
                        Methods.sendReply(msg.from.id, (int)msg.message_id, "The token you have specified does not exist. This error has been logged.");
                        Result <Message> res = Methods.sendMessage(Configs.RunningConfig.AdminChat, "The token command was attempted by ([" + msg.from.id + "](tg://user?id=" + msg.from.id + ")) using the token " + cmd[1]);
                        if (res == null || !res.ok)
                        {
                            Logger.LogError("Error contacting the admin Chat: " + res.description);
                        }
                        return(true);
                    }
                }
                return(true);
            }

            default: return(false);
            }
        }
示例#5
0
        static void Main()
        {
            #region Bot Initialization Phase

            AppDomain.CurrentDomain.ProcessExit += new EventHandler(ExitCleanUp);

            Database.Init();

            Result <User> res = null;
            while (res == null)
            {
                res = Methods.getMe();
                if (res == null)
                {
                    Logger.LogError("Error getting bot info. Reattempting..");
                }
                else if (!res.ok)
                {
                    Logger.LogFatal("Error getting bot info (" + res.errorCode + ") " + res.description);
                    Logger.LogFatal("Press anykey to terminate...");
                    Console.ReadKey();
                    Environment.Exit(-1);
                }
            }

            Configs.Me = res.result;
            Result <WebhookInfo> webres = Methods.getWebhookInfo();
            if (!webres.ok)
            {
                Logger.LogFatal("Error getting bot info: " + webres.description);
                Environment.Exit(-1);
            }
            Configs.webhookinfo = webres.result;

            ChatCaching.Init();

            PluginManager.Init();

            Cron.CronInit();

            if (Configs.RunningConfig.Owner == 0)
            {
                Console.WriteLine("There is no owner assigned to this bot.\r\nPlease send me the command below to claim ownership.\r\nThis token will be deleted once you use it.\r\n\r\n");
                Console.WriteLine("/token " + Utilities.CreateAdminToken(true));
            }

            if (Configs.RunningConfig.AdminChat < 0 || Configs.RunningConfig.AdminChat > 0)
            {
                string text = "**DreadBot Started**\n" + Utilities.NormalTime(Utilities.EpochTime()) + "\n\n" + PluginManager.getPluginList();
                Methods.sendMessage(Configs.RunningConfig.AdminChat, text);
            }

            Console.Title = "DreadBot v" + Configs.Version + " @" + Configs.Me.username;
            Console.WriteLine(PluginManager.getPluginList() + "\n");
            Console.WriteLine("DreadBot Loaded, and Started!\n");

            if (!String.IsNullOrEmpty(Configs.webhookinfo.url))
            {
                Configs.RunningConfig.GetupdatesMode = false;
            }                                                                                                     //WebHook is enabled. Launch in Webhook mode.

            #endregion

            while (true)
            {
                #region Main GetUpdates Loop

                if (Configs.RunningConfig.GetupdatesMode) //GetUpdates Mode
                {
                    Update[] updates = null;
                    if (UpdateId == 0)
                    {
                        if (Configs.webhookinfo.pending_update_count > 3)
                        {
                            Logger.LogInfo("Playing catchup; " + Configs.webhookinfo.pending_update_count + " updates behind.");
                        }
                        updates = Methods.getFirstUpdates(60);
                        if (updates == null || updates.Length < 1)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        GetUpdates request = new GetUpdates()
                        {
                            timeout = 20,
                            offset  = UpdateId + 1,
                            limit   = Configs.RunningConfig.GULimit
                        };
                        Result <Update[]> updatesres = Methods.getUpdates(request);
                        if (updatesres == null || !updatesres.ok)
                        {
                            Logger.LogError("Error fetching updates: (" + updatesres.errorCode + ") " + updatesres.description);
                            Thread.Sleep(10000);
                            continue;
                        }
                        updates = updatesres.result;
                    }
                    if (updates.Length < 1)
                    {
                        continue;
                    }
                    foreach (Update update in updates)
                    {
                        UpdateId = update.update_id;
                        //Console.WriteLine("Parsing Update: " + UpdateId);
                        Events.ParseUpdate(update);
                    }
                }

                #endregion

                #region WebHook Loop

                else // Webhook mode
                {
                    //// Webhook Not Implemented.
                    Logger.LogError("DreadBot Cannot continue:\n\nWebhook Mode is enabled for this bot. This version of DreadBot does not have Webhook support and will close.\n\n");
                    ExitCleanUp(null, null);
                    Thread.Sleep(10000);
                    throw new NotImplementedException("This version of DreadBot does not have Webhook support and will terminate.");
                }

                #endregion
            }
        }
示例#6
0
 private static void ParseMessage(Message msg, bool isEdited = false, bool isChannel = false)
 {
     if (msg.forward_from != null)
     {
         Events.OnForward(msg);
     }
     else if (!String.IsNullOrEmpty(msg.text))
     {
         if (isChannel)
         {
             OnChannelPost(msg, isEdited); return;
         }
         if (Utilities.isAdminCommand(msg.text) != "")
         {
             if (Utilities.AdminCommand(msg))
             {
                 return;
             }
         }
         else if (Utilities.isCommand(msg.text) != "")
         {
             if (Utilities.Commands(msg))
             {
                 return;
             }
         }
         Events.OnText(msg, isEdited);
     }
     else if (msg.sticker != null)
     {
         Events.OnSticker(msg);
     }
     else if (msg.photo != null)
     {
         Events.OnImage(msg, isEdited);
     }
     else if (msg.video_note != null)
     {
         Events.OnVideoNote(msg, isEdited);
     }
     else if (msg.new_chat_members != null)
     {
         foreach (User user in msg.new_chat_members)
         {
             if (user.id == Configs.Me.id)
             {
                 ChatCache cChat = ChatCaching.GetCache(msg.chat.id);
                 ChatCaching.UpdateCache(cChat);
                 break;
             }
         }
         Events.OnJoin(msg);
     }
     else if (msg.left_chat_member != null)
     {
         Events.OnPart(msg);
     }
     else if (msg.animation != null)
     {
         Events.OnAnimation(msg, isEdited);
     }
     else if (msg.audio != null)
     {
         Events.OnAudio(msg, isEdited);
     }
     else if (msg.video != null)
     {
         Events.OnVideo(msg, isEdited);
     }
     else if (msg.game != null)
     {
         Events.OnGame(msg, isEdited);
     }
     else if (msg.poll != null)
     {
         Events.OnPoll(msg.poll, msg);
     }
     else if (msg.dice != null)
     {
         Events.OnDice(msg);
     }
     else if (msg.voice != null)
     {
         Events.OnVoiceClip(msg, isEdited);
     }
     else if (msg.venue != null)
     {
         Events.OnVenueClip(msg, isEdited);
     }
     else if (msg.pinned_message != null)
     {
         ChatCaching.UpdatePinnedMsg(msg);
         Events.OnPinnedMessage(msg, isEdited);
     }
     else if (msg.new_chat_title != null)
     {
         ChatCaching.UpdateTitle(msg);
         Events.OnTitleChange(msg);
     }
     else if (msg.new_chat_photo != null || (msg.delete_chat_photo))
     {
         ChatCaching.UpdatePhoto(msg);
         Events.OnChatPhoto(msg);
     }
     else if (msg.location != null)
     {
         Events.OnLocation(msg);
     }
     else if (msg.group_chat_created)
     {
         ChatCache cChat = ChatCaching.GetCache(msg.chat.id);
         ChatCaching.UpdateCache(cChat);
         Events.OnNewGroup(msg);
     }
     else if (msg.passport_data != null)
     {
         Events.OnPassportData(msg);
     }
     else if (msg.migrate_from_chat_id != 0)
     {
         ChatCaching.ChatUpgrade(msg.migrate_from_chat_id, msg.chat.id);
         Events.OnGroupUpgrade(msg);
     }
 }