public async Task RedditAsync(CommandContext ctx,
                                          [Description("Subreddit.")] string sub)
            {
                string url = RedditService.GetFeedURLForSubreddit(sub, RedditCategory.New, out string rsub);

                if (url is null)
                {
                    throw new CommandFailedException("That subreddit doesn't exist.");
                }

                await this.Database.SubscribeAsync(ctx.Guild.Id, ctx.Channel.Id, url, rsub);

                await this.InformAsync(ctx, $"Subscribed to {Formatter.Bold(rsub)}", important : false);
            }
            public async Task RedditAsync(CommandContext ctx,
                                          [Description("Subreddit.")] string sub)
            {
                if (RedditService.GetFeedURLForSubreddit(sub, RedditCategory.New, out string rsub) is null)
                {
                    throw new CommandFailedException("That subreddit doesn't exist.");
                }

                using (DatabaseContext db = this.Database.CreateContext()) {
                    db.RssSubscriptions.RemoveRange(db.RssSubscriptions.Where(s => s.GuildId == ctx.Guild.Id && s.ChannelId == ctx.Channel.Id && s.Name == rsub));
                    await db.SaveChangesAsync();
                }

                await this.InformAsync(ctx, $"Unsubscribed from {Formatter.Bold(rsub)}", important : false);
            }
Exemple #3
0
        private Task SearchAndSendResultsAsync(CommandContext ctx, string sub, RedditCategory category)
        {
            if (string.IsNullOrWhiteSpace(sub))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-sub-none");
            }

            string?url = RedditService.GetFeedURLForSubreddit(sub, category, out string?rsub);

            if (url is null || rsub is null)
            {
                if (rsub is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-sub-format");
                }
                else
                {
                    throw new CommandFailedException(ctx, "cmd-err-sub-404", rsub);
                }
            }

            IReadOnlyList <SyndicationItem>?res = RssFeedsService.GetFeedResults(url);

            if (res is null)
            {
                throw new CommandFailedException(ctx, "cmd-err-sub-fail", rsub);
            }

            if (!res.Any())
            {
                return(ctx.FailAsync("cmd-err-res-none"));
            }

            return(ctx.PaginateAsync(res, (emb, r) => {
                emb.WithTitle(r.Title.Text);
                emb.WithDescription(r.Summary, unknown: false);
                emb.WithUrl(r.Links.First().Uri);
                if (r.Content is TextSyndicationContent content)
                {
                    string?url = RedditService.GetImageUrl(content);
                    if (url is { })
                    {
                        emb.WithImageUrl(url);
                    }
                }
Exemple #4
0
        private async Task SearchAndSendResults(CommandContext ctx, string sub, RedditCategory category)
        {
            string url = RedditService.GetFeedURLForSubreddit(sub, category, out string rsub);

            if (url is null)
            {
                throw new CommandFailedException("That subreddit doesn't exist.");
            }

            IReadOnlyList <SyndicationItem> res = RssService.GetFeedResults(url);

            if (res is null)
            {
                throw new CommandFailedException($"Failed to get the data from that subreddit ({Formatter.Bold(rsub)}).");
            }

            await RssService.SendFeedResultsAsync(ctx.Channel, res);
        }
Exemple #5
0
        public async Task UnsubscribeAsync(CommandContext ctx,
                                           [Description("desc-sub")] string sub)
        {
            if (string.IsNullOrWhiteSpace(sub))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-sub-none");
            }

            string?url = RedditService.GetFeedURLForSubreddit(sub, RedditCategory.New, out string?rsub);

            if (url is null || rsub is null)
            {
                if (rsub is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-sub-format");
                }
                else
                {
                    throw new CommandFailedException(ctx, "cmd-err-sub-404", rsub);
                }
            }

            RssFeed?feed = await this.Service.GetByUrlAsync(url);

            if (feed is null)
            {
                throw new CommandFailedException(ctx, "cmd-err-sub-not");
            }

            RssSubscription?s = await this.Service.Subscriptions.GetAsync((ctx.Guild.Id, ctx.Channel.Id), feed.Id);

            if (s is null)
            {
                throw new CommandFailedException(ctx, "cmd-err-sub-not");
            }

            await this.Service.Subscriptions.RemoveAsync((ctx.Guild.Id, ctx.Channel.Id), feed.Id);

            await ctx.InfoAsync(this.ModuleColor);
        }
Exemple #6
0
        public async Task SubscribeAsync(CommandContext ctx,
                                         [Description("desc-chn")] DiscordChannel chn,
                                         [Description("desc-sub")] string sub)
        {
            chn ??= ctx.Channel;
            if (chn.Type != ChannelType.Text)
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-chn-type-text");
            }

            if (string.IsNullOrWhiteSpace(sub))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-sub-none");
            }

            string?url = RedditService.GetFeedURLForSubreddit(sub, RedditCategory.New, out string?rsub);

            if (url is null || rsub is null)
            {
                if (rsub is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-sub-format");
                }
                else
                {
                    throw new CommandFailedException(ctx, "cmd-err-sub-404", rsub);
                }
            }

            if (await this.Service.SubscribeAsync(ctx.Guild.Id, ctx.Channel.Id, url, rsub))
            {
                await ctx.InfoAsync(this.ModuleColor);
            }
            else
            {
                await ctx.FailAsync("cmd-err-sub", url);
            }
        }