Пример #1
0
        public async Task <IActionResult> GetChannelMessagesAsync(Snowflake channelId, [FromQuery] Dictionary <string, string> queryMap)
        {
            var args = new GetChannelMessagesParams();

            args.LoadQueryMap(queryMap);
            args.Validate();

            var msg = new Message
            {
                ChannelId = channelId
            };

            if (args.Before.IsSpecified)
            {
                msg.Id = args.Before.Value;
            }
            if (args.Around.IsSpecified)
            {
                msg.Id = args.Around.Value;
            }
            if (args.After.IsSpecified)
            {
                msg.Id = args.After.Value;
            }

            return(Ok(new[] { msg }));
        }
Пример #2
0
        public virtual async Task <IReadOnlyCollection <IMessage> > GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
        {
            var args = new GetChannelMessagesParams {
                Limit = limit, RelativeMessageId = fromMessageId, RelativeDirection = dir
            };
            var models = await Discord.ApiClient.GetChannelMessagesAsync(Id, args).ConfigureAwait(false);

            return(models.Select(x => CreateIncomingMessage(x)).ToImmutableArray());
        }
Пример #3
0
        public static IAsyncEnumerable <IReadOnlyCollection <RestMessage> > GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                                             ulong?fromMessageId, Direction dir, int limit, RequestOptions options)
        {
            if (dir == Direction.Around)
            {
                throw new NotImplementedException(); //TODO: Impl
            }
            var guildId = (channel as IGuildChannel)?.GuildId;
            var guild   = guildId != null ? (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;

            return(new PagedAsyncEnumerable <RestMessage>(
                       DiscordConfig.MaxMessagesPerBatch,
                       async(info, ct) =>
            {
                var args = new GetChannelMessagesParams
                {
                    RelativeDirection = dir,
                    Limit = info.PageSize
                };
                if (info.Position != null)
                {
                    args.RelativeMessageId = info.Position.Value;
                }

                var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
                var builder = ImmutableArray.CreateBuilder <RestMessage>();
                foreach (var model in models)
                {
                    var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
                    builder.Add(RestMessage.Create(client, channel, author, model));
                }
                return builder.ToImmutable();
            },
                       nextPage: (info, lastPage) =>
            {
                if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
                {
                    return false;
                }
                if (dir == Direction.Before)
                {
                    info.Position = lastPage.Min(x => x.Id);
                }
                else
                {
                    info.Position = lastPage.Max(x => x.Id);
                }
                return true;
            },
                       start: fromMessageId,
                       count: limit
                       ));
        }
Пример #4
0
        public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
            ulong? fromMessageId, Direction dir, int limit, RequestOptions options)
        {
            var guildId = (channel as IGuildChannel)?.GuildId;
            var guild = guildId != null ? (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;

            if (dir == Direction.Around && limit > DiscordConfig.MaxMessagesPerBatch)
            {
                int around = limit / 2;
                if (fromMessageId.HasValue)
                    return GetMessagesAsync(channel, client, fromMessageId.Value + 1, Direction.Before, around + 1, options) //Need to include the message itself
                        .Concat(GetMessagesAsync(channel, client, fromMessageId, Direction.After, around, options));
                else //Shouldn't happen since there's no public overload for ulong? and Direction
                    return GetMessagesAsync(channel, client, null, Direction.Before, around + 1, options);
            }

            return new PagedAsyncEnumerable<RestMessage>(
                DiscordConfig.MaxMessagesPerBatch,
                async (info, ct) =>
                {
                    var args = new GetChannelMessagesParams
                    {
                        RelativeDirection = dir,
                        Limit = info.PageSize
                    };
                    if (info.Position != null)
                        args.RelativeMessageId = info.Position.Value;

                    var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
                    var builder = ImmutableArray.CreateBuilder<RestMessage>();
                    foreach (var model in models)
                    {
                        var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
                        builder.Add(RestMessage.Create(client, channel, author, model));
                    }
                    return builder.ToImmutable();
                },
                nextPage: (info, lastPage) =>
                {
                    if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
                        return false;
                    if (dir == Direction.Before)
                        info.Position = lastPage.Min(x => x.Id);
                    else
                        info.Position = lastPage.Max(x => x.Id);
                    return true;
                },
                start: fromMessageId,
                count: limit
            );
        }
Пример #5
0
 public static IAsyncEnumerable <IReadOnlyCollection <RestMessage> > GetMessagesAsync(IChannel channel, BaseDiscordClient client,
                                                                                      ulong?fromMessageId, Direction dir, int limit, IGuild guild, RequestOptions options)
 {
     if (dir == Direction.Around)
     {
         throw new NotImplementedException(); //TODO: Impl
     }
     return(new PagedAsyncEnumerable <RestMessage>(
                DiscordConfig.MaxMessagesPerBatch,
                async(info, ct) =>
     {
         var args = new GetChannelMessagesParams
         {
             RelativeDirection = dir,
             Limit = info.PageSize
         };
         if (info.Position != null)
         {
             args.RelativeMessageId = info.Position.Value;
         }
         var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
         return models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray();
     },
                nextPage: (info, lastPage) =>
     {
         if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
         {
             return false;
         }
         if (dir == Direction.Before)
         {
             info.Position = lastPage.Min(x => x.Id);
         }
         else
         {
             info.Position = lastPage.Max(x => x.Id);
         }
         return true;
     },
                start: fromMessageId,
                count: limit
                ));
 }
Пример #6
0
        public async Task <IReadOnlyCollection <ISocketMessage> > DownloadAsync(ulong?fromId, Direction dir, int limit)
        {
            //TODO: Test heavily, especially the ordering of messages
            if (limit < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(limit));
            }
            if (limit == 0)
            {
                return(ImmutableArray <ISocketMessage> .Empty);
            }

            var cachedMessages = GetMany(fromId, dir, limit);

            if (cachedMessages.Count == limit)
            {
                return(cachedMessages);
            }
            else if (cachedMessages.Count > limit)
            {
                return(cachedMessages.Skip(cachedMessages.Count - limit).ToImmutableArray());
            }
            else
            {
                var args = new GetChannelMessagesParams
                {
                    Limit             = limit - cachedMessages.Count,
                    RelativeDirection = dir
                };
                if (cachedMessages.Count == 0)
                {
                    if (fromId != null)
                    {
                        args.RelativeMessageId = fromId.Value;
                    }
                }
                else
                {
                    args.RelativeMessageId = dir == Direction.Before ? cachedMessages[0].Id : cachedMessages[cachedMessages.Count - 1].Id;
                }
                var downloadedMessages = await _discord.ApiClient.GetChannelMessagesAsync(_channel.Id, args).ConfigureAwait(false);

                var guild = (_channel as ISocketGuildChannel)?.Guild;
                return(cachedMessages.Concat(downloadedMessages.Select(x =>
                {
                    IUser user = _channel.GetUser(x.Author.Value.Id, true);
                    if (user == null)
                    {
                        var newUser = new User(x.Author.Value);
                        if (guild != null)
                        {
                            user = new GuildUser(guild, newUser);
                        }
                        else
                        {
                            user = newUser;
                        }
                    }
                    return Create(user, x);
                })).ToImmutableArray());
            }
        }