Beispiel #1
0
        //Roles
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
        public static async Task <RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
                                                            string name, GuildPermissions?permissions, Color?color, bool isHoisted, bool isMentionable, RequestOptions options)
        {
            if (name == null)
            {
                throw new ArgumentNullException(paramName: nameof(name));
            }

            var createGuildRoleParams = new API.Rest.ModifyGuildRoleParams
            {
                Color       = color?.RawValue ?? Optional.Create <uint>(),
                Hoist       = isHoisted,
                Mentionable = isMentionable,
                Name        = name,
                Permissions = permissions?.RawValue.ToString() ?? Optional.Create <string>()
            };

            var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, createGuildRoleParams, options).ConfigureAwait(false);

            return(RestRole.Create(client, guild, model));
        }
Beispiel #2
0
        //Roles
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
        public static async Task <RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
                                                            string name, GuildPermissions?permissions, Color?color, bool isHoisted, RequestOptions options)
        {
            if (name == null)
            {
                throw new ArgumentNullException(paramName: nameof(name));
            }

            var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false);

            var role = RestRole.Create(client, guild, model);

            await role.ModifyAsync(x =>
            {
                x.Name        = name;
                x.Permissions = (permissions ?? role.Permissions);
                x.Color       = (color ?? Color.Default);
                x.Hoist       = isHoisted;
            }, options).ConfigureAwait(false);

            return(role);
        }
Beispiel #3
0
        internal void Update(Model model)
        {
            AFKChannelId                = model.AFKChannelId;
            EmbedChannelId              = model.EmbedChannelId;
            SystemChannelId             = model.SystemChannelId;
            AFKTimeout                  = model.AFKTimeout;
            IsEmbeddable                = model.EmbedEnabled;
            IconId                      = model.Icon;
            Name                        = model.Name;
            OwnerId                     = model.OwnerId;
            VoiceRegionId               = model.Region;
            SplashId                    = model.Splash;
            VerificationLevel           = model.VerificationLevel;
            MfaLevel                    = model.MfaLevel;
            DefaultMessageNotifications = model.DefaultMessageNotifications;
            ExplicitContentFilter       = model.ExplicitContentFilter;
            ApplicationId               = model.ApplicationId;
            PremiumTier                 = model.PremiumTier;
            VanityURLCode               = model.VanityURLCode;
            BannerId                    = model.Banner;
            SystemChannelFlags          = model.SystemChannelFlags;
            Description                 = model.Description;
            PremiumSubscriptionCount    = model.PremiumSubscriptionCount.GetValueOrDefault();
            PreferredLocale             = model.PreferredLocale;
            PreferredCulture            = new CultureInfo(PreferredLocale);

            if (model.Emojis != null)
            {
                var emotes = ImmutableArray.CreateBuilder <GuildEmote>(model.Emojis.Length);
                for (int i = 0; i < model.Emojis.Length; i++)
                {
                    emotes.Add(model.Emojis[i].ToEntity());
                }
                _emotes = emotes.ToImmutableArray();
            }
            else
            {
                _emotes = ImmutableArray.Create <GuildEmote>();
            }

            if (model.Features != null)
            {
                _features = model.Features.ToImmutableArray();
            }
            else
            {
                _features = ImmutableArray.Create <string>();
            }

            var roles = ImmutableDictionary.CreateBuilder <ulong, RestRole>();

            if (model.Roles != null)
            {
                for (int i = 0; i < model.Roles.Length; i++)
                {
                    roles[model.Roles[i].Id] = RestRole.Create(Discord, this, model.Roles[i]);
                }
            }
            _roles = roles.ToImmutable();

            Available = true;
        }
Beispiel #4
0
 /// <inheritdoc cref="RestRole.ToString" />
 public override string ToString()
 => RestRole.ToString();
        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);
                }
            }
        }
Beispiel #6
0
 /// <inheritdoc />
 public int CompareTo(IRole other)
 => RestRole.CompareTo(other);
Beispiel #7
0
 /// <inheritdoc />
 public Task DeleteAsync(RequestOptions options = null)
 => RestRole.DeleteAsync(options);
Beispiel #8
0
 /// <inheritdoc />
 public Task ModifyAsync(Action <RoleProperties> func, RequestOptions options = null)
 => RestRole.ModifyAsync(func, options);
Beispiel #9
0
 /// <summary>
 /// Constructs a new <see cref="RestRoleAbstraction"/> around an existing <see cref="Rest.RestRole"/>.
 /// </summary>
 /// <param name="restRole">The value to use for <see cref="Rest.RestRole"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restRole"/>.</exception>
 public RestRoleAbstraction(RestRole restRole)
 {
     RestRole = restRole ?? throw new ArgumentNullException(nameof(restRole));
 }
Beispiel #10
0
 /// <summary>
 /// Converts an existing <see cref="RestRole"/> to an abstracted <see cref="IRestRole"/> value.
 /// </summary>
 /// <param name="restRole">The existing <see cref="RestRole"/> to be abstracted.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="restRole"/>.</exception>
 /// <returns>An <see cref="IRestRole"/> that abstracts <paramref name="restRole"/>.</returns>
 public static IRestRole Abstract(this RestRole restRole)
 => new RestRoleAbstraction(restRole);