Exemple #1
0
        //Messages
        public static async Task <RestMessage> GetMessageAsync(IChannel channel, BaseDiscordClient client,
                                                               ulong id, IGuild guild, RequestOptions options)
        {
            var model = await client.ApiClient.GetChannelMessageAsync(channel.Id, id, options).ConfigureAwait(false);

            return(RestMessage.Create(client, guild, model));
        }
Exemple #2
0
        public static async Task <IReadOnlyCollection <RestMessage> > GetPinnedMessagesAsync(IChannel channel, BaseDiscordClient client,
                                                                                             IGuild guild, RequestOptions options)
        {
            var models = await client.ApiClient.GetPinsAsync(channel.Id, options).ConfigureAwait(false);

            return(models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray());
        }
Exemple #3
0
 //Messages
 public static async Task<RestMessage> GetMessageAsync(IMessageChannel channel, BaseDiscordClient client,
     ulong id, RequestOptions options)
 {
     var guildId = (channel as IGuildChannel)?.GuildId;
     var guild = guildId != null ? await (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;
     var model = await client.ApiClient.GetChannelMessageAsync(channel.Id, id, options).ConfigureAwait(false);
     if (model == null)
         return null;
     var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
     return RestMessage.Create(client, channel, author, model);
 }
        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
                       ));
        }
Exemple #5
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
            );
        }
Exemple #6
0
 public static async Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
     RequestOptions options)
 {
     var guildId = (channel as IGuildChannel)?.GuildId;
     var guild = guildId != null ? await (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;
     var models = await client.ApiClient.GetPinsAsync(channel.Id, 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();
 }
Exemple #7
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
                ));
 }
        internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model)
        {
            var resolved = model.Resolved.Value;

            if (resolved.Users.IsSpecified)
            {
                foreach (var user in resolved.Users.Value)
                {
                    var restUser = RestUser.Create(discord, user.Value);

                    Users.Add(ulong.Parse(user.Key), restUser);
                }
            }

            if (resolved.Channels.IsSpecified)
            {
                var channels = await guild.GetChannelsAsync().ConfigureAwait(false);

                foreach (var channelModel in resolved.Channels.Value)
                {
                    var restChannel = channels.FirstOrDefault(x => x.Id == channelModel.Value.Id);

                    restChannel.Update(channelModel.Value);

                    Channels.Add(ulong.Parse(channelModel.Key), restChannel);
                }
            }

            if (resolved.Members.IsSpecified)
            {
                foreach (var member in resolved.Members.Value)
                {
                    // pull the adjacent user model
                    member.Value.User = resolved.Users.Value.FirstOrDefault(x => x.Key == member.Key).Value;
                    var restMember = RestGuildUser.Create(discord, guild, member.Value);

                    GuildMembers.Add(ulong.Parse(member.Key), restMember);
                }
            }

            if (resolved.Roles.IsSpecified)
            {
                foreach (var role in resolved.Roles.Value)
                {
                    var restRole = RestRole.Create(discord, guild, role.Value);

                    Roles.Add(ulong.Parse(role.Key), restRole);
                }
            }

            if (resolved.Messages.IsSpecified)
            {
                foreach (var msg in resolved.Messages.Value)
                {
                    channel ??= (IRestMessageChannel)(Channels.FirstOrDefault(x => x.Key == msg.Value.ChannelId).Value ?? await discord.GetChannelAsync(msg.Value.ChannelId).ConfigureAwait(false));

                    RestUser author;

                    if (msg.Value.Author.IsSpecified)
                    {
                        author = RestUser.Create(discord, msg.Value.Author.Value);
                    }
                    else
                    {
                        author = RestGuildUser.Create(discord, guild, msg.Value.Member.Value);
                    }

                    var message = RestMessage.Create(discord, channel, author, msg.Value);

                    Messages.Add(message.Id, message);
                }
            }

            if (resolved.Attachments.IsSpecified)
            {
                foreach (var attachment in resolved.Attachments.Value)
                {
                    var discordAttachment = Attachment.Create(attachment.Value);

                    Attachments.Add(ulong.Parse(attachment.Key), discordAttachment);
                }
            }
        }