Files
Parasha/SvetoforVKBot/Models/Commands/Registration/Date/GetDRYearCommand.cs
2021-10-19 06:04:52 +03:00

84 lines
3.1 KiB
C#

using SvetoforVKBot.Models.Updates;
using System;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using VkNet;
using VkNet.Enums.SafetyEnums;
using VkNet.Model.Keyboard;
using VkNet.Model.RequestParams;
namespace SvetoforVKBot.Models.Commands.Registration
{
public class GetDRYearCommand
{
public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
{
var chatId = update.@object.message.from_id;
MessagesSendParams @params = new MessagesSendParams();
var keyboardBuilder = new KeyboardBuilder().Clear();
int i = 0;
int year = 0;
Regex regex = new Regex("[^0-9]");
try
{
if (regex.Replace(update.@object.message.text, "").Length == 4)
year = Convert.ToInt32(regex.Replace(update.@object.message.text, ""));
if (year > 1920 && year < DateTime.Now.Year)
{
var user = db.Users.Single(usr => usr.chatId == chatId);
user.tag = "Подписчик - Регистрация - ДР - Месяц";
db.SaveChanges();
for (i = 1; i <= 12; i++)
{
keyboardBuilder
.AddButton(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i), "selectDRMonth-" + year + "-" + i, KeyboardButtonColor.Primary);
if (i % 3 == 0)
keyboardBuilder.AddLine();
}
@params.Keyboard = keyboardBuilder
.Build();
@params.Message = "Выберите месяц своего рождения на клавиатуре. \n";
@params.UserId = chatId;
@params.RandomId = GetRandomId();
client.Messages.SendAsync(@params);
}
else
{
@params.Message = "Данные введены некорректно 😕 Введите, пожалуйста, текст в формате 1234 (4 цифры).";
@params.UserId = chatId;
@params.RandomId = GetRandomId();
client.Messages.SendAsync(@params);
}
}
catch (Exception ee)
{
@params.Message = "‼Ошибка в GetDRYearCommand: " + ee.Message;
@params.Attachments = null;
@params.Keyboard = null;
@params.UserId = 59111081;
@params.RandomId = GetRandomId();
client.Messages.SendAsync(@params);
}
}
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
public int GetRandomId()
{
var intBytes = new byte[4];
Rng.GetBytes(intBytes);
return BitConverter.ToInt32(intBytes, 0);
}
}
}