示例#1
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions, InteractionRow[] components)
        {
            UploadWebhookFileParams args = new UploadWebhookFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (components != null)
            {
                args.Components = components.Select(x => x.ToModel()).ToArray();
            }

            API.MessageJson msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
示例#2
0
        public static async Task <ulong> SendMessageAsync(DiscordWebhookClient client,
                                                          string text, bool isTTS, IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options, AllowedMentions allowedMentions, InteractionRow[] components)
        {
            CreateWebhookMessageParams args = new CreateWebhookMessageParams(text)
            {
                IsTTS = isTTS
            };

            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (components != null)
            {
                args.Components = components.Select(x => x.ToModel()).ToArray();
            }

            API.MessageJson model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options : options).ConfigureAwait(false);

            return(model.Id);
        }
示例#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> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                 Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, MessageReferenceParams reference, 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));
                }
            }

            UploadFileParams args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, Embed = embed?.ToModel() ?? Optional <API.EmbedJson> .Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional <API.AllowedMentions> .Unspecified, IsSpoiler = isSpoiler, Components = components?.Select(x => x.ToModel()).ToArray()
            };

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

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
示例#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));
        }
示例#5
0
        //Messages
        public static async Task <RestMessage> GetMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                               ulong id, RequestOptions options)
        {
            ulong? guildId = (channel as IGuildChannel)?.GuildId;
            IGuild guild   = guildId != null?await(client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;

            API.MessageJson model = await client.ApiClient.GetChannelMessageAsync(channel.Id, id, options).ConfigureAwait(false);

            if (model == null)
            {
                return(null);
            }
            IUser author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());

            return(RestMessage.Create(client, channel, author, model));
        }
示例#6
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));
        }