2020-02-12 14:16:19 +00:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2020-11-23 04:11:34 +00:00
|
|
|
using System.Collections.Generic;
|
2020-02-12 14:16:19 +00:00
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2021-01-31 14:03:11 +00:00
|
|
|
using Myriad.Rest.Exceptions;
|
2020-05-05 17:09:18 +00:00
|
|
|
|
2020-05-07 21:59:05 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
2020-03-08 09:55:33 +00:00
|
|
|
using Npgsql;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-03-18 19:16:28 +00:00
|
|
|
using Polly.Timeout;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public static class MiscUtils {
|
2020-08-25 20:44:52 +00:00
|
|
|
public static string ProxyTagsString(this PKMember member, string separator = ", ") =>
|
|
|
|
string.Join(separator, member.ProxyTags.Select(t => t.ProxyString.AsCode()));
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2020-11-23 04:11:34 +00:00
|
|
|
|
|
|
|
private static String entityTerm<T>(int count, bool isTarget)
|
|
|
|
{
|
|
|
|
var ret = "";
|
|
|
|
ret += isTarget ? "Member" : "Group";
|
|
|
|
if ((
|
|
|
|
(typeof(T) == typeof(GroupId) && !isTarget) ||
|
|
|
|
(typeof(T) == typeof(MemberId) && isTarget)
|
|
|
|
) && count > 1)
|
|
|
|
ret += "s";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String GroupAddRemoveResponse<T>(List<T> entityList, List<T> actionedOn, Groups.AddRemoveOperation op)
|
|
|
|
{
|
|
|
|
var opStr = op == Groups.AddRemoveOperation.Add ? "added to" : "removed from";
|
|
|
|
var inStr = op == Groups.AddRemoveOperation.Add ? "in" : "not in";
|
|
|
|
var notActionedOn = entityList.Count - actionedOn.Count;
|
|
|
|
|
|
|
|
var groupNotActionedPosStr = typeof(T) == typeof(GroupId) ? notActionedOn.ToString() + " " : "";
|
|
|
|
var memberNotActionedPosStr = typeof(T) == typeof(MemberId) ? notActionedOn.ToString() + " " : "";
|
|
|
|
|
|
|
|
if (actionedOn.Count == 0)
|
|
|
|
return $"{Emojis.Error} {entityTerm<T>(notActionedOn, true)} not {opStr} {entityTerm<T>(entityList.Count, false).ToLower()} ({entityTerm<T>(notActionedOn, true).ToLower()} already {inStr} {entityTerm<T>(entityList.Count, false).ToLower()}).";
|
|
|
|
else
|
|
|
|
if (notActionedOn == 0)
|
|
|
|
return $"{Emojis.Success} {entityTerm<T>(actionedOn.Count, true)} {opStr} {entityTerm<T>(actionedOn.Count, false).ToLower()}.";
|
|
|
|
else
|
2021-02-01 13:26:51 +00:00
|
|
|
return $"{Emojis.Success} {actionedOn.Count} {entityTerm<T>(actionedOn.Count, true).ToLower()} {opStr} {entityTerm<T>(actionedOn.Count, false).ToLower()} ({memberNotActionedPosStr}{entityTerm<T>(actionedOn.Count, true).ToLower()} already {inStr} {groupNotActionedPosStr}{entityTerm<T>(notActionedOn, false).ToLower()}).";
|
2020-11-23 04:11:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
public static bool IsOurProblem(this Exception e)
|
|
|
|
{
|
|
|
|
// This function filters out sporadic errors out of our control from being reported to Sentry
|
|
|
|
// otherwise we'd blow out our error reporting budget as soon as Discord takes a dump, or something.
|
|
|
|
|
|
|
|
// Discord server errors are *not our problem*
|
2020-05-07 21:59:05 +00:00
|
|
|
// TODO: DSharpPlus doesn't have a generic "HttpException" type and only special cases a couple response codes (that we don't need here)
|
|
|
|
// Doesn't seem to handle 500s in the library at all, I'm not sure what it does in case it receives one...
|
2020-04-17 21:10:01 +00:00
|
|
|
// if (e is DSharpPlus.Exceptions he && ((int) he.HttpCode) >= 500) return false;
|
2020-05-07 21:59:05 +00:00
|
|
|
|
|
|
|
// Occasionally Discord's API will Have A Bad Time and return a bunch of CloudFlare errors (in HTML format).
|
|
|
|
// The library tries to parse these HTML responses as JSON and crashes with a consistent exception message.
|
|
|
|
if (e is JsonReaderException jre && jre.Message == "Unexpected character encountered while parsing value: <. Path '', line 0, position 0.") return false;
|
2020-05-12 20:19:33 +00:00
|
|
|
|
|
|
|
// And now (2020-05-12), apparently Discord returns these weird responses occasionally. Also not our problem.
|
2021-01-31 14:03:11 +00:00
|
|
|
if (e is BadRequestException bre && bre.ResponseBody.Contains("<center>nginx</center>")) return false;
|
|
|
|
if (e is NotFoundException ne && ne.ResponseBody.Contains("<center>nginx</center>")) return false;
|
|
|
|
if (e is UnauthorizedException ue && ue.ResponseBody.Contains("<center>nginx</center>")) return false;
|
2020-11-16 08:05:00 +00:00
|
|
|
|
2021-03-18 19:16:28 +00:00
|
|
|
// Filter out timeout/ratelimit related stuff
|
|
|
|
if (e is TooManyRequestsException) return false;
|
|
|
|
if (e is RatelimitBucketExhaustedException) return false;
|
|
|
|
if (e is TimeoutRejectedException) return false;
|
|
|
|
|
2021-01-31 14:03:11 +00:00
|
|
|
// 5xxs? also not our problem :^)
|
|
|
|
if (e is UnknownDiscordRequestException udre && (int) udre.StatusCode >= 500) return false;
|
2020-11-16 08:05:00 +00:00
|
|
|
|
2020-02-26 18:47:30 +00:00
|
|
|
// Webhook server errors are also *not our problem*
|
2020-03-26 23:01:42 +00:00
|
|
|
// (this includes rate limit errors, WebhookRateLimited is a subclass)
|
2020-02-26 18:47:30 +00:00
|
|
|
if (e is WebhookExecutionErrorOnDiscordsEnd) return false;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
// Socket errors are *not our problem*
|
2020-12-26 12:18:31 +00:00
|
|
|
if (e.GetBaseException() is SocketException) return false;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
|
|
|
// Tasks being cancelled for whatver reason are, you guessed it, also not our problem.
|
|
|
|
if (e is TaskCanceledException) return false;
|
2020-02-18 20:56:15 +00:00
|
|
|
|
|
|
|
// Sometimes Discord just times everything out.
|
|
|
|
if (e is TimeoutException) return false;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2020-03-08 09:55:33 +00:00
|
|
|
// Ignore "Database is shutting down" error
|
|
|
|
if (e is PostgresException pe && pe.SqlState == "57P03") return false;
|
|
|
|
|
2020-10-14 21:35:10 +00:00
|
|
|
// Ignore thread pool exhaustion errors
|
|
|
|
if (e is NpgsqlException npe && npe.Message.Contains("The connection pool has been exhausted")) return false;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
// This may expanded at some point.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|