예제 #1
0
 public RedditService(HttpClient httpClient, GuildHandler guildHandler, DiscordSocketClient client, IDocumentStore store, WebhookService webhook)
 {
     Client         = client;
     Store          = store;
     HttpClient     = httpClient;
     GuildHandler   = guildHandler;
     WebhookService = webhook;
 }
예제 #2
0
        public Task Start(ulong GuildId)
        {
            var Server = GuildHandler.GetGuild(GuildId);

            if (!Server.Reddit.Subreddits.Any())
            {
                return(Task.CompletedTask);
            }
            if (ChannelTimers.ContainsKey(Server.Reddit.Webhook.TextChannel))
            {
                return(Task.CompletedTask);
            }
            ChannelTimers.TryAdd(Server.Reddit.Webhook.TextChannel, new Timer(async _ =>
            {
                foreach (var Subbredit in Server.Reddit.Subreddits)
                {
                    var PostIds  = new List <string>();
                    var CheckSub = await SubredditAsync(Subbredit).ConfigureAwait(false);
                    if (CheckSub == null)
                    {
                        return;
                    }
                    var SubData = CheckSub.Data.Children[0].ChildData;
                    if (PostTrack.ContainsKey(Server.Reddit.Webhook.TextChannel))
                    {
                        PostTrack.TryGetValue(Server.Reddit.Webhook.TextChannel, out PostIds);
                    }
                    if (PostIds.Contains(SubData.Id))
                    {
                        return;
                    }
                    string Description = SubData.Selftext.Length > 500 ? $"{SubData.Selftext.Substring(0, 400)} ..." : SubData.Selftext;
                    await WebhookService.SendMessageAsync(new WebhookOptions
                    {
                        Message = $"New Post In **r/{SubData.Subreddit}** By **{SubData.Author}**\n**{SubData.Title}**\n{Description}\nPost Link: {SubData.Url}",
                        Name    = "Reddit Feed",
                        Webhook = Server.Reddit.Webhook
                    });
                    PostIds.Add(SubData.Id);
                    PostTrack.TryRemove(Server.Reddit.Webhook.TextChannel, out List <string> Useless);
                    PostTrack.TryAdd(Server.Reddit.Webhook.TextChannel, PostIds);
                }
            }, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30)));
            return(Task.CompletedTask);
        }