Esempio n. 1
0
File: Web.cs Progetto: ta1H3n/Namiko
        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());
        }
Esempio n. 2
0
        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;
            }
        }