Beispiel #1
0
        private async Task Client_MessageReactionAdded(DSharpPlus.EventArgs.MessageReactionAddEventArgs e)
        {
            using var context        = new DiscordContext();
            using var sankakuContext = new SankakuModel.SankakuContext();

            var message = await context.SankakuPost.FirstOrDefaultAsync(post => post.MessageId == Convert.ToInt64(e.Message.Id));

            if (message != null)
            {
                SankakuModel.ImageVote vote = null;

                vote = new SankakuModel.ImageVote()
                {
                    ImageId   = message.ImageId,
                    UserId    = Convert.ToInt64(e.User.Id),
                    VoteValue = 0
                };

                string discordName = e.Emoji.GetDiscordName();

                if (Config.Emojis.Confirms.Contains(discordName))
                {
                    vote.VoteValue = 1;
                }
                else if (Config.Emojis.Love.Contains(discordName))
                {
                    vote.VoteValue = 3;
                }
                else if (Config.Emojis.Declines.Contains(discordName))
                {
                    vote.VoteValue = -1;
                }
                else if (Config.Emojis.Hate.Contains(discordName))
                {
                    vote.VoteValue = -3;
                }
                else if (discordName == Config.Emojis.Underage)
                {
                    await e.Message.DeleteAsync("Underage");

                    await e.Message.RespondAsync("Oopsie");
                }

                if (vote.VoteValue != 0)
                {
                    await sankakuContext.ImageVote.AddAsync(vote);
                }

                await sankakuContext.SaveChangesAsync();
            }
        }
Beispiel #2
0
        private async Task Client_MessageReactionRemoved(DSharpPlus.EventArgs.MessageReactionRemoveEventArgs e)
        {
            using var sankakuContext = new SankakuModel.SankakuContext();
            using var discordContext = new DiscordContext();

            var message = await discordContext.SankakuPost.FirstOrDefaultAsync(post => post.MessageId == Convert.ToInt64(e.Message.Id));

            if (message != null)
            {
                int VoteValue = 0;

                string discordName = e.Emoji.GetDiscordName();

                if (Config.Emojis.Confirms.Contains(discordName))
                {
                    VoteValue = 1;
                }
                else if (Config.Emojis.Love.Contains(discordName))
                {
                    VoteValue = 3;
                }
                else if (Config.Emojis.Declines.Contains(discordName))
                {
                    VoteValue = -1;
                }
                else if (Config.Emojis.Hate.Contains(discordName))
                {
                    VoteValue = -3;
                }

                var vote = await sankakuContext.ImageVote.FirstOrDefaultAsync(sankakuVote => sankakuVote.ImageId == message.ImageId &&
                                                                              sankakuVote.UserId == Convert.ToInt64(e.User.Id) &&
                                                                              sankakuVote.VoteValue == VoteValue);

                if (vote != null)
                {
                    sankakuContext.ImageVote.Remove(vote);
                }

                await sankakuContext.SaveChangesAsync();
            }
        }
Beispiel #3
0
        private async Task <bool> Scrape(Order order = Order.Random, int limit = 100)
        {
            Console.WriteLine($"SankakuBot: scraping for {order.ToFormattedText()}");
            using var sankakuContext = new SankakuModel.SankakuContext(120, 200);

            string orderString = "random";

            switch (order)
            {
            case Order.Random:
                orderString = "random";
                break;

            case Order.Newest:
                orderString = "newest";
                break;
            }

            var response = await _httpClient.GetAsync(_baseUrl + _indexUrl + $"?limit={limit}&tags=order:{orderString}").ConfigureAwait(false);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(false);
            }

            var json = await response.Content.ReadAsStringAsync();

            var images = Entities.Sankaku.Image.FromJson(json);

            var allTags = sankakuContext.Tag.Select(t => t.Id).ToList();

            foreach (Entities.Sankaku.Image image in images)
            {
                Image dbImage = await sankakuContext.Image.FindAsync(image.Id);

                if (dbImage == null)
                {
                    dbImage = new Image()
                    {
                        Id          = image.Id,
                        Rating      = (int)image.Rating,
                        RatingCount = Convert.ToInt32(image.VoteCount),
                        Score       = Convert.ToInt32(image.TotalScore)
                    };

                    sankakuContext.Image.Add(dbImage);
                }
                else
                {
                    dbImage.Rating      = (int)image.Rating;
                    dbImage.RatingCount = Convert.ToInt32(image.VoteCount);
                    dbImage.Score       = Convert.ToInt32(image.TotalScore);
                }

                foreach (var tag in image.Tags)
                {
                    if (string.IsNullOrEmpty(tag.Name))
                    {
                        tag.Name = string.Empty;
                    }

                    if (allTags.All(savedTag => savedTag != Convert.ToInt32(tag.Id)))
                    {
                        var dbTag = new Tag()
                        {
                            Name = tag.Name,
                            Id   = Convert.ToInt32(tag.Id)
                        };

                        sankakuContext.Tag.Add(dbTag);
                        allTags.Add(dbTag.Id);
                    }

                    if (dbImage.ImageTag.All(t => t.TagId != tag.Id))
                    {
                        ImageTag imageTag = new ImageTag()
                        {
                            TagId = Convert.ToInt32(tag.Id)
                        };

                        dbImage.ImageTag.Add(imageTag);
                    }
                }

                dbImage = null;
            }

            allTags = null;

            await sankakuContext.SaveChangesAsync();

            sankakuContext.Dispose();

            GC.Collect(2);

            return(true);
        }