コード例 #1
0
            public RedditStream(string subreddit, ulong streamOwner, int checkInterval, IServiceProvider provider, SocketTextChannel textChannel, RedditType postType = RedditType.Text, int postCount = 20)
            {
                string baseUrl  = "https://www.reddit.com/r/";
                string apiRoute = $"/new/.json?count={PostCount}";

                FullUrl       = $"{baseUrl}{subreddit}{apiRoute}";
                Provider      = provider;
                PostType      = postType;
                Subreddit     = subreddit;
                StreamOwner   = streamOwner;
                PostCount     = postCount;
                TextChannel   = textChannel;
                CheckInterval = checkInterval;
                int minutes = Provider.GetService <MathService>().TimeUnitToMilli(TimeUnit.Minutes, checkInterval);

                CheckTimer = new Timer(async(e) => {
                    try
                    {
                        string content = await Provider.GetService <HttpService>().Get(FullUrl);
                        if (content.Contains("Ow!"))
                        {
                            CheckTimer.Change(minutes, 0);
                            return;
                        }

                        dynamic obj = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(content));
                        ConsoleEx.WriteColoredLine(LogSeverity.Verbose, ConsoleTextFormat.TimeAndText, $"Checking for new posts on $[[DarkMagenta]]$/r/{subreddit}$[[DarkGray]]$ for channel $[[White]]${textChannel.Name.Bold()}");
                        for (int i = 0; i < obj["data"]["children"].Count; i++)
                        {
                            dynamic child = obj["data"]["children"][i];
                            string title  = child["data"]["title"];
                            string desc   = child["data"]["selftext"];

                            Int32.TryParse(child["data"]["created_utc"].ToString(), out int utc);

                            bool nsfw        = child["data"]["over_18"];
                            string author    = child["data"]["author"];
                            string permaLink = $"https://reddit.com{child["data"]["permalink"].ToString()}";

                            if (utc > (UnixTimeStampUTC() - (60 * (checkInterval))))
                            {
                                string imgUrl = "";
                                if (PostType == RedditType.Image)
                                {
                                    imgUrl = child["data"]["url"].ToString().Replace("&amp;", "&");
                                }

                                EmbedBuilder embedBuilder = new EmbedBuilder();
                                Provider.GetService <EmbedService>().BuildFeedbackEmbed(embedBuilder);
                                embedBuilder.Description = "";
                                embedBuilder.WithTitle(title);
                                embedBuilder.WithUrl(permaLink);
                                embedBuilder.Description += $"\nAuthor: {author.ToString().Bold()}\nNSFW: {(nsfw ? "🚫 TRUE".Bold() : "💚 FALSE".Bold())}\n\n{desc}";
                                if (!string.IsNullOrEmpty(imgUrl))
                                {
                                    embedBuilder.WithImageUrl(imgUrl.Replace(@"\", ""));
                                }
                                await TextChannel.SendMessageAsync("", embed: embedBuilder.Build());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"$[[Red]]$Error thrown in reddit service.\n{ex.Message}");
                    }
                    CheckTimer.Change(minutes, 0);
                }, null, minutes, 0);
            }
コード例 #2
0
        public bool AddRedditStream(SocketTextChannel channel, string subreddit, int interval, ulong ownerId, RedditType postType = RedditType.Text)
        {
            var foundStream = RedditStreams.FirstOrDefault(x => x.TextChannel.Id.ToString() == channel.Id.ToString() && x.Subreddit == subreddit);

            if (foundStream != null)
            {
                return(false);
            }
            else
            {
                RedditStreams.Add(new RedditStream(subreddit, ownerId, interval, Provider, channel));
                return(true);
            }
        }