Exemplo n.º 1
0
        /// <summary>
        /// Clones this channel. This operation will create a channel with identical settings to this one. Note that this will not copy messages.
        /// </summary>
        /// <param name="reason">Reason for audit logs.</param>
        /// <returns>Newly-created channel.</returns>
        public async Task <DiscordChannel> CloneAsync(string reason = null)
        {
            if (this.Guild == null)
            {
                throw new InvalidOperationException("Non-guild channels cannot be cloned.");
            }

            var ovrs = new List <DiscordOverwriteBuilder>();

            foreach (var ovr in this._permissionOverwrites)
            {
                ovrs.Add(await new DiscordOverwriteBuilder().FromAsync(ovr).ConfigureAwait(false));
            }

            int?            bitrate          = this.Bitrate;
            int?            userLimit        = this.UserLimit;
            Optional <int?> perUserRateLimit = this.PerUserRateLimit;

            if (this.Type != ChannelType.Voice)
            {
                bitrate   = null;
                userLimit = null;
            }
            if (this.Type != ChannelType.Text)
            {
                perUserRateLimit = Optional <int?> .FromNoValue();
            }

            return(await this.Guild.CreateChannelAsync(this.Name, this.Type, this.Parent, bitrate, userLimit, ovrs, this.IsNSFW, perUserRateLimit, reason).ConfigureAwait(false));
        }
Exemplo n.º 2
0
 internal DiscordEmbed()
 {
     _colorLazy = new Lazy <Optional <DiscordColor> >(() =>
                                                      _color.HasValue
             ? Optional.FromValue <DiscordColor>(_color.Value)
             : Optional.FromNoValue <DiscordColor>());
 }
Exemplo n.º 3
0
        /// <summary>
        /// Create a new webhook
        /// </summary>
        /// <param name="name"></param>
        /// <param name="avatar"></param>
        /// <param name="reason">Reason for audit logs.</param>
        /// <returns></returns>
        public async Task<DiscordWebhook> CreateWebhookAsync(string name, Optional<Stream> avatar = default, string reason = null)
        {
            var av64 = Optional.FromNoValue<string>();
            if (avatar.HasValue && avatar.Value != null)
                using (var imgtool = new ImageTool(avatar.Value))
                    av64 = imgtool.GetBase64();
            else if (avatar.HasValue)
                av64 = null;

            return await this.Discord.ApiClient.CreateWebhookAsync(this.Id, name, av64, reason).ConfigureAwait(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructs a new embed from data supplied to this builder.
        /// </summary>
        /// <returns>New discord embed.</returns>
        public DiscordEmbed Build()
        {
            var embed = new DiscordEmbed
            {
                Title       = this._title,
                Description = this._description,
                Url         = this._url,
                _color      = this._color.HasValue ? this._color.Value.Value : Optional <int> .FromNoValue(),
                Timestamp   = this._timestamp
            };

            if (this.Footer != null)
            {
                embed.Footer = new DiscordEmbedFooter
                {
                    Text    = this.Footer.Text,
                    IconUrl = this.Footer.IconUrl != null ? new Uri(this.Footer.IconUrl) : null
                }
            }
            ;

            if (this.Author != null)
            {
                embed.Author = new DiscordEmbedAuthor
                {
                    Name    = this.Author.Name,
                    Url     = this.Author.Url != null ? new Uri(this.Author.Url) : null,
                    IconUrl = this.Author.IconUrl != null ? new Uri(this.Author.IconUrl) : null
                }
            }
            ;

            if (this._image_uri != null)
            {
                embed.Image = new DiscordEmbedImage {
                    Url = this._image_uri
                }
            }
            ;
            if (this._thumbnail_uri != null)
            {
                embed.Thumbnail = new DiscordEmbedThumbnail {
                    Url = this._thumbnail_uri
                }
            }
            ;

            if (this._fields.Any())
            {
                embed.Fields = new ReadOnlyCollection <DiscordEmbedField>(new List <DiscordEmbedField>(this._fields)); // copy the list, don't wrap it, prevents mutation
            }
            return(embed);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Modifies this webhook.
        /// </summary>
        /// <param name="name">New default name for this webhook.</param>
        /// <param name="avatar">New avatar for this webhook.</param>
        /// <returns>The modified webhook.</returns>
        public Task <DiscordWebhook> ModifyAsync(string name = null, Optional <Stream> avatar = default)
        {
            var avatarb64 = Optional.FromNoValue <string>();

            if (avatar.HasValue && avatar.Value != null)
            {
                using (var imgtool = new ImageTool(avatar.Value))
                    avatarb64 = imgtool.GetBase64();
            }
            else if (avatar.HasValue)
            {
                avatarb64 = null;
            }

            return(this.Discord.ApiClient.ModifyWebhookAsync(this.Id, name, avatarb64, Token));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Modifies this webhook.
        /// </summary>
        /// <param name="name">New default name for this webhook.</param>
        /// <param name="avatar">New avatar for this webhook.</param>
        /// <param name="channelId">The new channel id to move the webhook to.</param>
        /// <param name="reason">Reason for audit logs.</param>
        /// <returns>The modified webhook.</returns>
        /// <exception cref="Exceptions.UnauthorizedException">Thrown when the client does not have the <see cref="Permissions.ManageWebhooks"/> permission.</exception>
        /// <exception cref="Exceptions.NotFoundException">Thrown when the webhook does not exist.</exception>
        /// <exception cref="Exceptions.BadRequestException">Thrown when an invalid parameter was provided.</exception>
        /// <exception cref="Exceptions.ServerErrorException">Thrown when Discord is unable to process the request.</exception>
        public Task <DiscordWebhook> ModifyAsync(string name = null, Optional <Stream> avatar = default, ulong?channelId = null, string reason = null)
        {
            var avatarb64 = Optional.FromNoValue <string>();

            if (avatar.HasValue && avatar.Value != null)
            {
                using (var imgtool = new ImageTool(avatar.Value))
                    avatarb64 = imgtool.GetBase64();
            }
            else if (avatar.HasValue)
            {
                avatarb64 = null;
            }

            var newChannelId = channelId.HasValue ? channelId.Value : this.ChannelId;

            return(this.Discord.ApiClient.ModifyWebhookAsync(this.Id, newChannelId, name, avatarb64, reason));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Modifies this member.
        /// </summary>
        /// <param name="action">Action to perform on this member.</param>
        /// <returns></returns>
        public async Task ModifyAsync(Action <MemberEditModel> action)
        {
            var mdl = new MemberEditModel();

            action(mdl);

            if (mdl.VoiceChannel.HasValue && mdl.VoiceChannel.Value.Type != ChannelType.Voice)
            {
                throw new ArgumentException("Given channel is not a voice channel.", nameof(mdl.VoiceChannel));
            }

            if (mdl.Nickname.HasValue && this.Discord.CurrentUser.Id == this.Id)
            {
                await this.Discord.ApiClient.ModifyCurrentMemberNicknameAsync(this.Guild.Id, mdl.Nickname.Value,
                                                                              mdl.AuditLogReason).ConfigureAwait(false);

                await this.Discord.ApiClient.ModifyGuildMemberAsync(this.Guild.Id, this.Id, Optional <string> .FromNoValue(),
                                                                    mdl.Roles.IfPresent(e => e.Select(xr => xr.Id)), mdl.Muted, mdl.Deafened,
                                                                    mdl.VoiceChannel.IfPresent(e => e.Id), mdl.AuditLogReason).ConfigureAwait(false);
            }
            else
            {
                await this.Discord.ApiClient.ModifyGuildMemberAsync(this.Guild.Id, this.Id, mdl.Nickname,
                                                                    mdl.Roles.IfPresent(e => e.Select(xr => xr.Id)), mdl.Muted, mdl.Deafened,
                                                                    mdl.VoiceChannel.IfPresent(e => e.Id), mdl.AuditLogReason).ConfigureAwait(false);
            }
        }
Exemplo n.º 8
0
 internal DiscordEmbed()
 {
     this._colorLazy = new Lazy <Optional <DiscordColor> >(() => this._color.HasValue ? Optional <DiscordColor> .FromValue(this._color.Value) : Optional <DiscordColor> .FromNoValue());
 }