Merge branch 'new-shard-handler'
This commit is contained in:
@@ -287,7 +287,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
new ActivityPartial
|
||||
{
|
||||
Name = $"pk;help | in {totalGuilds:N0} servers | shard #{shard.ShardInfo?.ShardId}",
|
||||
Name = $"pk;help | in {totalGuilds:N0} servers | shard #{shard.ShardId}",
|
||||
Type = ActivityType.Game,
|
||||
Url = "https://pluralkit.me/"
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ namespace PluralKit.Bot
|
||||
private readonly MessageCreateEvent _message;
|
||||
private readonly Parameters _parameters;
|
||||
private readonly MessageContext _messageContext;
|
||||
private readonly PermissionSet _botPermissions;
|
||||
private readonly PermissionSet _userPermissions;
|
||||
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
@@ -42,7 +40,7 @@ namespace PluralKit.Bot
|
||||
private Command _currentCommand;
|
||||
|
||||
public Context(ILifetimeScope provider, Shard shard, Guild? guild, Channel channel, MessageCreateEvent message, int commandParseOffset,
|
||||
PKSystem senderSystem, MessageContext messageContext, PermissionSet botPermissions)
|
||||
PKSystem senderSystem, MessageContext messageContext)
|
||||
{
|
||||
_message = message;
|
||||
_shard = shard;
|
||||
@@ -59,9 +57,6 @@ namespace PluralKit.Bot
|
||||
_parameters = new Parameters(message.Content?.Substring(commandParseOffset));
|
||||
_rest = provider.Resolve<DiscordApiClient>();
|
||||
_cluster = provider.Resolve<Cluster>();
|
||||
|
||||
_botPermissions = botPermissions;
|
||||
_userPermissions = _cache.PermissionsFor(message);
|
||||
}
|
||||
|
||||
public IDiscordCache Cache => _cache;
|
||||
@@ -76,8 +71,8 @@ namespace PluralKit.Bot
|
||||
public Cluster Cluster => _cluster;
|
||||
public MessageContext MessageContext => _messageContext;
|
||||
|
||||
public PermissionSet BotPermissions => _botPermissions;
|
||||
public PermissionSet UserPermissions => _userPermissions;
|
||||
public PermissionSet BotPermissions => _provider.Resolve<Bot>().PermissionsIn(_channel.Id);
|
||||
public PermissionSet UserPermissions => _cache.PermissionsFor(_message);
|
||||
|
||||
public DiscordApiClient Rest => _rest;
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ namespace PluralKit.Bot {
|
||||
var totalSwitches = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SwitchCount.Name)?.Value ?? 0;
|
||||
var totalMessages = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MessageCount.Name)?.Value ?? 0;
|
||||
|
||||
var shardId = ctx.Shard.ShardInfo.ShardId;
|
||||
var shardId = ctx.Shard.ShardId;
|
||||
var shardTotal = ctx.Cluster.Shards.Count;
|
||||
var shardUpTotal = _shards.Shards.Where(x => x.Connected).Count();
|
||||
var shardInfo = _shards.GetShardInfo(ctx.Shard);
|
||||
|
||||
@@ -114,7 +114,7 @@ namespace PluralKit.Bot
|
||||
try
|
||||
{
|
||||
var system = ctx.SystemId != null ? await _db.Execute(c => _repo.GetSystem(c, ctx.SystemId.Value)) : null;
|
||||
await _tree.ExecuteCommand(new Context(_services, shard, guild, channel, evt, cmdStart, system, ctx, _bot.PermissionsIn(channel.Id)));
|
||||
await _tree.ExecuteCommand(new Context(_services, shard, guild, channel, evt, cmdStart, system, ctx));
|
||||
}
|
||||
catch (PKError)
|
||||
{
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace PluralKit.Bot
|
||||
// Start the Discord shards themselves (handlers already set up)
|
||||
logger.Information("Connecting to Discord");
|
||||
var info = await services.Resolve<DiscordApiClient>().GetGatewayBot();
|
||||
await services.Resolve<Cluster>().Start(info);
|
||||
await services.Resolve<Cluster>().Start(info with { Shards = 10 });
|
||||
logger.Information("Connected! All is good (probably).");
|
||||
|
||||
// Lastly, we just... wait. Everything else is handled in the DiscordClient event loop
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using App.Metrics;
|
||||
|
||||
@@ -66,11 +65,8 @@ namespace PluralKit.Bot
|
||||
} else _shardInfo[shard.ShardId] = info = new ShardInfo();
|
||||
|
||||
// Call our own SocketOpened listener manually (and then attach the listener properly)
|
||||
SocketOpened(shard);
|
||||
shard.SocketOpened += () => SocketOpened(shard);
|
||||
|
||||
|
||||
// Register listeners for new shards
|
||||
_logger.Information("Attaching listeners to new shard #{Shard}", shard.ShardId);
|
||||
shard.Resumed += () => Resumed(shard);
|
||||
shard.Ready += () => Ready(shard);
|
||||
shard.SocketClosed += (closeStatus, message) => SocketClosed(shard, closeStatus, message);
|
||||
@@ -78,14 +74,6 @@ namespace PluralKit.Bot
|
||||
|
||||
// Register that we've seen it
|
||||
info.HasAttachedListeners = true;
|
||||
|
||||
}
|
||||
|
||||
private void SocketOpened(Shard shard)
|
||||
{
|
||||
// We do nothing else here, since this kinda doesn't mean *much*? It's only really started once we get Ready/Resumed
|
||||
// And it doesn't get fired first time around since we don't have time to add the event listener before it's fired'
|
||||
_logger.Information("Shard #{Shard} opened socket", shard.ShardId);
|
||||
}
|
||||
|
||||
private ShardInfo TryGetShard(Shard shard)
|
||||
@@ -100,29 +88,22 @@ namespace PluralKit.Bot
|
||||
|
||||
private void Resumed(Shard shard)
|
||||
{
|
||||
_logger.Information("Shard #{Shard} resumed connection", shard.ShardId);
|
||||
|
||||
var info = TryGetShard(shard);
|
||||
// info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
info.Connected = true;
|
||||
ReportShardStatus();
|
||||
}
|
||||
|
||||
private void Ready(Shard shard)
|
||||
{
|
||||
_logger.Information("Shard #{Shard} sent Ready event", shard.ShardId);
|
||||
|
||||
var info = TryGetShard(shard);
|
||||
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
info.Connected = true;
|
||||
ReportShardStatus();
|
||||
}
|
||||
|
||||
private void SocketClosed(Shard shard, WebSocketCloseStatus closeStatus, string message)
|
||||
private void Ready(Shard shard)
|
||||
{
|
||||
var info = TryGetShard(shard);
|
||||
info.LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
info.Connected = true;
|
||||
ReportShardStatus();
|
||||
}
|
||||
|
||||
private void SocketClosed(Shard shard, WebSocketCloseStatus? closeStatus, string message)
|
||||
{
|
||||
_logger.Warning("Shard #{Shard} disconnected ({CloseCode}: {CloseMessage})",
|
||||
shard.ShardId, closeStatus, message);
|
||||
|
||||
var info = TryGetShard(shard);
|
||||
info.DisconnectionCount++;
|
||||
info.Connected = false;
|
||||
@@ -131,9 +112,6 @@ namespace PluralKit.Bot
|
||||
|
||||
private void Heartbeated(Shard shard, TimeSpan latency)
|
||||
{
|
||||
_logger.Information("Shard #{Shard} received heartbeat (latency: {Latency} ms)",
|
||||
shard.ShardId, latency.Milliseconds);
|
||||
|
||||
var info = TryGetShard(shard);
|
||||
info.LastHeartbeatTime = SystemClock.Instance.GetCurrentInstant();
|
||||
info.Connected = true;
|
||||
|
||||
@@ -155,6 +155,7 @@ namespace PluralKit.Bot {
|
||||
// "escape hatch", clean up as if we hit X
|
||||
}
|
||||
|
||||
// todo: re-check
|
||||
if (ctx.BotPermissions.HasFlag(PermissionSet.ManageMessages))
|
||||
await ctx.Rest.DeleteAllReactions(msg.ChannelId, msg.Id);
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace PluralKit.Bot
|
||||
{"guild", evt.GuildId.ToString()},
|
||||
{"message", evt.Id.ToString()},
|
||||
});
|
||||
scope.SetTag("shard", shard.ShardInfo.ShardId.ToString());
|
||||
scope.SetTag("shard", shard.ShardId.ToString());
|
||||
|
||||
// Also report information about the bot's permissions in the channel
|
||||
// We get a lot of permission errors so this'll be useful for determining problems
|
||||
@@ -55,7 +55,7 @@ namespace PluralKit.Bot
|
||||
{"guild", evt.GuildId.ToString()},
|
||||
{"message", evt.Id.ToString()},
|
||||
});
|
||||
scope.SetTag("shard", shard.ShardInfo.ShardId.ToString());
|
||||
scope.SetTag("shard", shard.ShardId.ToString());
|
||||
}
|
||||
|
||||
public void Enrich(Scope scope, Shard shard, MessageUpdateEvent evt)
|
||||
@@ -67,7 +67,7 @@ namespace PluralKit.Bot
|
||||
{"guild", evt.GuildId.Value.ToString()},
|
||||
{"message", evt.Id.ToString()}
|
||||
});
|
||||
scope.SetTag("shard", shard.ShardInfo.ShardId.ToString());
|
||||
scope.SetTag("shard", shard.ShardId.ToString());
|
||||
}
|
||||
|
||||
public void Enrich(Scope scope, Shard shard, MessageDeleteBulkEvent evt)
|
||||
@@ -79,7 +79,7 @@ namespace PluralKit.Bot
|
||||
{"guild", evt.GuildId.ToString()},
|
||||
{"messages", string.Join(",", evt.Ids)},
|
||||
});
|
||||
scope.SetTag("shard", shard.ShardInfo.ShardId.ToString());
|
||||
scope.SetTag("shard", shard.ShardId.ToString());
|
||||
}
|
||||
|
||||
public void Enrich(Scope scope, Shard shard, MessageReactionAddEvent evt)
|
||||
@@ -93,7 +93,7 @@ namespace PluralKit.Bot
|
||||
{"message", evt.MessageId.ToString()},
|
||||
{"reaction", evt.Emoji.Name}
|
||||
});
|
||||
scope.SetTag("shard", shard.ShardInfo.ShardId.ToString());
|
||||
scope.SetTag("shard", shard.ShardId.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user