Beispiel #1
0
        /* operation functions */
        public async Task Operate(ICommandContext context, string subreddit, string query, int sorting, bool hide)
        {
            _previousSub     = subreddit;
            _previousQuery   = query;
            _previousSorting = sorting;
            _previousHide    = hide;

            // is the reddit f**k off?
            if (!Program.redditEnabled)
            {
                context.Channel.SendMessageAsync("Reddit is disabled. Try again when it's back on.");
                return;
            }

            using WorkingBlock wb = new WorkingBlock(context);
            try
            {
                Program.Subreddit = await Program.Reddit.GetSubredditAsync(subreddit);

                Random rnd = Globals.Rng;

                Listing <Post> contents      = Program.Subreddit.GetPosts(RedditTools.ParseSorting.FromIntSort(sorting));
                int            contentsCount = await contents.CountAsync();

                if (contentsCount < 10)
                {
                    contents = Program.Subreddit.GetPosts();
                }
                //Console.WriteLine(contents.Count);
                var post = await contents.ElementAtAsync(rnd.Next(contentsCount));

                //EmbedBuilder embedd;

                bool isNsfw = await IsChannelNsfw(context);

                string postMessage = $"**{post.Title}**\nPosted on *{post.CreatedUTC.ToShortDateString()}* by **{post.AuthorName}**\n\n{ReturnCharOnTrue(hide, "<")}{post.Url.AbsoluteUri}{ReturnCharOnTrue(hide, ">")}\n<https://www.reddit.com{post.Permalink}>";

                if ((post.NSFW || post.Title.Contains("NSFW") || post.Title.Contains("NSFL")) && !isNsfw)
                {
                    postMessage = "The random post that was selected is NSFW or the subreddit is NSFW.Try again for another random post, with another subreddit, or move to a NSFW channel(needs nsfw in the name).";
                }

                await ReplyAsync(postMessage);
            }
            catch (Exception e)
            {
                await Error.BuildError(e, context);
            }
        }