コード例 #1
0
        public static async Task <RestChannel> GetChannelAsync(BaseDiscordClient client,
                                                               ulong id, RequestOptions options)
        {
            var model = await client.ApiClient.GetChannelAsync(id, options).ConfigureAwait(false);

            if (model != null)
            {
                return(RestChannel.Create(client, model));
            }
            return(null);
        }
コード例 #2
0
        /// <exception cref="InvalidOperationException">Unexpected channel type.</exception>
        public static async Task <IReadOnlyCollection <IRestPrivateChannel> > GetPrivateChannelsAsync(BaseDiscordClient client, RequestOptions options)
        {
            var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);

            return(models.Select(x => RestChannel.CreatePrivate(client, x)).ToImmutableArray());
        }
コード例 #3
0
ファイル: IRestChannel.cs プロジェクト: Scott-Caldwell/MODiX
 /// <summary>
 /// Converts an existing <see cref="RestChannel"/> to an abstracted <see cref="IRestChannel"/> value.
 /// </summary>
 /// <param name="restChannel">The existing <see cref="RestChannel"/> to be abstracted.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restChannel"/>.</exception>
 /// <returns>An <see cref="IRestChannel"/> that abstracts <paramref name="restChannel"/>.</returns>
 public static IRestChannel Abstract(this RestChannel restChannel)
 => restChannel switch
 {
     null
     => throw new ArgumentNullException(nameof(restChannel)),
コード例 #4
0
ファイル: IRestChannel.cs プロジェクト: Scott-Caldwell/MODiX
 /// <inheritdoc />
 public Task UpdateAsync(RequestOptions options = null)
 => RestChannel.UpdateAsync(options);
コード例 #5
0
ファイル: IRestChannel.cs プロジェクト: Scott-Caldwell/MODiX
 /// <summary>
 /// Constructs a new <see cref="RestChannelAbstraction"/> around an existing <see cref="Rest.RestChannel"/>.
 /// </summary>
 /// <param name="restChannel">The value to use for <see cref="Rest.RestChannel"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restChannel"/>.</exception>
 public RestChannelAbstraction(RestChannel restChannel)
 {
     RestChannel = restChannel ?? throw new ArgumentNullException(nameof(restChannel));
 }
コード例 #6
0
        internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model, bool doApiCall)
        {
            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 = doApiCall ? await guild.GetChannelsAsync().ConfigureAwait(false) : null;

                foreach (var channelModel in resolved.Channels.Value)
                {
                    if (channels != null)
                    {
                        var guildChannel = channels.FirstOrDefault(x => x.Id == channelModel.Value.Id);

                        guildChannel.Update(channelModel.Value);

                        Channels.Add(ulong.Parse(channelModel.Key), guildChannel);
                    }
                    else
                    {
                        var restChannel = RestChannel.Create(discord, channelModel.Value);

                        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
                                                      ?? (doApiCall
                        ? await discord.GetChannelAsync(msg.Value.ChannelId).ConfigureAwait(false)
                        : null));

                    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);
                }
            }
        }