2019-04-19 18:48:37 +00:00
using System ;
using System.Linq ;
2020-04-24 21:47:35 +00:00
using System.Net.WebSockets ;
2020-05-05 16:12:34 +00:00
using System.Threading ;
2019-04-19 18:48:37 +00:00
using System.Threading.Tasks ;
2020-05-01 23:52:52 +00:00
2019-07-16 19:59:06 +00:00
using App.Metrics ;
2019-12-23 00:49:21 +00:00
2020-01-26 00:27:45 +00:00
using Autofac ;
2020-04-17 21:10:01 +00:00
using DSharpPlus ;
using DSharpPlus.Entities ;
using DSharpPlus.EventArgs ;
2020-05-05 17:09:18 +00:00
using DSharpPlus.Exceptions ;
2020-04-17 21:10:01 +00:00
2020-04-28 23:14:49 +00:00
using NodaTime ;
2020-01-26 00:27:45 +00:00
using PluralKit.Core ;
2019-10-05 05:41:00 +00:00
2019-07-15 19:02:50 +00:00
using Sentry ;
2020-01-26 00:27:45 +00:00
2019-07-18 15:13:42 +00:00
using Serilog ;
2019-07-19 00:29:08 +00:00
using Serilog.Events ;
2019-04-19 18:48:37 +00:00
2019-04-21 13:33:22 +00:00
namespace PluralKit.Bot
2019-04-19 18:48:37 +00:00
{
2020-05-01 23:52:52 +00:00
public class Bot
2019-04-19 18:48:37 +00:00
{
2020-05-01 23:52:52 +00:00
private readonly DiscordShardedClient _client ;
private readonly ILogger _logger ;
private readonly ILifetimeScope _services ;
private readonly PeriodicStatCollector _collector ;
private readonly IMetrics _metrics ;
2019-04-19 18:48:37 +00:00
2020-05-05 16:12:34 +00:00
private Timer _periodicTask ; // Never read, just kept here for GC reasons
2019-07-18 15:13:42 +00:00
2020-05-01 23:52:52 +00:00
public Bot ( DiscordShardedClient client , ILifetimeScope services , ILogger logger , PeriodicStatCollector collector , IMetrics metrics )
2019-04-19 18:48:37 +00:00
{
2020-04-17 21:10:01 +00:00
_client = client ;
2020-05-01 23:52:52 +00:00
_services = services ;
2019-07-16 21:34:22 +00:00
_collector = collector ;
2020-05-01 23:52:52 +00:00
_metrics = metrics ;
2019-07-18 15:13:42 +00:00
_logger = logger . ForContext < Bot > ( ) ;
2019-04-19 18:48:37 +00:00
}
2020-05-01 23:52:52 +00:00
public void Init ( )
2019-04-19 18:48:37 +00:00
{
2020-05-01 23:52:52 +00:00
// Attach the handlers we need
2020-04-17 21:10:01 +00:00
_client . DebugLogger . LogMessageReceived + = FrameworkLog ;
2019-08-09 10:47:46 +00:00
2020-05-01 23:52:52 +00:00
// HandleEvent takes a type parameter, automatically inferred by the event type
2020-05-02 14:00:43 +00:00
// It will then look up an IEventHandler<TypeOfEvent> in the DI container and call that object's handler method
// For registering new ones, see Modules.cs
2020-05-01 23:52:52 +00:00
_client . MessageCreated + = HandleEvent ;
_client . MessageDeleted + = HandleEvent ;
_client . MessageUpdated + = HandleEvent ;
_client . MessagesBulkDeleted + = HandleEvent ;
_client . MessageReactionAdded + = HandleEvent ;
2020-05-05 16:12:34 +00:00
// Update shard status for shards immediately on connect
_client . Ready + = args = > UpdateBotStatus ( args . Client ) ;
_client . Resumed + = args = > UpdateBotStatus ( args . Client ) ;
2019-12-22 11:50:47 +00:00
2020-05-01 23:52:52 +00:00
// Init the shard stuff
2020-05-09 13:44:56 +00:00
_services . Resolve < ShardInfoService > ( ) . Init ( ) ;
2019-10-05 05:41:00 +00:00
2020-05-01 23:52:52 +00:00
// Not awaited, just needs to run in the background
2020-05-05 16:12:34 +00:00
// Trying our best to run it at whole minute boundaries (xx:00), with ~250ms buffer
// This *probably* doesn't matter in practice but I jut think it's neat, y'know.
var timeNow = SystemClock . Instance . GetCurrentInstant ( ) ;
var timeTillNextWholeMinute = TimeSpan . FromMilliseconds ( 60000 - timeNow . ToUnixTimeMilliseconds ( ) % 60000 + 250 ) ;
_periodicTask = new Timer ( _ = >
{
var __ = UpdatePeriodic ( ) ;
} , null , timeTillNextWholeMinute , TimeSpan . FromMinutes ( 1 ) ) ;
}
public async Task Shutdown ( )
{
// This will stop the timer and prevent any subsequent invocations
await _periodicTask . DisposeAsync ( ) ;
// Send users a lil status message
// We're not actually properly disconnecting from the gateway (lol) so it'll linger for a few minutes
// Should be plenty of time for the bot to connect again next startup and set the real status
await _client . UpdateStatusAsync ( new DiscordActivity ( "Restarting... (please wait)" ) ) ;
2020-05-01 23:52:52 +00:00
}
private Task HandleEvent < T > ( T evt ) where T : DiscordEventArgs
{
// We don't want to stall the event pipeline, so we'll "fork" inside here
var _ = HandleEventInner ( ) ;
2019-10-05 05:41:00 +00:00
return Task . CompletedTask ;
2020-05-01 23:52:52 +00:00
async Task HandleEventInner ( )
{
var serviceScope = _services . BeginLifetimeScope ( ) ;
2020-05-02 14:00:43 +00:00
// Also, find a Sentry enricher for the event type (if one is present), and ask it to put some event data in the Sentry scope
var sentryEnricher = serviceScope . ResolveOptional < ISentryEnricher < T > > ( ) ;
sentryEnricher ? . Enrich ( serviceScope . Resolve < Scope > ( ) , evt ) ;
2020-05-05 14:03:46 +00:00
// Find an event handler that can handle the type of event (<T>) we're given
var handler = serviceScope . Resolve < IEventHandler < T > > ( ) ;
var queue = serviceScope . ResolveOptional < HandlerQueue < T > > ( ) ;
2020-05-01 23:52:52 +00:00
try
{
2020-05-05 14:03:46 +00:00
// Delegate to the queue to see if it wants to handle this event
// the TryHandle call returns true if it's handled the event
// Usually it won't, so just pass it on to the main handler
if ( queue = = null | | ! await queue . TryHandle ( evt ) )
await handler . Handle ( evt ) ;
2020-05-01 23:52:52 +00:00
}
catch ( Exception exc )
{
await HandleError ( handler , evt , serviceScope , exc ) ;
}
}
2019-07-19 00:29:08 +00:00
}
2020-05-01 23:52:52 +00:00
private async Task HandleError < T > ( IEventHandler < T > handler , T evt , ILifetimeScope serviceScope , Exception exc )
where T : DiscordEventArgs
2019-07-19 00:29:08 +00:00
{
2020-05-05 14:42:14 +00:00
// Make this beforehand so we can access the event ID for logging
var sentryEvent = new SentryEvent ( exc ) ;
2020-05-05 17:09:18 +00:00
if ( exc is BadRequestException bre )
sentryEvent . SetExtra ( "errors" , MiscUtils . ExtractError ( bre ) ) ;
2020-05-05 14:42:14 +00:00
_logger . Error ( exc , "Exception in bot event handler (Sentry ID: {SentryEventId})" , sentryEvent . EventId ) ;
2019-07-19 00:29:08 +00:00
2020-05-01 23:52:52 +00:00
var shouldReport = exc . IsOurProblem ( ) ;
if ( shouldReport )
{
// Report error to Sentry
// This will just no-op if there's no URL set
var sentryScope = serviceScope . Resolve < Scope > ( ) ;
SentrySdk . CaptureEvent ( sentryEvent , sentryScope ) ;
// Once we've sent it to Sentry, report it to the user (if we have permission to)
var reportChannel = handler . ErrorChannelFor ( evt ) ;
2020-05-02 13:29:36 +00:00
if ( reportChannel ! = null & & reportChannel . BotHasAllPermissions ( Permissions . SendMessages ) )
2020-05-01 23:52:52 +00:00
{
var eid = sentryEvent . EventId ;
await reportChannel . SendMessageAsync (
$"{Emojis.Error} Internal error occurred. Please join the support server (<https://discord.gg/PczBt78>), and send the developer this ID: `{eid}`\nBe sure to include a description of what you were doing to make the error occur." ) ;
}
}
2019-04-19 18:48:37 +00:00
}
2020-04-28 23:14:49 +00:00
2019-04-25 16:50:07 +00:00
private async Task UpdatePeriodic ( )
2019-04-20 20:25:03 +00:00
{
2020-05-05 16:12:34 +00:00
_logger . Information ( "Running once-per-minute scheduled tasks" ) ;
await UpdateBotStatus ( ) ;
// Collect some stats, submit them to the metrics backend
await _collector . CollectStats ( ) ;
await Task . WhenAll ( ( ( IMetricsRoot ) _metrics ) . ReportRunner . RunAllAsync ( ) ) ;
_logger . Information ( "Submitted metrics to backend" ) ;
}
private async Task UpdateBotStatus ( DiscordClient specificShard = null )
{
var totalGuilds = _client . ShardClients . Values . Sum ( c = > c . Guilds . Count ) ;
try // DiscordClient may throw an exception if the socket is closed (e.g just after OP 7 received)
2020-04-24 21:47:35 +00:00
{
2020-05-05 16:12:34 +00:00
Task UpdateStatus ( DiscordClient shard ) = >
shard . UpdateStatusAsync ( new DiscordActivity ( $"pk;help | in {totalGuilds} servers | shard #{shard.ShardId}" ) ) ;
2020-04-28 23:14:49 +00:00
2020-05-05 16:12:34 +00:00
if ( specificShard ! = null )
await UpdateStatus ( specificShard ) ;
else // Run shard updates concurrently
await Task . WhenAll ( _client . ShardClients . Values . Select ( UpdateStatus ) ) ;
2020-04-28 23:14:49 +00:00
}
2020-05-05 16:12:34 +00:00
catch ( WebSocketException ) { }
2019-04-25 16:50:07 +00:00
}
2020-05-05 16:12:34 +00:00
2020-05-09 13:44:56 +00:00
public void FrameworkLog ( object sender , DebugLogMessageEventArgs args )
2020-02-12 13:21:48 +00:00
{
2020-05-01 23:52:52 +00:00
// Bridge D#+ logging to Serilog
LogEventLevel level = LogEventLevel . Verbose ;
if ( args . Level = = LogLevel . Critical )
level = LogEventLevel . Fatal ;
else if ( args . Level = = LogLevel . Debug )
level = LogEventLevel . Debug ;
else if ( args . Level = = LogLevel . Error )
level = LogEventLevel . Error ;
else if ( args . Level = = LogLevel . Info )
level = LogEventLevel . Information ;
else if ( args . Level = = LogLevel . Warning )
level = LogEventLevel . Warning ;
2020-02-12 13:21:48 +00:00
2020-05-01 23:52:52 +00:00
_logger . Write ( level , args . Exception , "D#+ {Source}: {Message}" , args . Application , args . Message ) ;
2020-02-12 13:21:48 +00:00
}
2019-08-09 10:47:46 +00:00
}
2020-05-01 23:52:52 +00:00
}