示例#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));
        }
示例#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());
        }
示例#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);
 }
示例#4
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
                       ));
        }
示例#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
            );
        }
示例#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();
 }
示例#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
                ));
 }
示例#8
0
 public IAsyncEnumerable <IReadOnlyCollection <IUser> > GetReactionUsersAsync(IEmote emoji, int limit, RequestOptions options = null)
 => RestMessage.GetReactionUsersAsync(emoji, limit, options);
示例#9
0
 public Task RemoveAllReactionsAsync(RequestOptions options = null)
 => RestMessage.RemoveAllReactionsAsync(options);
示例#10
0
 public Task RemoveReactionAsync(IEmote emote, ulong userId, RequestOptions options = null)
 => RestMessage.RemoveReactionAsync(emote, userId, options);
示例#11
0
 public Task RemoveAllReactionsForEmoteAsync(IEmote emote, RequestOptions options = null)
 => RestMessage.RemoveAllReactionsForEmoteAsync(emote, options);
示例#12
0
 public Task AddReactionAsync(IEmote emote, RequestOptions options = null)
 => RestMessage.AddReactionAsync(emote, options);
示例#13
0
 /// <inheritdoc cref="RestMessage.ToString" />
 public override string ToString()
 => RestMessage.ToString();
示例#14
0
 /// <inheritdoc />
 public Task DeleteAsync(RequestOptions options = null)
 => RestMessage.DeleteAsync(options);
        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);
                }
            }
        }
示例#16
0
 /// <summary>
 /// Converts an existing <see cref="RestMessage"/> to an abstracted <see cref="IRestMessage"/> value.
 /// </summary>
 /// <param name="restMessage">The existing <see cref="RestMessage"/> to be abstracted.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restMessage"/>.</exception>
 /// <returns>An <see cref="IRestMessage"/> that abstracts <paramref name="restMessage"/>.</returns>
 public static IRestMessage Abstract(this RestMessage restMessage)
 => restMessage switch
 {
     null
     => throw new ArgumentNullException(nameof(restMessage)),
示例#17
0
 /// <summary>
 /// Constructs a new <see cref="RestMessageAbstraction"/> around an existing <see cref="Rest.RestMessage"/>.
 /// </summary>
 /// <param name="restMessage">The value to use for <see cref="Rest.RestMessage"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restMessage"/>.</exception>
 protected RestMessageAbstraction(RestMessage restMessage)
 {
     RestMessage = restMessage ?? throw new ArgumentNullException(nameof(restMessage));
 }
示例#18
0
 /// <inheritdoc />
 public Task UpdateAsync(RequestOptions options = null)
 => RestMessage.UpdateAsync(options);