Beispiel #1
0
            public RoleCollection(CachedGuild guild, ulong[] ids)
            {
                _guild = guild;
                _ids   = new List <Snowflake>(ids.Length + 1)
                {
                    guild.Id
                };
                for (var i = 0; i < ids.Length; i++)
                {
                    var id = ids[i];
                    if (!guild.Roles.ContainsKey(id))
                    {
                        continue;
                    }

                    _ids.Add(id);
                }
            }
        internal static CachedGuildChannel Create(CachedGuild guild, ChannelModel model)
        {
            switch (model.Type.Value)
            {
            case ChannelType.Text:
            case ChannelType.News:
            case ChannelType.Store:
                return(new CachedTextChannel(guild, model));

            case ChannelType.Voice:
                return(new CachedVoiceChannel(guild, model));

            case ChannelType.Category:
                return(new CachedCategoryChannel(guild, model));

            default:
                return(new CachedUnknownGuildChannel(guild, model));
            }
        }
Beispiel #3
0
        public Task HandleTypingStartedAsync(PayloadModel payload)
        {
            var                   model = payload.D.ToType <TypingStartModel>();
            CachedGuild           guild = null;
            ICachedMessageChannel channel;
            CachedUser            user;

            if (model.GuildId != null)
            {
                guild   = GetGuild(model.GuildId.Value);
                channel = guild.GetTextChannel(model.ChannelId);
                user    = guild.GetMember(model.UserId);
            }
            else
            {
                channel = GetPrivateChannel(model.ChannelId);
                user    = GetUser(model.UserId);
            }

            var userOptional = FetchableSnowflakeOptional.Create <CachedUser, RestUser, IUser>(
                model.UserId, channel is CachedTextChannel textChannel
                    ? textChannel.Guild.GetMember(model.UserId) ?? GetUser(model.UserId)
                    : GetUser(model.UserId),
                RestFetchable.Create((this, model), async(tuple, options) =>
            {
                var(@this, model) = tuple;
                return(model.GuildId != null
                        ? await @this._client.GetMemberAsync(model.GuildId.Value, model.UserId, options).ConfigureAwait(false)
                       ?? await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false)
                        : await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false));
            }));

            return(_client._typingStarted.InvokeAsync(new TypingStartedEventArgs(_client,
                                                                                 new SnowflakeOptional <ICachedMessageChannel>(channel, model.ChannelId),
                                                                                 userOptional,
                                                                                 DateTimeOffset.FromUnixTimeSeconds(model.Timestamp))));
        }
Beispiel #4
0
        public Task HandleInviteCreateAsync(PayloadModel payload)
        {
            var           model = Serializer.ToObject <InviteCreateModel>(payload.D);
            CachedGuild   guild = null;
            CachedChannel channel;
            CachedUser    inviter = null;

            if (model.GuildId != null)
            {
                guild   = GetGuild(model.GuildId.Value);
                channel = guild.GetChannel(model.ChannelId);

                if (model.Inviter.HasValue)
                {
                    inviter = guild.GetMember(model.Inviter.Value.Id);
                }
            }
            else
            {
                channel = GetPrivateChannel(model.ChannelId);

                if (model.Inviter.HasValue)
                {
                    inviter = GetUser(model.Inviter.Value.Id);
                }
            }

            if (inviter == null && model.Inviter.HasValue)
            {
                inviter = new CachedUnknownUser(_client, model.Inviter.Value);
            }

            return(_client._inviteCreated.InvokeAsync(new InviteCreatedEventArgs(
                                                          _client, guild, new SnowflakeOptional <CachedChannel>(channel, model.ChannelId),
                                                          inviter, model.Code, model.Temporary, model.MaxUses, model.MaxAge, model.CreatedAt)));
        }
Beispiel #5
0
        public Task HandleMessageUpdateAsync(PayloadModel payload)
        {
            var model = payload.D.ToType <MessageModel>();
            ICachedMessageChannel channel;
            CachedGuild           guild = null;

            if (model.GuildId != null)
            {
                guild   = GetGuild(model.GuildId.Value);
                channel = guild.GetTextChannel(model.ChannelId);
            }
            else
            {
                channel = GetPrivateChannel(model.ChannelId);
            }

            if (channel == null)
            {
                Log(LogSeverity.Warning, $"Uncached channel in MessageUpdated. Id: {model.ChannelId}");
                return(Task.CompletedTask);
            }

            var message   = channel.GetMessage(model.Id);
            var before    = message?.Clone();
            var isWebhook = model.WebhookId.HasValue;

            if (message == null)
            {
                CachedUser author = null;
                if (!model.Author.HasValue && !isWebhook)
                {
                    Log(LogSeverity.Warning, "Unknown message and author has no value in MessageUpdated.");
                    return(Task.CompletedTask);
                }
                else if (!isWebhook)
                {
                    if (guild != null)
                    {
                        if (guild.Members.TryGetValue(model.Author.Value.Id, out var member))
                        {
                            author = member;
                        }

                        else if (model.Member.HasValue)
                        {
                            author = GetOrAddMember(guild, model.Member.Value, model.Author.Value);
                        }
                    }
                    else
                    {
                        author = GetUser(model.Author.Value.Id);
                    }
                }
                else
                {
                    // TODO?
                    // (if isWebhook and no author)
                    return(Task.CompletedTask);
                }

                if (author == null)
                {
                    // TODO
                    Log(LogSeverity.Error, "Author is still null in MessageUpdate.");
                    return(Task.CompletedTask);
                }

                message = new CachedUserMessage(channel, author, model);
            }
            else
            {
                message.Update(model);
            }

            return(_client._messageUpdated.InvokeAsync(new MessageUpdatedEventArgs(channel,
                                                                                   new SnowflakeOptional <CachedUserMessage>(before, model.Id),
                                                                                   message)));
        }
Beispiel #6
0
 internal CachedMember GetOrCreateMember(CachedGuild guild, MemberModel memberModel, UserModel userModel, bool sync = false)
 => guild.GetOrAddMember(userModel.Id, _ => new CachedMember(GetOrAddSharedUser(userModel), guild, memberModel), sync);
Beispiel #7
0
 internal CachedNestedChannel(CachedGuild guild, ChannelModel model) : base(guild, model)
 {
 }
 internal CachedGuildChannel(CachedGuild guild, ChannelModel model) : base(guild.Client, model)
 {
     Guild = guild;
 }
Beispiel #9
0
 public CachedMember GetOrAddMember(CachedGuild guild, MemberModel memberModel, UserModel userModel, bool sync = false)
 => guild.GetOrAddMember(userModel.Id, _ => CreateMember(guild, memberModel, userModel), sync);
Beispiel #10
0
 internal CachedNestedChannel(DiscordClient client, ChannelModel model, CachedGuild guild) : base(client, model, guild)
 {
 }
Beispiel #11
0
 internal CachedRole(DiscordClient client, RoleModel model, CachedGuild guild) : base(client, model.Id)
 {
     Guild   = guild;
     Members = new ReadOnlyValuePredicateDictionary <Snowflake, CachedMember>(guild.Members, x => x.Roles.ContainsKey(Id));
     Update(model);
 }
Beispiel #12
0
 internal CachedCategoryChannel(DiscordClient client, ChannelModel model, CachedGuild guild) : base(client, model, guild)
 {
     Channels = new ReadOnlyValuePredicateDictionary <Snowflake, CachedNestedChannel>(guild.NestedChannels, x => x.CategoryId == Id);
     Update(model);
 }
Beispiel #13
0
        internal static CachedGuildChannel Create(DiscordClient client, ChannelModel model, CachedGuild guild)
        {
            switch (model.Type.Value)
            {
            case ChannelType.Text:
            case ChannelType.News:
                return(new CachedTextChannel(client, model, guild));

            case ChannelType.Voice:
                return(new CachedVoiceChannel(client, model, guild));

            case ChannelType.Category:
                return(new CachedCategoryChannel(client, model, guild));

            default:
                return(null);
            }
        }
Beispiel #14
0
 internal CachedGuildChannel(DiscordClient client, ChannelModel model, CachedGuild guild) : base(client, model)
 {
     Guild = guild;
 }
 internal CachedUnknownGuildChannel(CachedGuild guild, ChannelModel model) : base(guild, model)
 {
     Type = (byte)model.Type.Value;
 }
Beispiel #16
0
 internal CachedTextChannel(DiscordClient client, ChannelModel model, CachedGuild guild) : base(client, model, guild)
 {
     Update(model);
 }
Beispiel #17
0
 public CachedMember CreateMember(CachedGuild guild, MemberModel memberModel, UserModel userModel)
 => new CachedMember(GetOrAddSharedUser(userModel), guild, memberModel);
Beispiel #18
0
 public RoleCollection(RoleCollection collection)
 {
     _guild = collection._guild;
     _ids   = new List <Snowflake>(collection._ids);
 }
Beispiel #19
0
 internal CachedVoiceChannel(DiscordClient client, ChannelModel model, CachedGuild guild) : base(client, model, guild)
 {
     Members = new ReadOnlyValuePredicateDictionary <Snowflake, CachedMember>(guild._members, x => x.VoiceChannelId == Id);
     Update(model);
 }
Beispiel #20
0
        internal CachedRole(CachedGuild guild, RoleModel model) : base(guild.Client, model.Id)
        {
            Guild = guild;

            Update(model);
        }
 internal CachedCategoryChannel(CachedGuild guild, ChannelModel model) : base(guild, model)
 {
     Update(model);
 }
Beispiel #22
0
 internal CachedVoiceChannel(CachedGuild guild, ChannelModel model) : base(guild, model)
 {
     Update(model);
 }