internal static void Save(ChatCache cChat) { lock (chatCacheCol) { chatCacheCol.Upsert(cChat); } }
internal static void UpdatePhoto(Message msg) { ChatCache chat = GetCache(msg.chat.id); chat.PhotoHistory.Add(DateTime.Now, msg.new_chat_photo[0]); Save(chat); }
internal static void UpdateTitle(Message msg) { ChatCache chat = GetCache(msg.chat.id); chat.TitleHistory.Add(DateTime.Now, msg.new_chat_title); Save(chat); }
internal static void UpdatePinnedMsg(Message msg) { ChatCache chat = GetCache(msg.chat.id); chat.PinnedMessageHistory.Add(DateTime.Now, msg); Save(chat); }
internal static void ChatUpgrade(long from_chat_id, long to_chat_id) { ChatCache cChat = GetCache(from_chat_id); cChat.id = to_chat_id; Logger.LogDebug("Chat Upgraded (" + from_chat_id + " to " + to_chat_id + ") Title:" + cChat.title); Save(cChat); }
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); }
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); }
internal static void UpdateCache(ChatCache chat, bool force = false) { TimeSpan t = DateTime.Now - chat.LastUpdate; if ((t.TotalSeconds > Configs.RunningConfig.ChatCacheTimer) || force) { chat.LastUpdate = DateTime.Now; Result <Chat> Chat = null; Chat = Methods.getChat(chat.id); if (Chat != null) { if (!Chat.ok) { Logger.LogError("There was an error fetching the Chat info for " + chat.id + "\r\nReason: " + Chat.description); } else { if (!chat.DescriptionHistory.ContainsValue(Chat.result.description)) { chat.DescriptionHistory.Add(DateTime.Now, Chat.result.description); } chat.GlobalPerms = Chat.result.permissions; } } Result <ChatMember[]> Admins = null; Admins = Methods.getChatAdministrators(chat.id); if (Admins != null) { if (!Admins.ok) { Logger.LogError("There was an error fetching the Admin list for " + chat.id + "\r\nReason: " + Admins.description); } else { chat.Admins.Clear(); foreach (ChatMember member in Admins.result) { if (member.status == "creator") { chat.owner_id = member.user.id; } chat.Admins.Add(member.user.id, member); } } } Save(chat); Logger.LogDebug("Updated Cache for Chat: " + chat.id); } }
public static ChatCache GetCache(long ChatID) { if (GroupCache.ContainsKey(ChatID)) { UpdateCache(GroupCache[ChatID]); return(GroupCache[ChatID]); } else { Result <Chat> gChatResult = Methods.getChat(ChatID); if (!gChatResult.ok) { Logger.LogError("Could not getChat during Caching update. Chat Returned a null value."); return(null); } else { Chat gChat = gChatResult.result; ChatCache chat = new ChatCache(); chat.id = ChatID; chat.can_set_sticker_set = gChat.can_set_sticker_set; chat.description = gChat.description; chat.first_name = gChat.first_name; chat.invite_link = gChat.invite_link; chat.last_name = gChat.last_name; chat.permissions = gChat.permissions; chat.photo = gChat.photo; chat.pinned_message = gChat.pinned_message; chat.title = gChat.title; chat.type = gChat.type; chat.LastUpdate = new DateTime(1900, 1, 1); chat.Admins = new Dictionary <long, ChatMember>(); chat.TitleHistory = new SortedDictionary <DateTime, string>(); chat.PinnedMessageHistory = new SortedDictionary <DateTime, Message>(); chat.DescriptionHistory = new SortedDictionary <DateTime, string>(); chat.PhotoHistory = new SortedDictionary <DateTime, PhotoSize>(); chat.GlobalPerms = new ChatPermissions(); UpdateCache(chat); lock (GroupCache) { GroupCache.Add(ChatID, chat); } return(chat); } } }
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); }
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); } }
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); } }