Exemple #1
0
        /// <summary>
        ///     Attempts to cache the given message.
        /// </summary>
        /// <param name="message"> The message to cache. </param>
        /// <returns>
        ///     A <see cref="bool"/> indicating whether the message was cached.
        /// </returns>
        public override bool TryAddMessage(CachedUserMessage message)
        {
            var cache = _caches.GetOrAdd(message.Channel.Id, (_, capacity) => new CircularBuffer <CachedUserMessage>(capacity), Capacity);

            cache.Add(message);
            return(true);
        }
Exemple #2
0
        /// <summary>
        ///     Attempts to fetch a message from the cache with the specified id, for the specified channel.
        /// </summary>
        /// <param name="channelId"> The message's channel's id. </param>
        /// <param name="messageId"> The id of the message. </param>
        /// <param name="message"> The optional cached message. </param>
        /// <returns>
        ///     A <see cref="bool"/> indicating whether the message was present in the cache.
        /// </returns>
        public override bool TryGetMessage(Snowflake channelId, Snowflake messageId, out CachedUserMessage message)
        {
            if (!_caches.TryGetValue(channelId, out var cache))
            {
                message = null;
                return(false);
            }

            message = cache.Find(x => x.Id == messageId);
            return(message != null);
        }
Exemple #3
0
        public Task HandleMessageDeleteAsync(PayloadModel payload)
        {
            var model   = payload.D.ToType <MessageDeleteModel>();
            var channel = model.GuildId != null
                ? GetGuildChannel(model.ChannelId) as ICachedMessageChannel
                : GetPrivateChannel(model.ChannelId);

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

            CachedUserMessage message = null;

            _messageCache?.TryRemoveMessage(channel.Id, model.Id, out message);

            return(_client._messageDeleted.InvokeAsync(new MessageDeletedEventArgs(channel,
                                                                                   new SnowflakeOptional <CachedUserMessage>(message, model.Id))));
        }
        public Task HandleMessageDeleteBulkAsync(PayloadModel payload)
        {
            var model = Serializer.ToObject <MessageDeleteBulkModel>(payload.D);

            if (model.GuildId == null)
            {
                Log(LogMessageSeverity.Error, $"MessageDeleteBulk contains a null guild_id. Channel id: {model.ChannelId}.");
                return(Task.CompletedTask);
            }

            var guild    = GetGuild(model.GuildId.Value);
            var channel  = guild.GetTextChannel(model.ChannelId);
            var messages = new SnowflakeOptional <CachedUserMessage> [model.Ids.Length];

            for (var i = 0; i < model.Ids.Length; i++)
            {
                var id = model.Ids[i];
                CachedUserMessage message = null;
                _messageCache?.TryRemoveMessage(channel.Id, id, out message);
                messages[i] = new SnowflakeOptional <CachedUserMessage>(message, id);
            }

            return(_client._messagesBulkDeleted.InvokeAsync(new MessagesBulkDeletedEventArgs(channel, messages.ReadOnly())));
        }
Exemple #5
0
 /// <inheritdoc/>
 public override bool TryRemoveMessage(Snowflake channelId, Snowflake messageId, out CachedUserMessage message)
 {
     message = null;
     return(false);
 }
Exemple #6
0
 public override bool TryAddMessage(CachedUserMessage message)
 => false;
Exemple #7
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)));
        }
Exemple #8
0
 /// <summary>
 ///     Attempts to remove a message from the cache with the specified id, for the specified channel.
 /// </summary>
 /// <param name="channelId"> The id of the message's channel. </param>
 /// <param name="messageId"> The id of the message. </param>
 /// <param name="message"> The optional cached message. </param>
 /// <returns>
 ///     A <see cref="bool"/> indicating whether the message was present in the cache.
 /// </returns>
 public abstract bool TryRemoveMessage(Snowflake channelId, Snowflake messageId, out CachedUserMessage message);
Exemple #9
0
 /// <summary>
 ///     Attempts to cache the given message.
 /// </summary>
 /// <param name="message"> The message to cache. </param>
 /// <returns>
 ///     A <see cref="bool"/> indicating whether the message was cached.
 /// </returns>
 public abstract bool TryAddMessage(CachedUserMessage message);
Exemple #10
0
        /// <summary>
        ///     Attempts to remove a message from the cache with the specified id, for the specified channel.
        /// </summary>
        /// <param name="channelId"> The message's channel's id. </param>
        /// <param name="messageId"> The id of the message. </param>
        /// <param name="message"> The optional cached message. </param>
        /// <returns>
        ///     A <see cref="bool"/> indicating whether the message was present in the cache.
        /// </returns>
        public override bool TryRemoveMessage(Snowflake channelId, Snowflake messageId, out CachedUserMessage message)
        {
            if (!_caches.TryGetValue(channelId, out var cache) || !cache.TryRemove(x => x.Id == messageId, out message))
            {
                message = null;
                return(false);
            }

            return(true);
        }