Example #1
0
        private void LoadBaths()
        {
            baths = new Dictionary <long, Bath>();
            if (!Directory.Exists("baths"))
            {
                Directory.CreateDirectory("baths");
            }

            FileInfo[] files = new DirectoryInfo("baths").GetFiles("*.json");
            foreach (FileInfo file in files)
            {
                FileStream   fs = null;
                StreamReader sr = null;

                try
                {
                    fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
                    sr = new StreamReader(fs);
                    Bath bath = JsonConvert.DeserializeObject <Bath>(sr.ReadToEnd());

                    baths[Convert.ToInt64(file.Name.Replace(file.Extension, string.Empty))] = bath;
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    sr?.Close();
                    fs?.Close();
                }
            }
        }
Example #2
0
        public void HowMuch(object sender, Message message)
        {
            if (!baths.ContainsKey(message.Chat.Id) || baths[message.Chat.Id].from == DateTime.MinValue)
            {
                bot.SendTextMessageAsync(
                    message.Chat.Id,
                    lexer.GetPhrase("unknownTime", cockyLevel, message.From),
                    ParseMode.Markdown);
                return;
            }

            Bath          bath = baths[message.Chat.Id];
            TimeSpan      time = bath.HowMuch();
            List <string> msg  = new List <string> {
                $"До баньки осталось {time.ToString(@"dd\:hh\:mm\:ss")}\n",
                $"В секундах: {(int)time.TotalSeconds}",
                $"В минутах: {(int)time.TotalMinutes}",
                $"В часах: {(int)time.TotalHours}",
                $"В днях: {(int)time.TotalDays}"
            };

            bot.SendTextMessageAsync(
                message.Chat.Id,
                string.Join("\n", msg));
        }
Example #3
0
        private void SaveBath(Bath bath, long chatId)
        {
            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                fs = new FileStream($"baths\\{chatId}.json", FileMode.OpenOrCreate, FileAccess.Write);
                sw = new StreamWriter(fs);
                sw.Write(JsonConvert.SerializeObject(bath));
            }
            catch (Exception ex)
            {
            }
            finally
            {
                sw?.Close();
                fs?.Close();
            }
        }
Example #4
0
        public void SetTimer(object sender, Message message)
        {
            try
            {
                ChatMember member = bot.GetChatMemberAsync(message.Chat.Id, message.From.Id).Result;
                if (member.Status != ChatMemberStatus.Administrator && member.Status != ChatMemberStatus.Creator)
                {
                    bot.SendTextMessageAsync(
                        message.Chat.Id,
                        lexer.GetPhrase("notAdmin", cockyLevel, message.From));
                    cockyLevel = Math.Min(cockyLevel + 5, 100);
                    return;
                }

                string[] dates = message.Text.Split(' ');
                Bath     bath  = new Bath {
                    from = DateTime.ParseExact(dates[0], "yyyyMMddHHmmss", CultureInfo.InvariantCulture),
                    to   = DateTime.ParseExact(dates[1], "yyyyMMddHHmmss", CultureInfo.InvariantCulture)
                };

                // Сохраняется в файле, если вдруг бот перезапускается.
                SaveBath(bath, message.Chat.Id);

                baths[message.Chat.Id] = bath;

                bot.SendTextMessageAsync(
                    message.Chat.Id,
                    lexer.GetPhrase("setComplete", cockyLevel, message.From));

                SendSticker(message, "ok");
            }
            catch
            {
                bot.SendTextMessageAsync(
                    message.Chat.Id,
                    lexer.GetPhrase("wrongDate", cockyLevel, message.From));

                SendSticker(message, "yobana");
                cockyLevel = Math.Min(cockyLevel + 5, 100);
            }
        }