public static void Run(
            [QueueTrigger("bundlequeue")] BundleQueue queuedBundle,
            [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue,
            [Table("webhookRegistration")] CloudTable existingWebhooks,
            TraceWriter log)
        {
            log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}");

            var bundle   = queuedBundle.Bundle;
            var webhooks = existingWebhooks.GetAllWebhooksForBundleType(queuedBundle.Bundle.Type, queuedBundle.IsUpdate);
            int queued   = 0;

            foreach (var webhook in webhooks)
            {
                var message = new DiscordWebhookPayload(queuedBundle, bundle);

                if (!string.IsNullOrEmpty(webhook.Partner))
                {
                    message.AddPartnerLink(webhook.Partner);
                }

                messageQueue.Add(new DiscordMessage
                {
                    WebhookUrl = webhook.GetDecryptedWebhook(),
                    Payload    = message
                });
                queued++;
            }

            log.Info($"Queued {queued} payloads for type {queuedBundle.Bundle.Type}");
        }
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestMessage req,
            [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue,
            [Table("webhookRegistration")] IQueryable <WebhookRegistrationEntity> existingWebhooks,
            TraceWriter log)
        {
            var message = new DiscordWebhookPayload
            {
                embeds = new List <DiscordEmbed>()
            };

            message.embeds.Add(new DiscordEmbed()
            {
                title       = "Developer Message",
                description = await req.Content.ReadAsStringAsync(),
                author      = new AuthorField()
                {
                    name = "HumbleBundleBot Developer",
                    url  = "https://github.com/cswendrowski/HumbleBundleBot"
                }
            });

            foreach (var webhook in GetAllWebhooksForDeveloperMessage(existingWebhooks))
            {
                messageQueue.Add(new DiscordMessage
                {
                    WebhookUrl = webhook,
                    Payload    = message
                });
            }

            return(req.CreateResponse(HttpStatusCode.OK));
        }
Exemple #3
0
        public static void Run(
            [QueueTrigger("bundlequeue")] BundleQueue queuedBundle,
            [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue,
            [Table("webhookRegistration")] CloudTable existingWebhooks,
            TraceWriter log)
        {
            log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}");

            var bundle   = queuedBundle.Bundle;
            var webhooks = existingWebhooks.GetAllWebhooksForBundleType(queuedBundle.Bundle.Type, queuedBundle.IsUpdate).ToList();

            if (queuedBundle.Bundle.Type == BundleTypes.BOOKS)
            {
                var isComicOrRpg = false;

                // If the same webhook is registered as RPG / COMIC, remove it unless this is the right type
                if (bundle.Name.ToLower().Contains("rpg"))
                {
                    var rpgWebhooks = existingWebhooks.GetAllWebhooksForBundleType(BundleTypes.RPG, queuedBundle.IsUpdate);
                    webhooks.AddRange(rpgWebhooks);
                    isComicOrRpg = true;
                }

                if (!bundle.Name.ToLower().Contains("comic"))
                {
                    var comicWebhooks = existingWebhooks.GetAllWebhooksForBundleType(BundleTypes.COMIC, queuedBundle.IsUpdate);
                    webhooks.AddRange(comicWebhooks);
                    isComicOrRpg = true;
                }

                if (!isComicOrRpg)
                {
                    var bookOtherWebhooks = existingWebhooks.GetAllWebhooksForBundleType(BundleTypes.BOOK_OTHER, queuedBundle.IsUpdate);
                    webhooks.AddRange(bookOtherWebhooks);
                }
            }
            int queued = 0;

            foreach (var webhook in webhooks)
            {
                var message = new DiscordWebhookPayload(queuedBundle, bundle);

                if (!string.IsNullOrEmpty(webhook.Partner))
                {
                    message.AddPartnerLink(webhook.Partner);
                }

                messageQueue.Add(new DiscordMessage
                {
                    WebhookUrl = webhook.GetDecryptedWebhook(),
                    Payload    = message
                });
                queued++;
            }

            log.Info($"Queued {queued} payloads for type {queuedBundle.Bundle.Type}");
        }
        private static void AddPartnerLink(DiscordWebhookPayload message, string partner)
        {
            foreach (var embed in message.embeds)
            {
                if (!string.IsNullOrEmpty(embed.url))
                {
                    embed.url += "?partner=" + partner;
                }

                if (embed.author != null && !string.IsNullOrEmpty(embed.author.url))
                {
                    embed.author.url += "?partner=" + partner;
                }
            }
        }
        public static void Run(
            [QueueTrigger("bundlequeue")] BundleQueue queuedBundle,
            [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue,
            [Table("webhookRegistration")] IQueryable <WebhookRegistrationEntity> existingWebhooks,
            TraceWriter log)
        {
            log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}");

            var bundle = queuedBundle.Bundle;

            var webhooks = GetAllWebhooksForBundleType(existingWebhooks, queuedBundle.Bundle.Type, queuedBundle.IsUpdate);

            log.Info($"Found {webhooks.Count} webhooks for type {queuedBundle.Bundle.Type}");

            var content = "New Bundle: " + bundle.Name;

            if (queuedBundle.IsUpdate)
            {
                content = "Bundle Updated: " + bundle.Name;
            }

            var message = new DiscordWebhookPayload
            {
                content = content,
                embeds  = new List <DiscordEmbed>()
            };

            message.embeds.Add(new DiscordEmbed()
            {
                url   = bundle.URL + "?partner=havenbreaker&charity=151160",
                title = bundle.Description,
                image = new ImageField()
                {
                    url = bundle.ImageUrl
                },
                author = new AuthorField()
                {
                    name = "Humble Bundle",
                    url  = bundle.URL + "?partner=havenbreaker&charity=151160"
                }
            });

            foreach (var section in bundle.Sections)
            {
                var embed = new DiscordEmbed
                {
                    title       = section.Title,
                    url         = bundle.URL + "?partner=havenbreaker&charity=151160",
                    description = ""
                };

                foreach (var item in section.Items.Take(25))
                {
                    embed.description += item.Name + "\n";
                }

                message.embeds.Add(embed);

                if (section.Items.Count > 25)
                {
                    var embedContinued = new DiscordEmbed
                    {
                        title       = section.Title,
                        url         = bundle.URL + "?partner=havenbreaker&charity=151160",
                        description = ""
                    };

                    foreach (var item in section.Items.Skip(25).Take(25))
                    {
                        embedContinued.description += item.Name + "\n";
                    }

                    message.embeds.Add(embedContinued);
                }
            }

            log.Info("Created message " + JsonConvert.SerializeObject(message));

            foreach (var webhook in webhooks)
            {
                messageQueue.Add(new DiscordMessage
                {
                    WebhookUrl = webhook,
                    Payload    = message
                });
            }
        }
        public static void Run(
            [QueueTrigger("bundlequeue")] BundleQueue queuedBundle,
            [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue,
            [Table("webhookRegistration")] CloudTable existingWebhooks,
            TraceWriter log)
        {
            log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}");

            var bundle   = queuedBundle.Bundle;
            var webhooks = existingWebhooks.GetAllWebhooksForBundleType(queuedBundle.Bundle.Type, queuedBundle.IsUpdate);
            int queued   = 0;

            foreach (var webhook in webhooks)
            {
                var content = "New Bundle: " + bundle.Name;

                if (queuedBundle.IsUpdate)
                {
                    content = "Bundle Updated: " + bundle.Name;
                }

                var message = new DiscordWebhookPayload
                {
                    content = content,
                    embeds  = new List <DiscordEmbed>()
                };

                message.embeds.Add(new DiscordEmbed()
                {
                    url   = bundle.URL,
                    title = bundle.Description,
                    image = new ImageField()
                    {
                        url = bundle.ImageUrl
                    },
                    author = new AuthorField()
                    {
                        name = "Humble Bundle",
                        url  = bundle.URL
                    }
                });

                foreach (var section in bundle.Sections)
                {
                    var embed = new DiscordEmbed
                    {
                        title       = section.Title,
                        url         = bundle.URL,
                        description = ""
                    };

                    var itemsAdded = 0;

                    foreach (var item in section.Items)
                    {
                        embed.description += GetItemName(item, queuedBundle.UpdatedItems);
                        itemsAdded++;

                        // Create a new embed every 25 items
                        if (itemsAdded % 25 == 0)
                        {
                            message.embeds.Add(embed);
                            embed = new DiscordEmbed
                            {
                                title       = section.Title + " (Continued)",
                                url         = bundle.URL,
                                description = ""
                            };
                        }
                    }

                    // Add last embed
                    message.embeds.Add(embed);
                }

                if (!string.IsNullOrEmpty(webhook.Partner))
                {
                    AddPartnerLink(message, webhook.Partner);
                }

                messageQueue.Add(new DiscordMessage
                {
                    WebhookUrl = webhook.GetDecryptedWebhook(),
                    Payload    = message
                });
                queued++;
            }

            log.Info($"Queued {queued} payloads for type {queuedBundle.Bundle.Type}");
        }
Exemple #7
0
        public static void Run(
            [QueueTrigger("bundlequeue")] BundleQueue queuedBundle,
            [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue,
            [Table("webhookRegistration")] IQueryable <WebhookRegistrationEntity> existingWebhooks,
            TraceWriter log)
        {
            log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}");

            var bundle = queuedBundle.Bundle;

            var webhooks = GetAllWebhooksForBundleType(existingWebhooks, queuedBundle.Bundle.Type, queuedBundle.IsUpdate);

            log.Info($"Found {webhooks.Count} webhooks for type {queuedBundle.Bundle.Type}");

            var content = "New Bundle: " + bundle.Name;

            if (queuedBundle.IsUpdate)
            {
                content = "Bundle Updated: " + bundle.Name;
            }

            var message = new DiscordWebhookPayload
            {
                content = content,
                embeds  = new List <DiscordEmbed>()
            };

            message.embeds.Add(new DiscordEmbed()
            {
                url   = bundle.URL,
                title = bundle.Description,
                image = new ImageField()
                {
                    url = bundle.ImageUrl
                },
                author = new AuthorField()
                {
                    name = "Humble Bundle",
                    url  = bundle.URL
                }
            });

            foreach (var section in bundle.Sections)
            {
                var embed = new DiscordEmbed
                {
                    title       = section.Title,
                    url         = bundle.URL,
                    description = ""
                };

                var itemsAdded = 0;

                foreach (var item in section.Items)
                {
                    embed.description += GetItemName(item, queuedBundle.UpdatedItems);
                    itemsAdded++;

                    // Create a new embed every 25 items
                    if (itemsAdded % 25 == 0)
                    {
                        message.embeds.Add(embed);
                        embed = new DiscordEmbed
                        {
                            title       = section.Title + " (Continued)",
                            url         = bundle.URL,
                            description = ""
                        };
                    }
                }

                // Add last embed
                message.embeds.Add(embed);
            }

            log.Info("Created message " + JsonConvert.SerializeObject(message));

            foreach (var webhook in webhooks)
            {
                messageQueue.Add(new DiscordMessage
                {
                    WebhookUrl = webhook,
                    Payload    = message
                });
            }
        }