Esempio n. 1
0
File: Hook.cs Progetto: ufgf/Anarchy
        public void SendMessage(string content)
        {
            WebhookMessageProperties message = new WebhookMessageProperties
            {
                Username  = Name,
                Content   = content,
                AvatarUrl = $"https://cdn.discordapp.com/avatars/{Id}/{AvatarId}.png"
            };

            SendMessage(message);
        }
Esempio n. 2
0
        public static async Task ModifyMessageAsync(DiscordWebhookClient client, ulong messageId,
                                                    Action <WebhookMessageProperties> func, RequestOptions options)
        {
            var args = new WebhookMessageProperties();

            func(args);

            if (args.AllowedMentions.IsSpecified)
            {
                var allowedMentions = args.AllowedMentions.Value;
                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?.AllowedTypes != null)
                {
                    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 apiArgs = new ModifyWebhookMessageParams
            {
                Content = args.Content.IsSpecified ? args.Content.Value : Optional.Create <string>(),
                Embeds  =
                    args.Embeds.IsSpecified
                        ? args.Embeds.Value.Select(embed => embed.ToModel()).ToArray()
                        : Optional.Create <API.Embed[]>(),
                AllowedMentions = args.AllowedMentions.IsSpecified
                    ? args.AllowedMentions.Value.ToModel()
                    : Optional.Create <API.AllowedMentions>(),
                Components = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() : Optional <API.ActionRowComponent[]> .Unspecified,
            };

            await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, apiArgs, options)
            .ConfigureAwait(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Sends a message through the webhook
        /// </summary>
        /// <param name="webhookId">ID of the webhook</param>
        /// <param name="webhookToken">The webhook's token</param>
        /// <param name="content">The message to send</param>
        /// <param name="embed">Embed to include in the message</param>
        /// <param name="profile">Custom Username and Avatar url (both are optional)</param>
        public static void SendWebhookMessage(this DiscordClient client, ulong webhookId, string webhookToken, string content, Embed embed = null, DiscordWebhookProfile profile = null)
        {
            var properties = new WebhookMessageProperties()
            {
                Content = content, Embed = embed
            };

            if (profile != null)
            {
                if (profile.NameProperty.Set)
                {
                    properties.Username = profile.Username;
                }
                if (profile.AvatarProperty.Set)
                {
                    properties.AvatarUrl = profile.AvatarUrl;
                }
            }

            client.HttpClient.Post($"/webhooks/{webhookId}/{webhookToken}", properties);
        }
Esempio n. 4
0
        /// <summary>
        /// Sends a message through the webhook
        /// </summary>
        /// <param name="message">The message to send</param>
        /// <param name="embed">Embed to include in the message</param>
        /// <param name="profile">Custom Username and Avatar url (both are optional)</param>
        public void SendMessage(string content, Embed embed = null, WebhookProfile profile = null)
        {
            var properties = new WebhookMessageProperties()
            {
                Content = content, Embed = embed
            };

            if (profile != null)
            {
                if (profile.NameProperty.Set)
                {
                    properties.Username = profile.Username;
                }
                if (profile.AvatarProperty.Set)
                {
                    properties.AvatarUrl = profile.AvatarUrl;
                }
            }

            Client.HttpClient.Post($"/webhooks/{Id}/{Token}",
                                   JsonConvert.SerializeObject(properties));
        }
Esempio n. 5
0
File: Hook.cs Progetto: ufgf/Anarchy
 public void SendMessage(WebhookMessageProperties message)
 {
     Client.HttpClient.Post($"/webhooks/{Id}/{Token}", JsonConvert.SerializeObject(message));
 }