From 7f0728ce1ad82dbdfbb084d69001ce4d1e6d9650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=A8=D0=B8?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD=D1=81=D0=BA=D0=B8=D0=B9?= Date: Fri, 3 Nov 2023 18:38:18 +0300 Subject: [PATCH] add timer-cnhager nickname --- Justice.cs | 31 ++++++++++++++++++++- Justice/Interactions/BiznessInteractions.cs | 12 ++++---- Justice/Interactions/PassportInteraction.cs | 10 ++++--- Justice/Interactions/PatentsInteractions.cs | 4 +-- Justice/Modals/PassportModals.cs | 4 +-- JusticeHandler.cs | 13 +++------ 6 files changed, 50 insertions(+), 24 deletions(-) diff --git a/Justice.cs b/Justice.cs index 3fb8a24..8abbef9 100644 --- a/Justice.cs +++ b/Justice.cs @@ -1,6 +1,7 @@ using Discord; using Discord.WebSocket; using DiscordApp; +using System.Timers; namespace DiscordApp { @@ -28,13 +29,41 @@ namespace DiscordApp client.Log += LogAsync; client.UserJoined += Justice.Events.Events.onJoinGuild; client.MessageReceived += Justice.Events.Events.onMessageCreate; + client.Ready += onReady; await client.LoginAsync(TokenType.Bot, "MTE2NjA3OTk3NjQ0NjEwMzYwMg.GAKOIo.4af972Wh11G0EF4O5tNYb7l-vt5OwMc4HPRnjE"); await client.StartAsync(); await interactionHandler.InitializeAsync(); await Task.Delay(Timeout.Infinite); } - + public async Task onReady() + { + ChangeNicknames(); + var myTimer = new System.Timers.Timer(6 * 60 * 60 * 1000); //calculate six hours in milliseconds + myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); + myTimer.Start(); + } + private async void OnTimedEvent(object sender, ElapsedEventArgs e) + { + Console.WriteLine("Started timer"); + await ChangeNicknames(); + } + private async Task ChangeNicknames() + { + IGuild guild = client.GetGuild(1107742957458685985); + IRole role = guild.GetRole(1136564585420304444); + var members = await guild.GetUsersAsync(); + var membersArray = members.ToArray(); + foreach (IGuildUser user in membersArray) + { + var spUser = await Startup.sp.GetUser(user.Id.ToString()); + if (spUser.IsPlayer()) + { + await user.ModifyAsync(func => { func.Nickname = spUser.Name; }); + await user.AddRoleAsync(role); + } + } + } private async Task LogAsync(LogMessage message) => Console.WriteLine(message.ToString()); } diff --git a/Justice/Interactions/BiznessInteractions.cs b/Justice/Interactions/BiznessInteractions.cs index 0fab448..ac07b16 100644 --- a/Justice/Interactions/BiznessInteractions.cs +++ b/Justice/Interactions/BiznessInteractions.cs @@ -10,15 +10,15 @@ namespace DiscordApp.Justice.Interactions { [ComponentInteraction("NewIndividualEntrepreneur")] public async Task AplyWork() - => await Context.Interaction.RespondWithModalAsync("newIndividualEterpreneur"); + => await Context.Interaction.RespondWithModalAsync("newIndividualEterpreneurModal"); [ComponentInteraction("NewBizness")] public async Task reCreatePassport() - => await Context.Interaction.RespondWithModalAsync("NewBizness"); + => await Context.Interaction.RespondWithModalAsync("NewBiznessModal"); - [ModalInteraction("newIndividualEterpreneur")] + [ModalInteraction("newIndividualEterpreneurModal")] public async Task newIndividualEterpreneur(INewIndividualEntrepreneur modal) { - await DeferAsync(true); + Passport? applicant = await Startup.appDbContext.Passport.FindAsync(int.Parse(modal.passportId)); if (applicant == null) { @@ -68,10 +68,10 @@ namespace DiscordApp.Justice.Interactions await channel.SendMessageAsync(embed: embed); } - [ModalInteraction("NewBizness")] + [ModalInteraction("NewBiznessModal")] public async Task newBizness(INewBizness modal) { - await DeferAsync(true); + Passport? applicant = await Startup.appDbContext.Passport.FindAsync(int.Parse(modal.passportId)); if (applicant == null) diff --git a/Justice/Interactions/PassportInteraction.cs b/Justice/Interactions/PassportInteraction.cs index 851b73b..97e7d9d 100644 --- a/Justice/Interactions/PassportInteraction.cs +++ b/Justice/Interactions/PassportInteraction.cs @@ -28,8 +28,9 @@ namespace DiscordApp.Justice.Interactions [ModalInteraction("reworkpassportModal")] public async Task reCreatePassportInteraction(IReWorkPassportModal modal) { - await DeferAsync(true); - double passportId = modal.Id; + + int passportId; + int.TryParse(modal.Id, out passportId); bool recreatePassport = modal.IsNewPassport == 1; if (recreatePassport) { @@ -37,6 +38,7 @@ namespace DiscordApp.Justice.Interactions } else { + if (passportId == null) { 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; } @@ -113,7 +115,7 @@ namespace DiscordApp.Justice.Interactions [ModalInteraction("ReNewPassportModal")] public async Task renewPassportInteraction(INewPassportModal modal) { - await DeferAsync(true); + string name = modal.NickName; string RpName = modal.RPName; int supporterInt = modal.Supporter; @@ -239,7 +241,7 @@ namespace DiscordApp.Justice.Interactions [ModalInteraction("passportModal")] public async Task createPassportInteraction(INewPassportModal modal) { - await DeferAsync(true); + string name = modal.NickName; string RpName = modal.RPName; int supporterInt = modal.Supporter; diff --git a/Justice/Interactions/PatentsInteractions.cs b/Justice/Interactions/PatentsInteractions.cs index e212bc5..ad42c68 100644 --- a/Justice/Interactions/PatentsInteractions.cs +++ b/Justice/Interactions/PatentsInteractions.cs @@ -18,7 +18,7 @@ namespace DiscordApp.Justice.Interactions [ModalInteraction("newArtCallback")] public async Task newArtModalInteraction(INewArtModal modal) { - await DeferAsync(true); + string name = modal.Name; string maps = modal.MapNumbers; string size = modal.Size; @@ -89,7 +89,7 @@ namespace DiscordApp.Justice.Interactions [ModalInteraction("newBookCallback")] public async Task newBookModalInteraction(INewBookModal modal) { - await DeferAsync(true); + string name = modal.Name; string janre = modal.Janre; string annotation = modal.Annotation; diff --git a/Justice/Modals/PassportModals.cs b/Justice/Modals/PassportModals.cs index c0af5dd..eb1bdde 100644 --- a/Justice/Modals/PassportModals.cs +++ b/Justice/Modals/PassportModals.cs @@ -33,8 +33,8 @@ namespace DiscordApp.Justice.Modals public string Title => "Создание паспорта"; [InputLabel("ID паспорта")] - [ModalTextInput("id", TextInputStyle.Short, placeholder: "82-777", maxLength: 7)] - public double Id { get; set; } + [ModalTextInput("id", TextInputStyle.Short, placeholder: "97166", maxLength: 7)] + public string Id { get; set; } [InputLabel("Новые данные(0/1)")] [ModalTextInput("isNewPassportData", TextInputStyle.Short, placeholder: "1 - да, 0 - нет", maxLength: 1, initValue: "0")] diff --git a/JusticeHandler.cs b/JusticeHandler.cs index 83c3f79..0c58610 100644 --- a/JusticeHandler.cs +++ b/JusticeHandler.cs @@ -2,6 +2,7 @@ using Discord.Interactions; using Discord.WebSocket; using DiscordApp.Database; +using Microsoft.VisualBasic; using System.Reflection; @@ -47,22 +48,16 @@ namespace DiscordApp try { var context = new SocketInteractionContext(client, interaction); - + //await context.Interaction.DeferAsync(true); var result = await handler.ExecuteCommandAsync(context, services); if (!result.IsSuccess) - switch (result.Error) - { - case InteractionCommandError.UnmetPrecondition: - break; - default: - break; - } + await interaction.RespondAsync($"Возникла какая-то ошибка: {result.Error}", ephemeral: true); } catch { if (interaction.Type is InteractionType.ApplicationCommand) - await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync()); + await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync()); ; } } }