Exemple #1
0
        public AddResult Add(ulong ChannelId, List <string> Tags)
        {
            SubTag n = new SubTag()
            {
                Tags = Tags
            };

            if (this.Tags.ContainsKey(ChannelId))
            {
                for (int i = 0; i < this.Tags[ChannelId].Count; i++)
                {
                    SubTag current = this.Tags[ChannelId][i];

                    if (current.Identical(n))
                    {
                        return(AddResult.AlreadyContains);
                    }
                }

                List <Post> posts = Gelbooru.FetchImages(Tags);
                if (posts.Count == 0)
                {
                    return(AddResult.NoImages);
                }

                n.PostIds = posts.Select(t => t.PostId).ToList();
                this.Tags[ChannelId].Add(n);
            }
            else
            {
                List <Post> posts = Gelbooru.FetchImages(Tags);
                if (posts.Count == 0)
                {
                    return(AddResult.NoImages);
                }

                n.PostIds = posts.Select(t => t.PostId).ToList();
                this.Tags.Add(ChannelId, new List <SubTag>()
                {
                    n
                });
            }
            Save();

            return(AddResult.Success);
        }
Exemple #2
0
        private void ProcessImages(object sender, EventArgs e)
        {
            bool SaveTags = false;

            foreach (ulong key in Tags.Keys)
            {
                if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested)
                {
                    break;
                }

                if (Global.Client.GetChannel(key) is ITextChannel channel)
                {
                    LanguageEntry language = channel.GetSettings().GetLanguage();
                    List <SubTag> SubTags  = Tags[key];

                    for (int i = 0; i < SubTags.Count; i++)
                    {
                        if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested)
                        {
                            break;
                        }

                        List <Post> CurrentImages = new List <Post>();
                        try
                        {
                            CurrentImages.AddRange(Gelbooru.FetchImages(SubTags[i].Tags));
                        }
                        catch (Exception ex)
                        {
                            SendToErrorChannel(ex, $"Couldn't fetch images with tags: `{ string.Join(", ", SubTags[i].Tags) }`!\nWaiting 30 seconds...");
                            if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested)
                            {
                                break;
                            }
                            Thread.Sleep(30 * 1000);
                        }
                        if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested)
                        {
                            break;
                        }
                        Thread.Sleep(2000);
                        if (CurrentImages.Count == 0)
                        {
                            continue;
                        }

                        bool PerformSave = false;
                        for (int j = 0; j < CurrentImages.Count; j++)
                        {
                            Post post = CurrentImages[j];

                            if (!SubTags[i].PostIds.Contains(post.PostId))
                            {
                                PerformSave = true;
                                string joined_tags = SubTags[i].Tags.Count == 0 ? language.GetEntry("SubTagHandler:NoTag") : string.Join(" ", SubTags[i].Tags);
                                string mentions    = "";
                                if (SubTags[i].UserMentions.Count != 0)
                                {
                                    mentions = string.Join(", ", SubTags[i].UserMentions.Select(t => $"<@{ t }>"));
                                }
                                channel.SendMessageAsync(mentions, embed: new EmbedBuilder()
                                {
                                    Color    = Color.Magenta,
                                    Title    = language.GetEntry("SubTagHandler:NewImage", "TAGS", joined_tags) + (post.IsAnimated ? "[ANIMATED]" : "") + " - " + post.Filename,
                                    ImageUrl = (!post.IsAnimated || post.Filename.EndsWith(".gif") ? post.Link : post.ThumbnailUrl),
                                    Url      = post.PostLink
                                }.Build());
                                if (!Checker.Enabled || Entrance.CancellationToken.IsCancellationRequested)
                                {
                                    break;
                                }
                                Thread.Sleep(2000);
                            }
                        }

                        if (PerformSave)
                        {
                            Tags[key][i].PostIds = CurrentImages.Select(t => t.PostId).ToList();
                            Save();
                        }
                    }
                }
                else
                {
                    Tags.Remove(key);
                    SaveTags = true;
                }
            }
            if (SaveTags)
            {
                Save();
            }
        }