Merge pull request #288 from 13xforever/vnext

Maintenance release
This commit is contained in:
Ilya 2019-04-21 22:31:30 +05:00 committed by GitHub
commit 0469cf1547
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 14 deletions

View File

@ -41,15 +41,29 @@ namespace CompatBot.EventHandlers
{
try
{
var botMember = client.GetMember(guild, client.CurrentUser);
var after = DateTime.UtcNow - Config.ModerationTimeThreshold;
foreach (var channel in guild.Channels.Values.Where(ch => !ch.IsCategory && ch.Type != ChannelType.Voice))
{
var messages = await channel.GetMessagesAsync(500).ConfigureAwait(false);
var messagesToCheck = from msg in messages
where msg.CreationTimestamp > after
select msg;
foreach (var message in messagesToCheck)
await CheckMessageForInvitesAsync(client, message).ConfigureAwait(false);
if (!channel.PermissionsFor(botMember).HasPermission(Permissions.ReadMessageHistory))
{
Config.Log.Warn($"No permissions to read message history in #{channel.Name}");
continue;
}
try
{
var messages = await channel.GetMessagesAsync(500).ConfigureAwait(false);
var messagesToCheck = from msg in messages
where msg.CreationTimestamp > after
select msg;
foreach (var message in messagesToCheck)
await CheckMessageForInvitesAsync(client, message).ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Warn(e);
}
}
}
catch (Exception e)
@ -147,6 +161,9 @@ namespace CompatBot.EventHandlers
public static async Task<(bool hasInvalidInvite, bool attemptToWorkaround, List<DiscordInvite> invites)> GetInvitesAsync(this DiscordClient client, string message, DiscordUser author = null, bool tryMessageAsACode = false)
{
if (string.IsNullOrEmpty(message))
return (false, false, new List<DiscordInvite>(0));
var inviteCodes = new HashSet<string>(InviteLink.Matches(message).Select(m => m.Groups["invite_id"]?.Value).Where(s => !string.IsNullOrEmpty(s)));
var discordMeLinks = InviteLink.Matches(message).Select(m => m.Groups["me_id"]?.Value).Distinct().Where(s => !string.IsNullOrEmpty(s)).ToList();
var attemptedWorkaround = false;
@ -214,6 +231,5 @@ namespace CompatBot.EventHandlers
}
return (hasInvalidInvites, attemptedWorkaround, result);
}
}
}

View File

@ -61,14 +61,14 @@ namespace CompatBot.EventHandlers
return;
gameTitle = CompatList.FixGameTitleSearch(gameTitle);
if (ProductCodeLookup.ProductCode.IsMatch(args.Message.Content))
if (!string.IsNullOrEmpty(args.Message.Content) && ProductCodeLookup.ProductCode.IsMatch(args.Message.Content))
return;
var (_, info) = await LookupGameAsync(args.Channel, args.Message, gameTitle).ConfigureAwait(false);
var botSpamChannel = await args.Client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false);
gameTitle = info.Title.StripMarks();
var msg = $"{args.Message.Author.Mention} {gameTitle} is {info.Status.ToLowerInvariant()} since {info.ToUpdated()}\n" +
$"for more results please use compatibility list (<https://rpcs3.net/compatibility>) or `{Config.CommandPrefix}c` command in {botSpamChannel.Mention} (`!c {gameTitle.Sanitize()}`)";
gameTitle = info?.Title?.StripMarks();
var msg = $"{args.Message.Author.Mention} {gameTitle} is {info?.Status?.ToLowerInvariant()} since {info?.ToUpdated()}\n" +
$"for more results please use compatibility list (<https://rpcs3.net/compatibility>) or `{Config.CommandPrefix}c` command in {botSpamChannel.Mention} (`!c {gameTitle?.Sanitize()}`)";
await args.Channel.SendMessageAsync(msg).ConfigureAwait(false);
CooldownBuckets[args.Channel.Id] = DateTime.UtcNow;
}

View File

@ -199,14 +199,22 @@ namespace CompatBot
Action<Exception, string> logLevel = Config.Log.Info;
if (eventArgs.Level == LogLevel.Debug)
logLevel = Config.Log.Debug;
//else if (eventArgs.Level == LogLevel.Info)
// logLevel = botLog.Info;
else if (eventArgs.Level == LogLevel.Info)
{
//logLevel = Config.Log.Info;
if (eventArgs.Message?.Contains("Session resumed") ?? false)
Watchdog.DisconnectTimestamps.Clear();
}
else if (eventArgs.Level == LogLevel.Warning)
logLevel = Config.Log.Warn;
else if (eventArgs.Level == LogLevel.Error)
logLevel = Config.Log.Error;
else if (eventArgs.Level == LogLevel.Critical)
{
logLevel = Config.Log.Fatal;
if (eventArgs.Message?.Contains("Socket connection terminated") ?? false)
Watchdog.DisconnectTimestamps.Enqueue(DateTime.UtcNow);
}
logLevel(eventArgs.Exception, eventArgs.Message);
};
@ -237,7 +245,11 @@ namespace CompatBot
}
Config.Log.Debug("Running RPC3 update check thread");
backgroundTasks = Task.WhenAll(backgroundTasks, NewBuildsMonitor.MonitorAsync(client));
backgroundTasks = Task.WhenAll(
backgroundTasks,
NewBuildsMonitor.MonitorAsync(client),
Watchdog.Watch(client)
);
while (!Config.Cts.IsCancellationRequested)
{

36
CompatBot/Watchdog.cs Normal file
View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus;
namespace CompatBot
{
internal static class Watchdog
{
private static readonly TimeSpan CheckInterval = TimeSpan.FromSeconds(10);
public static readonly ConcurrentQueue<DateTime> DisconnectTimestamps = new ConcurrentQueue<DateTime>();
public static async Task Watch(DiscordClient client)
{
do
{
await Task.Delay(CheckInterval, Config.Cts.Token).ConfigureAwait(false);
if (DisconnectTimestamps.IsEmpty)
continue;
try
{
var ch = await client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false);
await client.SendMessageAsync(ch, "Potential socket deadlock detected, restarting...").ConfigureAwait(false);
Config.Cts.Cancel(false);
}
catch (Exception e)
{
Config.Log.Error(e);
}
} while (!Config.Cts.IsCancellationRequested);
}
}
}