Esempio n. 1
0
        public virtual void Update(MessageJsonModel model)
        {
            if (_transientAuthor != null)
            {
                if (model.Member.HasValue)
                {
                    model.Member.Value.User = model.Author;
                    _transientAuthor        = new TransientMember(Client, GuildId.Value, model.Member.Value);
                }
                else
                {
                    _transientAuthor = new TransientUser(Client, model.Author);
                }
            }
            Content        = model.Content;
            MentionedUsers = model.Mentions.ToReadOnlyList(Client, (x, client) =>
            {
                var user = client.GetUser(x.Id);
                if (user != null)
                {
                    return(user);
                }

                return(new TransientUser(client, x) as IUser);
            });
            Reactions = Optional.Convert(model.Reactions, x => x.ToReadOnlyDictionary(x => Emoji.Create(x.Emoji), x => new Reaction(x)));
        }
Esempio n. 2
0
        public void Update(GuildScheduledEventJsonModel model)
        {
            if (model.Creator.HasValue)
            {
                Creator = new TransientUser(Client, model.Creator.Value);
            }

            ChannelId      = model.ChannelId;
            Name           = model.Name;
            Description    = model.Description.Value;
            CoverImageHash = model.Image;
            StartsAt       = model.ScheduledStartTime;
            EndsAt         = model.ScheduledEndTime;
            PrivacyLevel   = model.PrivacyLevel;
            Status         = model.Status;
            TargetType     = model.EntityType;
            TargetId       = model.EntityId;

            if (model.EntityMetadata != null)
            {
                Metadata = new TransientGuildEventMetadata(model.EntityMetadata);
            }

            SubscriberCount = model.UserCount.GetValueOrNullable();
        }
Esempio n. 3
0
        public override ValueTask <MemberLeftEventArgs> HandleDispatchAsync(IGatewayApiClient shard, GuildMemberRemoveJsonModel model)
        {
            IUser user;

            if (CacheProvider.TryGetMembers(model.GuildId, out var cache) && cache.TryRemove(model.User.Id, out var cachedMember))
            {
                user = cachedMember;
            }
            else
            {
                user = new TransientUser(Client, model.User);
            }

            var e = new MemberLeftEventArgs(model.GuildId, user);

            return(new(e));
        }
Esempio n. 4
0
        protected CachedMessage(IGatewayClient client, CachedMember author, MessageJsonModel model)
            : base(client, model.Id)
        {
            ChannelId = model.ChannelId;
            GuildId   = model.GuildId.GetValueOrNullable();
            if (author != null)
            {
                _cachedAuthor = author;
            }
            else
            {
                if (model.Member.HasValue)
                {
                    model.Member.Value.User = model.Author;
                    _transientAuthor        = new TransientMember(Client, GuildId.Value, model.Member.Value);
                }
                else
                {
                    _transientAuthor = new TransientUser(Client, model.Author);
                }
            }

            Update(model);
        }
Esempio n. 5
0
        public void Update(MessageUpdateJsonModel model)
        {
            if (model.Author.HasValue)
            {
                if (_transientAuthor != null)
                {
                    if (model.Member.HasValue)
                    {
                        model.Member.Value.User = model.Author;
                        _transientAuthor        = new TransientMember(Client, GuildId.Value, model.Member.Value);
                    }
                    else
                    {
                        _transientAuthor = new TransientUser(Client, model.Author.Value);
                    }
                }
            }

            if (model.Content.HasValue)
            {
                Content = model.Content.Value;
            }

            if (model.Mentions.HasValue)
            {
                MentionedUsers = model.Mentions.Value.ToReadOnlyList(Client, (x, client) =>
                {
                    var user = client.GetUser(x.Id);
                    if (user != null)
                    {
                        return(user);
                    }

                    return(new TransientUser(client, x) as IUser);
                });
            }

            if (model.Reactions.HasValue)
            {
                Reactions = Optional.Convert(model.Reactions, x => x.ToReadOnlyDictionary(x => Emoji.Create(x.Emoji), x => new MessageReaction(x)));
            }

            if (model.EditedTimestamp.HasValue)
            {
                EditedAt = model.EditedTimestamp.Value;
            }

            if (model.Pinned.HasValue)
            {
                IsPinned = model.Pinned.Value;
            }

            if (model.MentionEveryone.HasValue)
            {
                MentionsEveryone = model.MentionEveryone.Value;
            }

            if (model.MentionRoles.HasValue)
            {
                MentionedRoleIds = model.MentionRoles.Value.ReadOnly();
            }

            if (model.Attachments.HasValue)
            {
                Attachments = model.Attachments.Value.ToReadOnlyList(x => new Attachment(x));
            }

            if (model.Embeds.HasValue)
            {
                Embeds = model.Embeds.Value.ToReadOnlyList(x => new Embed(x));
            }

            if (model.Activity.HasValue)
            {
                Activity = Optional.ConvertOrDefault(model.Activity, x => new MessageActivity(x));
            }

            if (model.Application.HasValue)
            {
                Application = Optional.ConvertOrDefault(model.Application, x => new MessageApplication(x));
            }

            if (model.MessageReference.HasValue)
            {
                Reference = Optional.ConvertOrDefault(model.MessageReference, x => new MessageReference(x));
            }

            if (model.Flags.HasValue)
            {
                Flags = model.Flags.Value;
            }

            if (model.ReferencedMessage.HasValue)
            {
                ReferencedMessage = Optional.Convert(model.ReferencedMessage, x => new TransientUserMessage(Client, x) as IUserMessage);
            }

            if (model.Components.HasValue)
            {
                Components = Optional.ConvertOrDefault(model.Components, (models, client) => models.ToReadOnlyList(client, (model, client) => TransientComponent.Create(client, model)), Client) ?? Array.Empty <IComponent>();
            }

            if (model.StickerItems.HasValue)
            {
                Stickers = Optional.ConvertOrDefault(model.StickerItems, x => x.ToReadOnlyList(y => new MessageSticker(y)), Array.Empty <MessageSticker>());
            }
        }
        public TransientGuildAuditLogChanges(IClient client, AuditLogJsonModel auditLogJsonModel, AuditLogEntryJsonModel model)
        {
            for (var i = 0; i < model.Changes.Value.Length; i++)
            {
                var change = model.Changes.Value[i];
                switch (change.Key)
                {
                case "name":
                {
                    Name = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "description":
                {
                    Description = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "icon_hash":
                {
                    IconHash = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "splash_hash":
                {
                    SplashHash = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "discovery_splash_hash":
                {
                    DiscoverySplashHash = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "banner_hash":
                {
                    BannerHash = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "owner_id":
                {
                    OwnerId = AuditLogChange <Snowflake> .Convert(change);

                    IUser oldOwner      = null;
                    var   oldOwnerModel = Array.Find(auditLogJsonModel.Users, x => x.Id == OwnerId.OldValue.Value);
                    if (oldOwnerModel != null)
                    {
                        oldOwner = new TransientUser(client, oldOwnerModel);
                    }

                    IUser newOwner      = null;
                    var   newOwnerModel = Array.Find(auditLogJsonModel.Users, x => x.Id == OwnerId.NewValue.Value);
                    if (newOwnerModel != null)
                    {
                        newOwner = new TransientUser(client, newOwnerModel);
                    }

                    Owner = new AuditLogChange <Optional <IUser> >(Optional.FromNullable(oldOwner), Optional.FromNullable(newOwner));
                    break;
                }

                case "region":
                {
                    VoiceRegion = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "preferred_locale":
                {
                    PreferredLocale = AuditLogChange <CultureInfo> .Convert <string>(change, Discord.Internal.GetLocale);

                    break;
                }

                case "afk_channel_id":
                {
                    AfkChannelId = AuditLogChange <Snowflake?> .Convert(change);

                    break;
                }

                case "afk_timeout":
                {
                    AfkTimeout = AuditLogChange <int> .Convert(change);

                    break;
                }

                case "rules_channel_id":
                {
                    RulesChannelId = AuditLogChange <Snowflake?> .Convert(change);

                    break;
                }

                case "public_updates_channel_id":
                {
                    PublicUpdatesChannelId = AuditLogChange <Snowflake?> .Convert(change);

                    break;
                }

                case "mfa_level":
                {
                    MfaLevel = AuditLogChange <GuildMfaLevel> .Convert(change);

                    break;
                }

                case "verification_level":
                {
                    VerificationLevel = AuditLogChange <GuildVerificationLevel> .Convert(change);

                    break;
                }

                case "explicit_content_filter":
                {
                    ContentFilterLevel = AuditLogChange <GuildContentFilterLevel> .Convert(change);

                    break;
                }

                case "default_message_notifications":
                {
                    DefaultNotificationLevel = AuditLogChange <GuildNotificationLevel> .Convert(change);

                    break;
                }

                case "vanity_url_code":
                {
                    VanityUrlCode = AuditLogChange <string> .Convert(change);

                    break;
                }

                case "prune_days":
                {
                    PruneDays = AuditLogChange <int?> .Convert(change);

                    break;
                }

                case "widget_enabled":
                {
                    IsWidgetEnabled = AuditLogChange <bool> .Convert(change);

                    break;
                }

                case "widget_channel_id":
                {
                    WidgetChannelId = AuditLogChange <Snowflake?> .Convert(change);

                    break;
                }

                case "system_channel_id":
                {
                    SystemChannelId = AuditLogChange <Snowflake?> .Convert(change);

                    break;
                }

                default:
                {
                    client.Logger.LogDebug("Unknown key {0} for {1}", change.Key, this);
                    break;
                }
                }
            }
        }