private async Task CleanUp(DiscordChannel channel, DiscordMessage afterMessage) { var messages = await channel.GetMessagesAfterAsync(afterMessage.Id); if (messages.Count > 1) { await channel.DeleteMessagesAsync(messages); } await channel.DeleteMessageAsync(afterMessage); }
private async void ObserveLobbies() { try { TargetChannel = await Program.Client.GetChannelAsync(Convert.ToUInt64(PalantirEndpoint.ChannelID)); TargetMessage = await TargetChannel.GetMessageAsync(Convert.ToUInt64(PalantirEndpoint.MessageID)); } catch (Exception e) { Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " > Exception: " + e.ToString() + "at Channel:" + PalantirEndpoint.ChannelID + ", Msg: " + PalantirEndpoint.MessageID + ", Guild:" + PalantirEndpoint.GuildName); //RemoveTether(); return; } int notFound = 0; string lastContent = ""; while (!abort) { try { // try to build lobby message string content = BuildLobbyContent(); if (content == lastContent) { await TargetChannel.TriggerTypingAsync(); Thread.Sleep(4000); continue; } else { lastContent = content; } DiscordMessage split; try { do { split = (await TargetChannel.GetMessagesAfterAsync(TargetMessage.Id, 1)).First(); }while (split.Author.Id != Program.Client.CurrentUser.Id); } catch { // no split message found -> not required yet split = null; } if (content.Length <= 1900) { TargetMessage = await TargetMessage.ModifyAsync(content.Replace(" ", "")); if (!(split is null)) { await split.ModifyAsync("_ _"); } } else { int lastLobbyBreak = content.Length > 1900 ? 1900 : content.Length; while (content[lastLobbyBreak] != ' ' || lastLobbyBreak < 1000) { lastLobbyBreak--; } TargetMessage = await TargetMessage.ModifyAsync(content.Substring(0, lastLobbyBreak - 1).Replace(" ", "")); if (split is null) { split = await TargetChannel.SendMessageAsync("_ _"); } split = await split.ModifyAsync(content.Substring(lastLobbyBreak + 1, content.Length - lastLobbyBreak - 1).Replace(" ", "")); } await TargetChannel.TriggerTypingAsync(); notFound = 0; } catch (Microsoft.Data.Sqlite.SqliteException e) // catch sql exceptions { if (e.SqliteErrorCode == 8) { Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " > Locked DB. Skipped writing lobby data for this cycle."); } else { Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " > DB Error: " + e.SqliteErrorCode + ". Skipped writing lobby data for this cycle.\n" + e.ToString()); } } catch (DSharpPlus.Exceptions.NotFoundException e) // catch Discord api axceptions { notFound++; if (notFound > maxErrorCount) { Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " > Target Message couldnt be edited. Not found incremented to " + notFound + " / " + maxErrorCount + " Error: " + e.ToString()); RemoveTether(); return; } } catch (DSharpPlus.Exceptions.UnauthorizedException e) // catch Discord api axceptions { notFound++; if (notFound > maxErrorCount) { Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " > Target Message couldnt be edited. Not found incremented to " + notFound + " / " + maxErrorCount + " Error: " + e.ToString()); //RemoveTether(); return; } } catch (Exception e) // catch other exceptions { int line = new StackTrace(e, true).GetFrame(0).GetFileLineNumber(); Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " > Unhandled exception on line " + line + " - Target message couldnt be edited. No removal of tether, just 15s timeout. Error: " + e.ToString()); Thread.Sleep(15000); } Thread.Sleep(4000); } }