From 1f02d92a1ed9fc113e78b396f2c713209d053ce1 Mon Sep 17 00:00:00 2001 From: AltronMaxX Date: Mon, 11 Dec 2023 21:34:08 +0400 Subject: [PATCH] wow, new update --- handlers/GoogleSheetsModule.cs | 2 +- handlers/NumberCountingModule.cs | 107 +++++++++++++------------------ 2 files changed, 45 insertions(+), 64 deletions(-) diff --git a/handlers/GoogleSheetsModule.cs b/handlers/GoogleSheetsModule.cs index 5c8e818..749f8fd 100644 --- a/handlers/GoogleSheetsModule.cs +++ b/handlers/GoogleSheetsModule.cs @@ -9,7 +9,7 @@ class GoogleSheetsHelper { static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets }; static readonly string ApplicationName = "Edenor Bot"; - static readonly string SpreadsheetId = "1LJw2eAYBN_hx9z1klto758D_D-UM3vR8Rlx-YZtqN08"; + static readonly string SpreadsheetId = "1iD0heGU3wCvTOVrgc3U5kU4d7u2Hu3cSjaVzZzzaLC4"; static readonly string sheet = "Ответы на форму (1)"; static SheetsService service; diff --git a/handlers/NumberCountingModule.cs b/handlers/NumberCountingModule.cs index a8edcfa..c7db12a 100644 --- a/handlers/NumberCountingModule.cs +++ b/handlers/NumberCountingModule.cs @@ -10,28 +10,27 @@ class NumberCountingModule public static async Task loadAll() { - if (lastNumber == 0) + if (lastNumber == 0 && lastUser == 0) { try { - lastNumber = getLastNumber(); + if (File.Exists(dir)) + { + StreamReader sr = new StreamReader(dir); + string text = sr.ReadToEnd(); + sr.Close(); + lastNumber = Convert.ToInt64(text.Split(":")[0]); + lastUser = Convert.ToInt64(text.Split(":")[1]); + } + else + { + return; + } } catch (Exception e) { - Program.logError("Error while setting up last number " + e.Message); + await Program.logError("Error while setting up last number and user" + e.Message); lastNumber = 0; - } - } - - if (lastUser == 0) - { - try - { - lastUser = getLastUser(); - } - catch (Exception e) - { - Program.logError("Error while setting up last user " + e.Message); lastUser = 0; } } @@ -39,12 +38,16 @@ public static async Task loadAll() public static Task onMessageDeleted(IMessage msg, IMessageChannel channel) { - if (Convert.ToInt64(msg.Content) == lastNumber) - { - ((SocketTextChannel)Program.instance.edenor.GetChannel(channel.Id)).SendMessageAsync($"Число {msg.Content}, отправленное {msg.Author.Username}, было удалено. Следующее число - {Convert.ToInt64(msg.Content) + 1}"); - lastUser = (long)msg.Author.Id; - WriteSetting(lastNumber, (long)msg.Author.Id); + long msgC = checkAndConvertNumber(msg.Content); + if (msgC != -1) { + if (msgC == lastNumber) + { + ((SocketTextChannel)Program.instance.edenor.GetChannel(channel.Id)).SendMessageAsync($"Число {msg.Content}, отправленное {msg.Author.Username}, было удалено. Следующее число - {Convert.ToInt64(msg.Content) + 1}"); + lastUser = (long)msg.Author.Id; + WriteSetting(lastNumber, (long)msg.Author.Id); + } } + return Task.CompletedTask; } @@ -52,40 +55,42 @@ public static Task doWork(SocketMessage msg) { if (!msg.Content.StartsWith("Число") && msg.Author.Id != 710401785663193158) { - try + long msgC = checkAndConvertNumber(msg.Content); + long authorId = Convert.ToInt64(msg.Author.Id.ToString()); + if (msgC != -1) { - if (Convert.ToInt64(msg.Content) == lastNumber + 1 && lastUser != Convert.ToInt64(msg.Author.Id.ToString())) + if (msgC == lastNumber + 1 && lastUser != authorId) { lastNumber += 1; - lastUser = Convert.ToInt64(msg.Author.Id.ToString()); + lastUser = authorId; WriteSetting(lastNumber, lastUser); msg.AddReactionAsync(numberReact); - giveRole(Convert.ToInt64(msg.Content), msg.Author); + giveRole(lastNumber, msg.Author); return Task.CompletedTask; } else { - if (msg.Author.Id != 710401785663193158) - { - msg.DeleteAsync(); - } + removeMsg(msg); return Task.CompletedTask; } } - catch (Exception e) + else { - if (e.Message.Contains("Input string was not in a correct format.")) return Task.CompletedTask; - if (msg.Author.Id != 710401785663193158) - { - msg.DeleteAsync(); - } - Program.logError(e.Message); + removeMsg(msg); return Task.CompletedTask; } } else { return Task.CompletedTask; } } + private static void removeMsg(SocketMessage msg) + { + if (msg.Author.Id != 710401785663193158) + { + msg.DeleteAsync(); + } + } + private static void giveRole(long number, SocketUser user) { var guildUser = (SocketGuildUser)user; @@ -110,38 +115,14 @@ public static void WriteSetting(long number, long user) File.WriteAllText(dir, number.ToString() + ":" + user.ToString()); } - public static long getLastNumber() + private static long checkAndConvertNumber(string str) { - if (File.Exists(dir)) + if (str.All(char.IsDigit)) { - long retVal = 0; - StreamReader sr = new StreamReader(dir); - string text = sr.ReadToEnd(); - sr.Close(); - retVal = Convert.ToInt64(text.Split(":")[0]); - return retVal; + return Convert.ToInt64(str); } - else - { - return 0; - } - } - public static long getLastUser() - { - if (File.Exists(dir)) - { - long retVal = 0; - StreamReader sr = new StreamReader(dir); - string text = sr.ReadToEnd(); - sr.Close(); - retVal = Convert.ToInt64(text.Split(":")[1]); - return retVal; - } - else - { - return 0; - } + return -1; } } }