diff --git a/Justice/Commands/Passports.cs b/Justice/Commands/Passports.cs index f0a436e..318dc22 100644 --- a/Justice/Commands/Passports.cs +++ b/Justice/Commands/Passports.cs @@ -19,6 +19,7 @@ namespace DiscordApp.Justice.Commands var Components = new ComponentBuilder() .WithButton(new ButtonBuilder() { CustomId = "newPassport", Label = "Новый паспорт", Style = ButtonStyle.Primary }) .WithButton(new ButtonBuilder() { CustomId = "reworkPassport", Label = "Замена паспорта", Style = ButtonStyle.Primary }) + .WithButton(new ButtonBuilder() { CustomId = "BuyIdPassportButton", Label = "Покупка номера", Style = ButtonStyle.Primary }) .Build(); await Context.Channel.SendMessageAsync(embed: Embed, components: Components); await FollowupAsync("OK!");//, ephemeral: true); diff --git a/Justice/Interactions/PassportInteraction.cs b/Justice/Interactions/PassportInteraction.cs index 264d53c..eafadfd 100644 --- a/Justice/Interactions/PassportInteraction.cs +++ b/Justice/Interactions/PassportInteraction.cs @@ -27,7 +27,284 @@ namespace DiscordApp.Justice.Interactions => await Context.Interaction.RespondWithModalAsync("reworkpassportModal"); [ComponentInteraction("reNewPassportButton")] public async Task reNewPassportModal() => await Context.Interaction.RespondWithModalAsync("ReNewPassportModal"); + [ComponentInteraction("BuyIdPassportModal")] + public async Task buyIdPassport() => await Context.Interaction.RespondWithModalAsync("buyPassportId"); + [ComponentInteraction("reBuyIdPassportModal")] + public async Task reworkPassportWithId() => await Context.Interaction.RespondWithModalAsync("renewPassportWithIdModal"); + + [ModalInteraction("buyPassportId")] + public async Task createPassportInteraction(INewIdPassportModal modal) + { + await DeferAsync(true); + + string name = modal.NickName; + string RpName = modal.RPName; + int supporterInt = modal.Supporter; + + string gender = modal.Gender; + + Startup startup = new(); + + SocketGuildUser user = Context.Guild.GetUser(Context.User.Id); + Supporter supporter; + Random random = new(); + spworlds.Types.User spUser = await spworlds.Types.User.CreateUser(name); + var spUserData = await startup.getUserData(name); + DateTimeOffset toTime; + DateOnly birthDate; + int id; + long unixBirthDateTime; + string cityName; + string cardNumber; + bool isIntNewPassportId = int.TryParse(modal.newId, out id); + if (!isIntNewPassportId) + { + await FollowupAsync("Новый номер паспорта это не число!", ephemeral: true); + return; + } + if (modal.newId[0] == '0') + { + await FollowupAsync("Первой цифрой не может быть 0", ephemeral: true); + return; + } + if (Startup.appDbContext.Passport.FirstOrDefault(k => k.Id == id) != null) + { + await FollowupAsync("Паспорт с таким числом уже есть!", ephemeral: true); + return; + } + try + { + birthDate = DateOnly.FromDateTime(spUserData.createdAt); + unixBirthDateTime = DateTimeOffset.Parse(birthDate.ToString()).ToUnixTimeSeconds(); + if (birthDate.AddDays(14) < DateOnly.FromDateTime(DateTime.Now)) + { + await FollowupAsync($"Возможно, игрок {name} больше не новичек, и бесплатный паспорт ему не положен! Оформляю паспорт на месяц...", ephemeral: true); + toTime = DateTimeOffset.Now.AddMonths(2); + } + else + { + toTime = DateTimeOffset.Now.AddDays(14); + } + } + catch + { + await FollowupAsync($"Сайт вернул очень странную дату... Попробуйте позже, и напишите об этом <@945317832290336798>", ephemeral: true); + return; + } + + switch (supporterInt) + { + case 0: + supporter = Supporter.None; + break; + case 1: + supporter = Supporter.FirstLvl; + break; + case 2: + supporter = Supporter.SecondLvl; + break; + case 3: + supporter = Supporter.ThirdLvl; + break; + + default: + await FollowupAsync("Неправильно указан уровень благотворителя. Используйте числа от 0 до 3(в зависимости от уровня)", ephemeral: true); + return; + } + if (spUserData.city != null) + { + cityName = spUserData.city.name; + } + else + { + cityName = "Спавн"; + } + if (spUserData.cardsOwned.Count > 0) + { + cardNumber = spUserData.cardsOwned.First().number; + } + else + { + cardNumber = "Отсутствует"; + } + + Passport passport = new() + { + Employee = user.Id, + RpName = RpName, + Gender = gender, + Date = toTime.ToUnixTimeSeconds(), + birthDate = unixBirthDateTime, + Applicant = name, + Id = id, + Support = supporter + }; + + + var passportData = new EmbedFieldBuilder() + .WithName("Данные паспорта:") + .WithValue(@$" +Имя: {passport.Applicant} +РП Имя: {passport.RpName} +Айди: {id} +Благотворитель: {passport.Support} +Гендер: {passport.Gender} +Дата рождения: +Город: {cityName} +Номер карты: {cardNumber}") + .WithIsInline(true); + + var author = new EmbedAuthorBuilder() + .WithName(user.DisplayName) + .WithIconUrl(user.GetDisplayAvatarUrl()); + + var embed = new EmbedBuilder() + .WithTitle("**Новый паспорт**") + .AddField(passportData) + .AddField(new EmbedFieldBuilder().WithName("Составитель: ").WithValue(user.GlobalName).WithIsInline(true)) + .AddField(new EmbedFieldBuilder().WithName("Доступен до: ").WithValue($"").WithIsInline(true)) + .WithThumbnailUrl(spUser.GetSkinPart(SkinPart.face)) + .WithColor(Color.DarkBlue) + .WithAuthor(author) + .WithTimestamp(toTime) + .Build(); + + Reports report = new() + { + Employee = ((IGuildUser)Context.User).DisplayName, + type = ReportTypes.buyId + }; + await Startup.appDbContext.Reports.AddAsync(report); + await Startup.appDbContext.Passport.AddAsync(passport); + if (!RpName.StartsWith("test")) { await Startup.appDbContext.SaveChangesAsync(); } + await FollowupAsync($"ID для паспорта: {id}", embed: embed, ephemeral: true); + + var channel = Context.Guild.GetChannel(1108006685626355733) as ITextChannel; + + await channel.SendMessageAsync(embed: embed); + } + + [ModalInteraction("ReNewPassportWithIdModal")] + public async Task reCreatePassportIdInteraction(IReNewPassportWithIdModal modal) + { + await DeferAsync(true); + + int passportId; + int newId; + bool tryToParsePassport = int.TryParse(modal.Id, out passportId); + bool tryToParseNewId = int.TryParse(modal.newId, out newId); + if (!tryToParsePassport || !tryToParseNewId) { await FollowupAsync("Айди паспорта устаревший или новый айди не число, используйте кнопку \"Создать новый\" для создания паспорта", ephemeral: true); return; } + var passport = Startup.appDbContext.Passport.Where(x => x.Id == passportId).FirstOrDefault(); + if (passport == null ) { await FollowupAsync("ID паспорта не правильный, или не существует.", ephemeral: true); return; } + if (modal.newId[0] == '0') { await FollowupAsync("Такое нельзя, первая цифра 0 - бан", ephemeral: true); return; } + Startup startup = new(); + SocketGuildUser user = Context.Guild.GetUser(Context.User.Id); + Random random = new(); + var spUser = await spworlds.Types.User.CreateUser(passport.Applicant); + var spUserData = await startup.getUserData(passport.Applicant); + string cityName; + string cardNumber; + DateTimeOffset toTime; + if (DateTimeOffset.FromUnixTimeSeconds(passport.birthDate).AddDays(14) < DateTimeOffset.Now) + { + toTime = DateTime.Now.AddDays(_AddDays); + } + else + { + toTime = DateTime.Now.AddDays(14); + } + + if (spUserData.city != null) + { + cityName = spUserData.city.name; + } + else + { + cityName = "Спавн"; + } + if (spUserData.cardsOwned.Count > 0) + { + cardNumber = spUserData.cardsOwned.First().number; + } + else + { + cardNumber = "Отсутствует"; + } + + int id = newId; + long unixTime = toTime.ToUnixTimeSeconds(); + long nowUnixTime = DateTimeOffset.Now.ToUnixTimeSeconds(); + + passport.Id = id; + passport.Date = nowUnixTime; + + var report = new Reports() + { + Employee = Startup.sp.GetUser(Context.User.Id.ToString()).Result.Name, + type = ReportTypes.ChangePassportId + }; + + var passportData = new EmbedFieldBuilder() + .WithName("Данные паспорта:") + .WithValue(@$" +Имя: {passport.Applicant} +РП Имя: {passport.RpName} +Айди: {id} +Благотворитель: {passport.Support} +Гендер: {passport.Gender} +Дата рождения: +Город: {cityName} +Номер карты: {cardNumber}").WithIsInline(true); + + var author = new EmbedAuthorBuilder() + .WithName(user.DisplayName) + .WithIconUrl(user.GetDisplayAvatarUrl()); + + var embed = new EmbedBuilder() + .WithTitle("**Паспорт переделан**") + .AddField(passportData) + .AddField(new EmbedFieldBuilder().WithName("Составитель: ").WithValue(user.GlobalName).WithIsInline(true)) + .AddField(new EmbedFieldBuilder().WithName("Доступен до: ").WithValue($"").WithIsInline(true)) + .WithThumbnailUrl(spUser.GetSkinPart(SkinPart.face)) + .WithColor(Color.DarkBlue) + .WithAuthor(author) + .WithTimestamp(toTime) + .Build(); + + if (Startup.appDbContext.Passport.FindAsync(passport.Id).Result != null) + { + await FollowupAsync("Паспорт с таким ID существует!", ephemeral: true); + return; + } + await Startup.appDbContext.Reports.AddAsync(report); + await Startup.appDbContext.Passport.AddAsync(passport); + await Startup.appDbContext.SaveChangesAsync(); + await FollowupAsync($"ID для паспорта: {id}", embed: embed, ephemeral: true); + + var channel = Context.Guild.GetChannel(1108006685626355733) as ITextChannel; + + var message = await channel.SendMessageAsync(embed: embed); + + + } + + + [ComponentInteraction("BuyIdPassportButton")] + public async Task buyIdPassportButton() + { + await DeferAsync(true); + var Embed = new EmbedBuilder() + .WithTitle("**Выбери тип паспоррта!**") + .WithDescription("Только при покупке номера") + .WithColor(Color.Blue) + .Build(); + var Components = new ComponentBuilder() + .WithButton(new ButtonBuilder() { CustomId = "BuyIdPassportModal", Label = "Новый паспорт", Style = ButtonStyle.Primary }) + .WithButton(new ButtonBuilder() { CustomId = "reBuyIdPassportModal", Label = "Замена паспорта", Style = ButtonStyle.Primary }) + .Build(); + await FollowupAsync(embed: Embed, components: Components, ephemeral:true); + } [ModalInteraction("reworkpassportModal")] public async Task reCreatePassportInteraction(IReWorkPassportModal modal) @@ -81,8 +358,8 @@ namespace DiscordApp.Justice.Interactions cardNumber = "Отсутствует"; } - int id = random.Next(00001, 99999); - while (id.ToString().Length < 5) { id = random.Next(00001, 99999); } + int id = random.Next(10000, 99999); + while (id.ToString().Length < 5) { id = random.Next(10000, 99999); } long unixTime = toTime.ToUnixTimeSeconds(); long nowUnixTime = DateTimeOffset.Now.ToUnixTimeSeconds(); @@ -121,14 +398,21 @@ namespace DiscordApp.Justice.Interactions bool isUnical = false; while (!isUnical) { - id = random.Next(00001, 99999); + id = random.Next(10000, 99999); passport.Id = id; - while (id.ToString().Length < 5) { id = random.Next(00001, 99999); } + while (id.ToString().Length < 5) { id = random.Next(10000, 99999); } Console.WriteLine(passport.Id); if (Startup.appDbContext.Passport.FindAsync(passport.Id).Result == null) { break; } } } + var report = new Reports() + { + Employee = Startup.sp.GetUser(Context.User.Id.ToString()).Result.Name, + type = ReportTypes.editPassport + }; + + await Startup.appDbContext.Reports.AddAsync(report); await Startup.appDbContext.Passport.AddAsync(passport); await Startup.appDbContext.SaveChangesAsync(); await FollowupAsync($"ID для паспорта: {id}", embed: embed, ephemeral: true); @@ -139,6 +423,7 @@ namespace DiscordApp.Justice.Interactions } } + [ModalInteraction("ReNewPassportModal")] public async Task renewPassportInteraction(INewPassportModal modal) { @@ -287,6 +572,7 @@ namespace DiscordApp.Justice.Interactions var message = await channel.SendMessageAsync(embed: embed); } + [ModalInteraction("passportModal")] public async Task createPassportInteraction(INewPassportModal modal) { @@ -307,8 +593,8 @@ namespace DiscordApp.Justice.Interactions var spUserData = await startup.getUserData(name); DateTimeOffset toTime; DateOnly birthDate; - int id = random.Next(00001, 99999); - while (id.ToString().Length < 5) { id = random.Next(00001, 99999); } + int id = random.Next(10000, 99999); + while (id.ToString().Length < 5) { id = random.Next(10000, 99999); } long unixBirthDateTime; string cityName; string cardNumber; @@ -385,8 +671,8 @@ namespace DiscordApp.Justice.Interactions bool isUnical = false; while (!isUnical) { - id = random.Next(00001, 99999); - while (id.ToString().Length < 5) { id = random.Next(00001, 99999); } + id = random.Next(10000, 99999); + while (id.ToString().Length < 5) { id = random.Next(10000, 99999); } passport.Id = id; Console.WriteLine(passport.Id); if (Startup.appDbContext.Passport.FindAsync(passport.Id).Result == null) { break; } diff --git a/Justice/Modals/PassportModals.cs b/Justice/Modals/PassportModals.cs index ad20c80..c86efe7 100644 --- a/Justice/Modals/PassportModals.cs +++ b/Justice/Modals/PassportModals.cs @@ -25,6 +25,46 @@ namespace DiscordApp.Justice.Modals } + public class INewIdPassportModal : IModal + { + public string Title => "Обновление номера"; + + [InputLabel("Ник игрока")] + [ModalTextInput("nickname", TextInputStyle.Short, placeholder: "YaFlay", maxLength: 90)] + public string NickName { get; set; } + + [InputLabel("Благотворитель")] + [ModalTextInput("Supporter", TextInputStyle.Short, placeholder: "1", maxLength: 5)] + public int Supporter { get; set; } + + [InputLabel("РП Имя")] + [ModalTextInput("rolePlayName", TextInputStyle.Short, placeholder: "Олег Бебров", maxLength: 200)] + public string RPName { get; set; } + + [InputLabel("Пол")] + [ModalTextInput("gender", TextInputStyle.Short, maxLength: 200)] + public string Gender { get; set; } + + [InputLabel("Новый номер паспорта")] + [ModalTextInput("id", TextInputStyle.Short, maxLength: 5)] + public string newId { get; set; } + + } + + public class IReNewPassportWithIdModal : IModal + { + public string Title => "Обновление номера"; + + + [InputLabel("Старый номер паспорта")] + [ModalTextInput("oldid", TextInputStyle.Short, maxLength: 5)] + public string Id { get; set; } + + [InputLabel("Новый номер паспорта")] + [ModalTextInput("id", TextInputStyle.Short, maxLength: 5)] + public string newId { get; set; } + } + public class IReWorkPassportModal : IModal { public string Title => "Создание паспорта"; diff --git a/Types/ReportTypes.cs b/Types/ReportTypes.cs index 2427027..244bc2a 100644 --- a/Types/ReportTypes.cs +++ b/Types/ReportTypes.cs @@ -9,6 +9,7 @@ PetPassport = 2, Marry = 4, BirthDocument = 4, - Patent = 2 + Patent = 2, + buyId = 64 } } diff --git a/Utilities/IdChecker.cs b/Utilities/IdChecker.cs index bb65982..1596a3a 100644 --- a/Utilities/IdChecker.cs +++ b/Utilities/IdChecker.cs @@ -7,8 +7,8 @@ namespace DiscordApp.Utilities public static void IdGenerator(out int id) { Random random = new(); - id = random.Next(00001, 99999); - while (id.ToString().Length < 5) { id = random.Next(00001, 99999); } + id = random.Next(10000, 99999); + while (id.ToString().Length < 5) { id = random.Next(10000, 99999); } return; } public static bool IsPassport(int id, out Passport passport)