Example #1
0
        // Subscribe via Guild Channel / Server Channel
        public async Task <bool> SubscribeNotPrivate(string searchQuery, ulong userId, ulong channelId, ulong guildId)
        {
            if (searchQuery.Count() > _max_search_query_character_size)
            {
                return(false);
            }

            e6Subscription subscription = new e6Subscription();

            subscription.IsNew       = true;
            subscription.IsActive    = true;
            subscription.SearchQuery = searchQuery;
            subscription.IsPrivate   = false;
            subscription.UserId      = userId;
            subscription.ChannelId   = channelId;
            subscription.GuildId     = guildId;

            using (e6PollerBotDbContext dbcontext = new e6PollerBotDbContext())
            {
                await dbcontext.AddAsync <e6Subscription>(subscription);

                await dbcontext.SaveChangesAsync();
            }
            return(true);
        }
Example #2
0
        private async Task SendUpdates(e6Subscription subscription, IEnumerable <int> newIds)
        {
            try
            {
                if (!newIds.Any())
                {
                    return;
                }
                string replyString = $"Hello @here! <@!{subscription.UserId}> has a Subscription Update for [{subscription.SearchQuery}]!";
                int    counter     = 1;
                foreach (int id in newIds)
                {
                    replyString = $"{replyString}\n{_e6_show_post_base_url}/{id}";
                    counter++;
                    if (counter >= _max_reply_list_lines)
                    {
                        if (subscription.IsPrivate)
                        {
                            IDMChannel channel = await _client.GetUser(subscription.UserId).GetOrCreateDMChannelAsync();

                            await channel.SendMessageAsync(replyString);
                        }
                        else
                        {
                            await _client.GetGuild(subscription.GuildId).GetTextChannel(subscription.ChannelId).SendMessageAsync(replyString);
                        }
                        replyString = "";
                        counter     = 0;
                    }
                }

                if (!String.IsNullOrWhiteSpace(replyString))
                {
                    if (subscription.IsPrivate)
                    {
                        IDMChannel channel = await _client.GetUser(subscription.UserId).GetOrCreateDMChannelAsync();

                        await channel.SendMessageAsync(replyString);
                    }
                    else
                    {
                        await _client.GetGuild(subscription.GuildId).GetTextChannel(subscription.ChannelId).SendMessageAsync(replyString);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Example #3
0
        // Poll e6 Periodically.
        private async Task PollerThread()
        {
            while (true)
            {
                try
                {
                    using (e6PollerBotDbContext dbcontext = new e6PollerBotDbContext())
                    {
                        List <e6Subscription> subscriptions = await dbcontext.e6Subscriptions.Where(s => s.IsActive == true).ToListAsync();

                        foreach (e6Subscription subscription in subscriptions)
                        {
                            if (subscription.IsNew)
                            {
                                List <e6Post> e6PostsRaw = await Querye6ByTag(subscription.SearchQuery, _query_limit);

                                HashSet <int> e6PostIds = e6PostsRaw.Select(x => x.id).ToHashSet();

                                using (IDbContextTransaction dbContextTransaction = await dbcontext.Database.BeginTransactionAsync())
                                {
                                    try
                                    {
                                        e6Subscription tempSub = await dbcontext.e6Subscriptions.SingleOrDefaultAsync(s => s.e6SubscriptionId == subscription.e6SubscriptionId);

                                        PropertyValues propertyValues = await dbcontext.Entry(tempSub).GetDatabaseValuesAsync();

                                        tempSub.IsActive   = (bool)propertyValues["IsActive"];
                                        tempSub.e6Posts    = e6PostIds;
                                        subscription.IsNew = false;
                                        dbcontext.e6Subscriptions.Update(tempSub);
                                        await dbcontext.SaveChangesAsync();

                                        dbContextTransaction.Commit();
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                        dbContextTransaction.Rollback();
                                    }
                                }
                            }
                            else
                            {
                                List <e6Post> e6PostsRaw = await Querye6ByTag(subscription.SearchQuery, _query_limit);

                                HashSet <int>     e6PostIds = e6PostsRaw.Select(x => x.id).ToHashSet();
                                IEnumerable <int> newIds    = e6PostIds.Except(subscription.e6Posts);

                                await SendUpdates(subscription : subscription, newIds : newIds);

                                using (IDbContextTransaction dbContextTransaction = await dbcontext.Database.BeginTransactionAsync())
                                {
                                    try
                                    {
                                        e6Subscription tempSub = await dbcontext.e6Subscriptions.SingleOrDefaultAsync(s => s.e6SubscriptionId == subscription.e6SubscriptionId);

                                        PropertyValues propertyValues = await dbcontext.Entry(tempSub).GetDatabaseValuesAsync();

                                        tempSub.IsActive = (bool)propertyValues["IsActive"];
                                        tempSub.e6Posts.UnionWith(newIds);
                                        dbcontext.e6Subscriptions.Update(tempSub);
                                        await dbcontext.SaveChangesAsync();

                                        dbContextTransaction.Commit();
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                        dbContextTransaction.Rollback();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    await Task.Delay(_poller_thread_latency);
                }
            }
        }