refactor: only pass shard ID to event handlers instead of full shard object

This commit is contained in:
spiral
2022-01-14 18:39:03 -05:00
parent bf80dd0988
commit 50a24f03a7
13 changed files with 74 additions and 70 deletions

View File

@@ -63,9 +63,9 @@ public class MessageCreated: IEventHandler<MessageCreateEvent>
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
_lastMessageCache.GetLastMessage(msg.ChannelId)?.Current.Id == msg.Id;
public async Task Handle(Shard shard, MessageCreateEvent evt)
public async Task Handle(int shardId, MessageCreateEvent evt)
{
if (evt.Author.Id == shard.User?.Id) return;
if (evt.Author.Id == await _cache.GetOwnUser()) return;
if (evt.Type != Message.MessageType.Default && evt.Type != Message.MessageType.Reply) return;
if (IsDuplicateMessage(evt)) return;
@@ -90,9 +90,9 @@ public class MessageCreated: IEventHandler<MessageCreateEvent>
if (evt.Author.Bot || evt.WebhookId != null || evt.Author.System == true)
return;
if (await TryHandleCommand(shard, evt, guild, channel, ctx))
if (await TryHandleCommand(shardId, evt, guild, channel, ctx))
return;
await TryHandleProxy(shard, evt, guild, channel, ctx);
await TryHandleProxy(evt, guild, channel, ctx);
}
private async ValueTask<bool> TryHandleLogClean(MessageCreateEvent evt, MessageContext ctx)
@@ -105,14 +105,16 @@ public class MessageCreated: IEventHandler<MessageCreateEvent>
return true;
}
private async ValueTask<bool> TryHandleCommand(Shard shard, MessageCreateEvent evt, Guild? guild,
private async ValueTask<bool> TryHandleCommand(int shardId, MessageCreateEvent evt, Guild? guild,
Channel channel, MessageContext ctx)
{
var content = evt.Content;
if (content == null) return false;
var ourUserId = await _cache.GetOwnUser();
// Check for command prefix
if (!HasCommandPrefix(content, shard.User?.Id ?? default, out var cmdStart) || cmdStart == content.Length)
if (!HasCommandPrefix(content, ourUserId, out var cmdStart) || cmdStart == content.Length)
return false;
// Trim leading whitespace from command without actually modifying the string
@@ -125,7 +127,7 @@ public class MessageCreated: IEventHandler<MessageCreateEvent>
{
var system = ctx.SystemId != null ? await _repo.GetSystem(ctx.SystemId.Value) : null;
var config = ctx.SystemId != null ? await _repo.GetSystemConfig(ctx.SystemId.Value) : null;
await _tree.ExecuteCommand(new Context(_services, shard, guild, channel, evt, cmdStart, system, config, ctx));
await _tree.ExecuteCommand(new Context(_services, shardId, guild, channel, evt, cmdStart, system, config, ctx));
}
catch (PKError)
{
@@ -156,14 +158,14 @@ public class MessageCreated: IEventHandler<MessageCreateEvent>
return false;
}
private async ValueTask<bool> TryHandleProxy(Shard shard, MessageCreateEvent evt, Guild guild, Channel channel,
private async ValueTask<bool> TryHandleProxy(MessageCreateEvent evt, Guild guild, Channel channel,
MessageContext ctx)
{
var botPermissions = await _cache.PermissionsIn(channel.Id);
try
{
return await _proxy.HandleIncomingMessage(shard, evt, ctx, guild, channel, ctx.AllowAutoproxy,
return await _proxy.HandleIncomingMessage(evt, ctx, guild, channel, ctx.AllowAutoproxy,
botPermissions);
}