public static ValueTask HandleGatewayEvent(this IDiscordCache cache, IGatewayEvent evt)
        {
            switch (evt)
            {
            case GuildCreateEvent gc:
                return(cache.SaveGuildCreate(gc));

            case GuildUpdateEvent gu:
                return(cache.SaveGuild(gu));

            case GuildDeleteEvent gd:
                return(cache.RemoveGuild(gd.Id));

            case ChannelCreateEvent cc:
                return(cache.SaveChannel(cc));

            case ChannelUpdateEvent cu:
                return(cache.SaveChannel(cu));

            case ChannelDeleteEvent cd:
                return(cache.RemoveChannel(cd.Id));

            case GuildRoleCreateEvent grc:
                return(cache.SaveRole(grc.GuildId, grc.Role));

            case GuildRoleUpdateEvent gru:
                return(cache.SaveRole(gru.GuildId, gru.Role));

            case GuildRoleDeleteEvent grd:
                return(cache.RemoveRole(grd.GuildId, grd.RoleId));

            case MessageReactionAddEvent mra:
                return(cache.TrySaveDmChannelStub(mra.GuildId, mra.ChannelId));

            case MessageCreateEvent mc:
                return(cache.SaveMessageCreate(mc));

            case MessageUpdateEvent mu:
                return(cache.TrySaveDmChannelStub(mu.GuildId.Value, mu.ChannelId));

            case MessageDeleteEvent md:
                return(cache.TrySaveDmChannelStub(md.GuildId, md.ChannelId));

            case MessageDeleteBulkEvent md:
                return(cache.TrySaveDmChannelStub(md.GuildId, md.ChannelId));

            case ThreadCreateEvent tc:
                return(cache.SaveChannel(tc));

            case ThreadUpdateEvent tu:
                return(cache.SaveChannel(tu));

            case ThreadDeleteEvent td:
                return(cache.RemoveChannel(td.Id));

            case ThreadListSyncEvent tls:
                return(cache.SaveThreadListSync(tls));
            }

            return(default);
Example #2
0
 private async Task OnEventReceivedInner(int shardId, IGatewayEvent evt)
 {
     // HandleEvent takes a type parameter, automatically inferred by the event type
     // 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
     if (evt is MessageCreateEvent mc)
     {
         await HandleEvent(shardId, mc);
     }
     if (evt is MessageUpdateEvent mu)
     {
         await HandleEvent(shardId, mu);
     }
     if (evt is MessageDeleteEvent md)
     {
         await HandleEvent(shardId, md);
     }
     if (evt is MessageDeleteBulkEvent mdb)
     {
         await HandleEvent(shardId, mdb);
     }
     if (evt is MessageReactionAddEvent mra)
     {
         await HandleEvent(shardId, mra);
     }
     if (evt is InteractionCreateEvent ic)
     {
         await HandleEvent(shardId, ic);
     }
 }
Example #3
0
 private async Task HandleEvent(IGatewayEvent evt)
 {
     if (OnEventReceived != null)
     {
         await OnEventReceived.Invoke(evt);
     }
 }
Example #4
0
    private async Task OnEventReceived(int shardId, IGatewayEvent evt)
    {
        // we HandleGatewayEvent **before** getting the own user, because the own user is set in HandleGatewayEvent for ReadyEvent
        await _cache.HandleGatewayEvent(evt);

        var userId = await _cache.GetOwnUser();

        await _cache.TryUpdateSelfMember(userId, evt);

        await OnEventReceivedInner(shardId, evt);
    }
Example #5
0
        private void PersistEvent(IGatewayEvent e)
        {
            // persist event
            Persist(e, UpdateState);

            // check if it's turn to save at-least-once-delivery state into snapshot store
            counter = (counter + 1) % SnapshotInterval;
            if (counter == 0)
            {
                var snapshot = GetDeliverySnapshot();
                SaveSnapshot(snapshot);
            }
        }
Example #6
0
    public async Task <ILogEventEnricher> GetEnricher(int shardId, IGatewayEvent evt)
    {
        var props = new List <LogEventProperty> {
            new("ShardId", new ScalarValue(shardId))
        };

        if (_botConfig.Cluster != null)
        {
            props.Add(new LogEventProperty("ClusterId", new ScalarValue(_botConfig.Cluster.NodeName)));
        }

        var(guild, channel) = GetGuildChannelId(evt);
        var user    = GetUserId(evt);
        var message = GetMessageId(evt);

        if (guild != null)
        {
            props.Add(new LogEventProperty("GuildId", new ScalarValue(guild.Value)));
        }

        if (channel != null)
        {
            props.Add(new LogEventProperty("ChannelId", new ScalarValue(channel.Value)));

            if (await _cache.TryGetChannel(channel.Value) != null)
            {
                var botPermissions = await _cache.PermissionsIn(channel.Value);

                props.Add(new LogEventProperty("BotPermissions", new ScalarValue(botPermissions)));
            }
        }

        if (message != null)
        {
            props.Add(new LogEventProperty("MessageId", new ScalarValue(message.Value)));
        }

        if (user != null)
        {
            props.Add(new LogEventProperty("UserId", new ScalarValue(user.Value)));
        }

        if (evt is MessageCreateEvent mce)
        {
            props.Add(new LogEventProperty("UserPermissions", new ScalarValue(await _cache.PermissionsFor(mce))));
        }

        return(new Inner(props));
    }
Example #7
0
        public ILogEventEnricher GetEnricher(Shard shard, IGatewayEvent evt)
        {
            var props = new List <LogEventProperty>
            {
                new("ShardId", new ScalarValue(shard.ShardId)),
            };

            var(guild, channel) = GetGuildChannelId(evt);
            var user    = GetUserId(evt);
            var message = GetMessageId(evt);

            if (guild != null)
            {
                props.Add(new("GuildId", new ScalarValue(guild.Value)));
            }

            if (channel != null)
            {
                props.Add(new("ChannelId", new ScalarValue(channel.Value)));

                if (_cache.TryGetChannel(channel.Value, out _))
                {
                    var botPermissions = _bot.PermissionsIn(channel.Value);
                    props.Add(new("BotPermissions", new ScalarValue(botPermissions)));
                }
            }

            if (message != null)
            {
                props.Add(new("MessageId", new ScalarValue(message.Value)));
            }

            if (user != null)
            {
                props.Add(new("UserId", new ScalarValue(user.Value)));
            }

            if (evt is MessageCreateEvent mce)
            {
                props.Add(new("UserPermissions", new ScalarValue(_cache.PermissionsFor(mce))));
            }

            return(new Inner(props));
        }
Example #8
0
    private async Task OnEventReceived(int shardId, IGatewayEvent evt)
    {
        // we HandleGatewayEvent **before** getting the own user, because the own user is set in HandleGatewayEvent for ReadyEvent
        await _cache.HandleGatewayEvent(evt);

        var userId = await _cache.GetOwnUser();

        await _cache.TryUpdateSelfMember(userId, evt);

        // HandleEvent takes a type parameter, automatically inferred by the event type
        // 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
        if (evt is MessageCreateEvent mc)
        {
            await HandleEvent(shardId, mc);
        }
        if (evt is MessageUpdateEvent mu)
        {
            await HandleEvent(shardId, mu);
        }
        if (evt is MessageDeleteEvent md)
        {
            await HandleEvent(shardId, md);
        }
        if (evt is MessageDeleteBulkEvent mdb)
        {
            await HandleEvent(shardId, mdb);
        }
        if (evt is MessageReactionAddEvent mra)
        {
            await HandleEvent(shardId, mra);
        }
        if (evt is InteractionCreateEvent ic)
        {
            await HandleEvent(shardId, ic);
        }
    }
Example #9
0
 private (ulong?, ulong?) GetGuildChannelId(IGatewayEvent evt) => evt switch
 {
Example #10
0
 private void UpdateState(IGatewayEvent message) => message.Match()
 // once message sent request has been stored, start delivery procedure to recipient
 .With <DeliverySent <T> >(sent => Deliver(sent.Recipient, deliveryId => new Deliver <T>(deliveryId, sent.Payload)))
 // once message confirmation has been stored, officially confirm that delivery
 .With <DeliveryConfirmed>(confirmed => ConfirmDelivery(confirmed.DeliveryId));