Beispiel #1
0
        public static async Task <RestUserMessage> ModifyMessageAsync(IMessageChannel channel, ulong messageId, Action <MessageProperties> func,
                                                                      BaseDiscordClient client, RequestOptions options)
        {
            var msgModel = await MessageHelper.ModifyAsync(channel.Id, messageId, client, func, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, msgModel.Author.IsSpecified ? RestUser.Create(client, msgModel.Author.Value) : client.CurrentUser, msgModel));
        }
Beispiel #2
0
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
            Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
        {
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            var args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, Embed = embed?.ToModel() ?? Optional<API.Embed>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, IsSpoiler = isSpoiler };
            var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);
            return RestUserMessage.Create(client, channel, client.CurrentUser, model);
        }
Beispiel #3
0
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                    string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, RequestOptions options)
        {
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            var args = new CreateMessageParams(text)
            {
                IsTTS = isTTS, Embed = embed?.ToModel(), AllowedMentions = allowedMentions?.ToModel(), MessageReference = messageReference?.ToModel()
            };
            var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
Beispiel #4
0
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                    string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReferenceParams reference, RequestOptions options, InteractionRow[] components)
        {
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            CreateMessageParams args = new CreateMessageParams(text)
            {
                IsTTS            = isTTS, Embed = embed?.ToModel(), AllowedMentions = allowedMentions?.ToModel(),
                MessageReference = reference?.ToModel(),
                Components       = components?.Select(x => x.ToModel()).ToArray()
            };

            //Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(args, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { ContractResolver = new DiscordContractResolver() }));
            API.MessageJson model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
        internal static async Task <RestUserMessage> SendFollowupAsync(BaseDiscordClient client, API.Rest.CreateWebhookMessageParams args,
                                                                       string token, IMessageChannel channel, RequestOptions options = null)
        {
            var model = await client.ApiClient.CreateInteractionFollowupMessage(args, token, options).ConfigureAwait(false);

            var entity = RestUserMessage.Create(client, channel, client.CurrentUser, model);

            return(entity);
        }
Beispiel #6
0
 internal static RestMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
 {
     if (model.Type == MessageType.Default)
     {
         return(RestUserMessage.Create(discord, channel, author, model));
     }
     else
     {
         return(RestSystemMessage.Create(discord, channel, author, model));
     }
 }
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                 Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
        {
            var args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, Embed = embed != null?embed.ToModel() : Optional <API.Embed> .Unspecified
            };
            var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                    string text, bool isTTS, Embed embed, RequestOptions options)
        {
            var args = new CreateMessageParams(text)
            {
                IsTTS = isTTS, Embed = embed?.ToModel()
            };
            var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
Beispiel #9
0
        public static async Task <RestUserMessage> SendFileAsync(IChannel channel, BaseDiscordClient client,
                                                                 Stream stream, string filename, string text, bool isTTS, IGuild guild, RequestOptions options)
        {
            var args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS
            };
            var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, guild, model));
        }
        internal override async Task UpdateAsync(DiscordRestClient discord, Model model, bool doApiCall)
        {
            await base.UpdateAsync(discord, model, doApiCall).ConfigureAwait(false);

            if (model.Message.IsSpecified && model.ChannelId.IsSpecified)
            {
                if (Message == null)
                {
                    Message = RestUserMessage.Create(Discord, Channel, User, model.Message.Value);
                }
            }
        }
 internal static RestMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
 {
     if (model.Type == MessageType.Default ||
         model.Type == MessageType.Reply ||
         model.Type == MessageType.ApplicationCommand ||
         model.Type == MessageType.ThreadStarterMessage)
     {
         return(RestUserMessage.Create(discord, channel, author, model));
     }
     else
     {
         return(RestSystemMessage.Create(discord, channel, author, model));
     }
 }
Beispiel #12
0
        public static async Task <RestUserMessage> SendInteractionFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                            InteractionData interaction, Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, MessageReferenceParams reference, InteractionMessageType type, bool ghostMessage, InteractionRow[] components)
        {
            if (interaction == null)
            {
                return(await SendFileAsync(channel, client, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler, reference, components).ConfigureAwait(false));
            }
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }
            UploadInteractionFileParams args = new UploadInteractionFileParams
            {
                Type = type,
                Data = new UploadWebhookFileParams(stream)
                {
                    Filename = filename, Content = text, IsTTS = isTTS, AllowedMentions = allowedMentions?.ToModel() ?? Optional <API.AllowedMentions> .Unspecified, IsSpoiler = isSpoiler, Components = components?.Select(x => x.ToModel()).ToArray()
                }
            };

            if (embed != null)
            {
                args.Data.Embeds = new API.EmbedJson[] { embed.ToModel() }
            }
            ;

            if (ghostMessage)
            {
                args.Data.Flags = 64;
            }


            API.MessageJson model = await client.ApiClient.UploadInteractionFileAsync(channel.Id, interaction, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
Beispiel #13
0
        internal override void Update(Model model)
        {
            base.Update(model);

            if (model.IsTextToSpeech.IsSpecified)
            {
                _isTTS = model.IsTextToSpeech.Value;
            }
            if (model.Pinned.IsSpecified)
            {
                _isPinned = model.Pinned.Value;
            }
            if (model.EditedTimestamp.IsSpecified)
            {
                _editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks;
            }
            if (model.MentionEveryone.IsSpecified)
            {
                _isMentioningEveryone = model.MentionEveryone.Value;
            }
            if (model.RoleMentions.IsSpecified)
            {
                _roleMentionIds = model.RoleMentions.Value.ToImmutableArray();
            }

            if (model.Attachments.IsSpecified)
            {
                var value = model.Attachments.Value;
                if (value.Length > 0)
                {
                    var attachments = ImmutableArray.CreateBuilder <Attachment>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        attachments.Add(Attachment.Create(value[i]));
                    }
                    _attachments = attachments.ToImmutable();
                }
                else
                {
                    _attachments = ImmutableArray.Create <Attachment>();
                }
            }

            if (model.Embeds.IsSpecified)
            {
                var value = model.Embeds.Value;
                if (value.Length > 0)
                {
                    var embeds = ImmutableArray.CreateBuilder <Embed>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        embeds.Add(value[i].ToEntity());
                    }
                    _embeds = embeds.ToImmutable();
                }
                else
                {
                    _embeds = ImmutableArray.Create <Embed>();
                }
            }

            if (model.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <RestUser>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val = value[i];
                        if (val.Object != null)
                        {
                            newMentions.Add(RestUser.Create(Discord, val.Object));
                        }
                    }
                    _userMentions = newMentions.ToImmutable();
                }
            }

            var guildId = (Channel as IGuildChannel)?.GuildId;
            var guild   = guildId != null ? (Discord as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;
                _tags         = MessageHelper.ParseTags(text, null, guild, _userMentions);
                model.Content = text;
            }

            if (model.ReferencedMessage.IsSpecified && model.ReferencedMessage.Value != null)
            {
                var   refMsg       = model.ReferencedMessage.Value;
                IUser refMsgAuthor = MessageHelper.GetAuthor(Discord, guild, refMsg.Author.Value, refMsg.WebhookId.ToNullable());
                _referencedMessage = RestUserMessage.Create(Discord, Channel, refMsgAuthor, refMsg);
            }

            if (model.Stickers.IsSpecified)
            {
                var value = model.Stickers.Value;
                if (value.Length > 0)
                {
                    var stickers = ImmutableArray.CreateBuilder <Sticker>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        stickers.Add(Sticker.Create(value[i]));
                    }
                    _stickers = stickers.ToImmutable();
                }
                else
                {
                    _stickers = ImmutableArray.Create <Sticker>();
                }
            }
        }
Beispiel #14
0
        public static async Task <RestUserMessage> SendInteractionMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                               InteractionData interaction, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReferenceParams reference, RequestOptions options, InteractionMessageType type, bool ghostMessage, InteractionRow[] components)
        {
            if (interaction == null)
            {
                return(await SendMessageAsync(channel, client, text, isTTS, embed, allowedMentions, reference, options, components).ConfigureAwait(false));
            }

            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            CreateInteractionMessageParams args = new CreateInteractionMessageParams()
            {
                Type = type
            };

            switch (type)
            {
            case InteractionMessageType.ChannelMessageWithSource:
                args.Data = new CreateWebhookMessageParams(text)
                {
                    IsTTS           = isTTS,
                    AllowedMentions = allowedMentions?.ToModel()
                };
                break;
            }
            if (components != null)
            {
                args.Data.Components = components?.Select(x => x.ToModel()).ToArray();
            }

            if (embed != null)
            {
                args.Data.Embeds = new API.EmbedJson[] { embed.ToModel() }
            }
            ;

            if (ghostMessage)
            {
                args.Data.Flags = 64;
            }
            API.MessageJson model = await client.ApiClient.CreateInteractionMessageAsync(channel.Id, interaction, args, options).ConfigureAwait(false);

            if (model == null)
            {
                return(null);
            }
            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }