private static async void Timer_RedditPost(object sender, ElapsedEventArgs e) { if (RedditLock) { return; } try { RedditLock = true; var ids = SpecialChannelDb.GetChannelsByType(Model.ChannelType.Reddit); var channels = await GetChannels(ids); var grouped = channels.GroupBy(x => x.Subreddit); foreach (var sub in grouped) { await Post(sub); await Task.Delay(5000); } } catch (Exception ex) { SentrySdk.CaptureException(ex); } finally { RedditLock = false; } }
public async Task Unsubscribe(string name) { await Context.Channel.TriggerTypingAsync(); var subs = SpecialChannelDb.GetChannelsByGuild(Context.Guild.Id, Model.ChannelType.Reddit); var olds = subs.Where(x => x.Args.Split(",")[0].Equals(name, StringComparison.OrdinalIgnoreCase)); if (!olds.Any()) { await Context.Channel.SendMessageAsync($"Subreddit **{name}** not found. Try `{Program.GetPrefix(Context)}sublist` for a list of your subreddits."); } foreach (var old in olds) { await SpecialChannelDb.Delete(old); await Context.Channel.SendMessageAsync($"Unsubscribed from **{name}**. Were their posts not good enough?"); } }
public static EmbedBuilder SubListEmbed(ulong guildId) { var eb = new EmbedBuilder(); var subs = SpecialChannelDb.GetChannelsByGuild(guildId, Model.ChannelType.Reddit); string desc = ""; int i = 1; foreach (var sub in subs) { string[] args = sub.Args.Split(","); desc += $"{i++}. *{args[0]}* - **{args[1]}** upvotes\n"; } eb.WithDescription(desc == "" ? "-" : desc); eb.WithAuthor("Subreddits subscribed in this server"); eb.WithColor(BasicUtil.RandomColor()); eb.WithFooter($"Type `{Program.GetPrefix(guildId)}unsub [name]` to unsubscribe."); return(eb); }
public static async Task <List <RedditChannel> > GetChannels(IEnumerable <SpecialChannel> ids) { var client = Program.GetClient(); var channels = new List <RedditChannel>(); await Task.Run(async() => { foreach (var x in ids) { try { var ch = client.GetChannel(x.ChannelId); if (ch == null) { await SpecialChannelDb.Delete(x.ChannelId); SentrySdk.WithScope(scope => { scope.SetExtras(x.GetProperties()); SentrySdk.CaptureMessage("Deleted subreddit channel"); }); } else if (ch.GetType() == typeof(SocketTextChannel)) { channels.Add(new RedditChannel { Channel = (SocketTextChannel)ch, Subreddit = x.Args.Split(',')[0], Upvotes = Int32.Parse(x.Args.Split(',')[1]) }); } } catch (Exception ex) { SentrySdk.CaptureException(ex); } } }); return(channels); }
public async Task Subreddit(string name, int upvotes) { var subs = SpecialChannelDb.GetChannelsByGuild(Context.Guild.Id, Model.ChannelType.Reddit); int limit = 1; if (PremiumDb.IsPremium(Context.Guild.Id, ProType.Guild)) { limit = 5; } if (PremiumDb.IsPremium(Context.Guild.Id, ProType.GuildPlus)) { limit = 10; } if (subs.Count() >= limit) { await Context.Channel.SendMessageAsync($"Limit {limit} subscription per guild. Upgrade server to increase the limit! `{Program.GetPrefix(Context)}Pro`", embed : WebUtil.SubListEmbed(Context.Guild.Id).Build()); return; } if (upvotes < 100) { await Context.Channel.SendMessageAsync("Upvote minimum must be at least 100."); return; } await Context.Channel.TriggerTypingAsync(); Subreddit sub = null; try { sub = await RedditAPI.GetSubreddit(name); } catch { await Context.Channel.SendMessageAsync($"Subreddit **{name}** not found."); return; } if (sub.Subscribers < 20000) { await Context.Channel.SendMessageAsync("The Subreddit must have at least **20,000** subscribers.\n" + $"**{sub.Name}** has **{sub.Subscribers?.ToString("n0")}**."); return; } try { if (sub.Over18.Value && !((SocketTextChannel)Context.Channel).IsNsfw) { await Context.Channel.SendMessageAsync($"**{sub.Name}** is a NSFW subreddit. This channel must be marked as NSFW."); return; } } catch { } var old = subs.FirstOrDefault(x => x.ChannelId == Context.Channel.Id && x.Args.Split(",")[0].Equals(sub.Name)); if (old != null) { await SpecialChannelDb.Delete(old); } await SpecialChannelDb.AddChannel(Context.Channel.Id, Model.ChannelType.Reddit, Context.Guild.Id, sub.Name + "," + upvotes); await Context.Channel.SendMessageAsync(embed : WebUtil.SubredditSubscribedEmbed(sub, upvotes).Build()); }
public static async Task Post(IGrouping <string, RedditChannel> sub) { var hot = await RedditAPI.GetHot(sub.Key); if (hot == null) { return; } foreach (var post in hot) { var dbUpvotes = RedditDb.GetUpvotes(post.Permalink); var channels = sub.Where(ch => ch.Upvotes <post.UpVotes && ch.Upvotes> dbUpvotes && !(post.NSFW && !ch.Channel.IsNsfw)); if (!channels.Any()) { continue; } await RedditDb.AddPost(post.Permalink, post.UpVotes); var eb = RedditPostEmbed(post, sub.Key); if (eb == null) { continue; } var embed = eb.Build(); foreach (var ch in channels) { try { if (!ch.Channel.Guild.CurrentUser.GetPermissions(ch.Channel).Has(ChannelPermission.SendMessages) || !ch.Channel.Guild.CurrentUser.GetPermissions(ch.Channel).Has(ChannelPermission.EmbedLinks)) { await SpecialChannelDb.Delete(ch.Channel.Id); SentrySdk.WithScope(scope => { scope.SetExtras(ch.GetProperties()); SentrySdk.CaptureMessage("Deleted subreddit channel"); }); await ch.Channel.Guild.Owner.SendMessageAsync(embed : new EmbedBuilder() .WithTitle($"r/{ch.Subreddit} subscription cancel") .WithDescription($"I do not have permission to send messages to channel **{ch.Channel.Name}**. Therefore, I cannot send posts from your subscribed subreddit.\n\n" + $"I have automatically unsubscribed. If you would like to subscribe again, use the `subreddit` command and make sure I have the permission to send messages and embed links in the channel.") .WithColor(Color.DarkRed) .Build()); } else { var msg = await ch.Channel.SendMessageAsync(embed : embed); _ = Task.Run(async() => { await msg.AddReactionAsync(Emote.Parse("<:SignUpvote:577919849250947072>")); await msg.AddReactionAsync(Emote.Parse("<:SignDownvote:577919848554823680>")); }); } } catch (Exception ex) { SentrySdk.WithScope(scope => { scope.SetExtras(ch.GetProperties()); SentrySdk.CaptureException(ex); }); } } return; } }