Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrodicaprio committed Oct 8, 2022
1 parent 17df672 commit c1bbabb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
73 changes: 41 additions & 32 deletions Unifiedban.Terminal/Bot/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public static void Initialize(string apikey)

if (string.IsNullOrEmpty(apikey))
{
Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban Terminal Startup",
Level = Models.SystemLog.Levels.Fatal,
Level = SystemLog.Levels.Fatal,
Message = "API KEY must be set!",
UserId = -1
});
Expand All @@ -65,24 +65,24 @@ public static void Initialize(string apikey)
var me = BotClient.GetMeAsync().Result;
Username = me.Username;
MyId = me.Id;
Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban Terminal Startup",
Level = Models.SystemLog.Levels.Warn,
Level = SystemLog.Levels.Warn,
Message = $"Hello, World! I am user {me.Id} and my name is {me.FirstName}.",
UserId = -1
});

Console.Title = $"Unifiedban - Username: {me.Username} - Instance ID: {instanceId}";

Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban Terminal Startup",
Level = Models.SystemLog.Levels.Info,
Level = SystemLog.Levels.Info,
Message = "Bot Client initialized",
UserId = -2
});
Expand All @@ -106,30 +106,30 @@ await BotClient.SendTextMessageAsync(
var infoUrl = $"https://api.telegram.org/bot{APIKEY}/getWebhookInfo";
var infoClient = new HttpClient();
var infoRes = await infoClient.GetAsync(infoUrl);
Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "StartReceiving",
Level = Models.SystemLog.Levels.Warn,
Level = SystemLog.Levels.Warn,
Message = infoRes.Content.ReadAsStringAsync().Result,
UserId = -2
});
Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "StartReceiving",
Level = Models.SystemLog.Levels.Warn,
Level = SystemLog.Levels.Warn,
Message = $"Skipping pending updates older than: {pastHoursToSkip} hour(s)",
UserId = -2
});
Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "StartReceiving",
Level = Models.SystemLog.Levels.Warn,
Level = SystemLog.Levels.Warn,
Message = $"Throwing pending updates: {throwPendingUpdates}",
UserId = -2
});
Expand Down Expand Up @@ -202,6 +202,16 @@ ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => ex.ToString()
};

Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "PollingErrorHandler",
Level = SystemLog.Levels.Error,
Message = errMsg,
UserId = -1
});

if(!Cts.IsCancellationRequested)
Restart(ex.Message);
Expand Down Expand Up @@ -295,12 +305,12 @@ private static async Task HandleCallbackQuery(CallbackQuery callbackQuery)
{
if (callbackQuery.Message!.Date < DateTime.Now.AddHours(-pastHoursToSkip))
{
Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban.Bot.Manager.BotClient_OnCallbackQuery",
Level = Models.SystemLog.Levels.Debug,
Level = SystemLog.Levels.Debug,
Message = $"Skipping callback older than {pastHoursToSkip} hour(s)",
UserId = -1
});
Expand All @@ -310,14 +320,14 @@ private static async Task HandleCallbackQuery(CallbackQuery callbackQuery)
await Task.Run(() => CacheData.IncrementHandledMessages());

if(CacheData.Groups[callbackQuery.Message.Chat.Id].State !=
Models.Group.TelegramGroup.Status.Active) return;
TelegramGroup.Status.Active) return;

Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban.Bot.Manager.BotClient_OnCallbackQuery",
Level = Models.SystemLog.Levels.Debug,
Level = SystemLog.Levels.Debug,
Message = "CallbackQuery received",
UserId = -1
});
Expand All @@ -335,37 +345,36 @@ private static async Task HandleUpdateAsync(Message message)
{
if (message.Date < DateTime.Now.AddHours(-pastHoursToSkip))
{
Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban.Bot.Manager.BotClient_OnMessage",
Level = Models.SystemLog.Levels.Debug,
Level = SystemLog.Levels.Debug,
Message = $"Skipping update older than {pastHoursToSkip} hour(s)",
UserId = -1
});
return;
}

await Task.Run(CacheData.IncrementHandledMessages);
await Task.Run(() => Functions.CacheUsername(message));

if(CacheData.Groups.Keys.Contains(message.Chat.Id))
if (CacheData.Groups[message.Chat.Id].State !=
Models.Group.TelegramGroup.Status.Active &&
message.Text != "/enable") return;

Data.Utils.Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban.Bot.Manager.BotClient_OnMessage",
Level = Models.SystemLog.Levels.Debug,
Level = SystemLog.Levels.Debug,
Message = "Message received",
UserId = -1
});

await Task.Run(() => Functions.CacheUsername(message));

if(CacheData.Groups.Keys.Contains(message.Chat.Id))
if (CacheData.Groups[message.Chat.Id].State !=
TelegramGroup.Status.Active &&
message.Text != "/enable") return;

if (message.MigrateToChatId is not null)
{
Functions.MigrateToChatId(message);
Expand Down Expand Up @@ -398,7 +407,7 @@ private static async Task HandleUpdateAsync(Message message)
message.Chat.Id,
message.Chat.Id.ToString().Replace("-", ""),
Guid.NewGuid());
MessageQueueManager.EnqueueLog(new Models.ChatMessage()
MessageQueueManager.EnqueueLog(new ChatMessage()
{
ParseMode = ParseMode.Markdown,
Text = logMessage
Expand All @@ -410,16 +419,15 @@ await BotClient.SendTextMessageAsync(message.Chat.Id,
"We're sorry but an error has occurred while retrieving this chat on our database.\n" +
"Please add again the bot if you want to continue to use it.\n" +
"For any doubt reach us in our support group @unifiedban_group");

}
catch (Exception ex)
{
Logging.AddLog(new Models.SystemLog()
Logging.AddLog(new SystemLog()
{
LoggerName = CacheData.LoggerName,
Date = DateTime.Now,
Function = "Unifiedban.Bot.Manager.BotClient_OnMessage",
Level = Models.SystemLog.Levels.Warn,
Level = SystemLog.Levels.Warn,
Message = $"Can't send left notification due to missing permission.\n\n{ex}",
UserId = -1
});
Expand Down Expand Up @@ -461,6 +469,7 @@ await BotClient.SendTextMessageAsync(message.Chat.Id,
Controls.Manager.DoCheck(message);
}
}

if (message.NewChatMembers != null)
{
Functions.UserJoinedAction(message);
Expand Down
2 changes: 1 addition & 1 deletion Unifiedban.Terminal/Unifiedban.Terminal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryUrl>https://github.com/unified-ban/Terminal</RepositoryUrl>
<RepositoryType>GIT</RepositoryType>
<AssemblyVersion>3.13.11</AssemblyVersion>
<FileVersion>3.13.11.1</FileVersion>
<FileVersion>3.13.11.2</FileVersion>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down

0 comments on commit c1bbabb

Please sign in to comment.