コード例 #1
0
        /// <summary>
        /// Returns a List of posts.
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        private async Task <List <Post> > PopulateChannelPostsAsync(PostChannel channel)
        {
            switch (channel.Rating)
            {
            case Rating.Q:
                return(await PullPosts(ConstantVars.QUES_SEARCH_QUERY_WAIFU));

            case Rating.E:
                return(await PullPosts(ConstantVars.NSFW_SEARCH_QUERY_WAIFU));

            case Rating.A:
                return(await PullPosts(ConstantVars.ANY_SEARCH_QUERY_WAIFU));

            default:
                return(await PullPosts(ConstantVars.SAFE_SEARCH_QUERY_WAIFU));
            }
        }
コード例 #2
0
        public async Task RegisterChannel(CommandContext ctx, [Description("ID of the channel.")] ulong ChannelID,
                                          [Description("Lets you choose to use the default tag filter list which filters some extreme and distasteful content, can be customized later")] bool UseDefaultTagFilter,
                                          [Description("Toggle posting.")] bool TogglePosting)
        {
            if (!ctx.Guild.Channels.Any(c => c.Id == ChannelID))
            {
                await ctx.Channel.SendMessageAsync("That channel does not belong to this guild.");

                return;
            }

            List <string> tagfilter = new List <string>();

            if (UseDefaultTagFilter)
            {
                tagfilter = ConstantVars.FILTERS.Split(" ").ToList();
            }
            var tmpChannel = new PostChannel(ChannelID, tagfilter, new List <string>(), TogglePosting);

            _imagePoster.Channels.Add(tmpChannel);
            await _imagePoster.SaveChannelData();

            await ctx.Channel.SendMessageAsync("Channel added you can now configure its settings.");
        }
コード例 #3
0
        /// <summary>
        /// Posts a random post to the channel.
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        private async Task PostToChannel(PostChannel channel)
        {
            // This is just so it doesn't keep posting over and over if it takes longer than 5 seconds for..
            // this to choose and send a image, The the time is updated again at the end of this method.
            channel.UpdateNextPostTime();

            if (channel.Posts == null)
            {
                channel.SetPosts(await PopulateChannelPostsAsync(channel));
            }

            if (channel.Posts.Count == 0)
            {
                channel.SetPosts(await PopulateChannelPostsAsync(channel));
            }

            var discordChannel = await _client.GetChannelAsync(channel.ChannelId);

            var i = true;

            while (i)
            {
                // fail safe incase we remove all posts for some reason and end up with non.
                if (channel.Posts.Count == 0)
                {
                    channel.SetPosts(await PopulateChannelPostsAsync(channel));
                }

                var post = channel.Posts[rng.Next(0, channel.Posts.Count - 1)];

                // filter tags and sources
                if (post.Tags.Any(t => channel.FilteredTags.Contains(t)) || channel.FilteredSources.Contains(post.Source))
                {
                    channel.RemovePost(post);
                    continue;
                }

                // filter webm, Filter gif if the channel set AllowGif to false
                if (post.Extension == "webm" || (post.Extension == "gif" && !channel.AllowGif))
                {
                    channel.RemovePost(post);
                    continue;
                }

                var contentWarning = "(Warning website may NSFW content)";
                // build the embed
                var eb = new DiscordEmbedBuilder
                {
                    // if the post is meant to be safe show a warning in the title.
                    Title    = $"{post.Source}" + (post.Rating == Rating.S ? contentWarning : string.Empty),
                    Url      = post.Page,
                    ImageUrl = post.Url,
                    Footer   = new EmbedFooter
                    {
                        Text    = (post.SourceUrl.Count() > 0) ? $"Source: {post.SourceUrl}" : $"{DateTime.Now.ToString("F")}",
                        IconUrl = "https://i.imgur.com/mrnHP3J.png"
                    },
                    Color = new DiscordColor(channel.EmbedColorHex)
                };

                // send the embed to the channel then remove the post from list, Update the next send time and exit the loop
                await discordChannel.SendMessageAsync("", false, eb);

                channel.RemovePost(post);
                channel.UpdateNextPostTime();
                i = false;
            }
        }