예제 #1
0
        public void GetFeedURLForSubredditTest()
        {
            string sub  = null;
            string aww  = "https://www.reddit.com/r/aww/new/.rss";
            string csgo = "https://www.reddit.com/r/globaloffensive/new/.rss";

            Assert.AreEqual(aww, RssService.GetFeedURLForSubreddit("/r/aww", out sub));
            Assert.AreEqual(sub, "/r/aww");
            Assert.AreEqual(aww, RssService.GetFeedURLForSubreddit("r/aww", out sub));
            Assert.AreEqual(sub, "/r/aww");
            Assert.AreEqual(aww, RssService.GetFeedURLForSubreddit("/aww", out sub));
            Assert.AreEqual(sub, "/r/aww");
            Assert.AreEqual(aww, RssService.GetFeedURLForSubreddit("/r/aWW", out sub));
            Assert.AreEqual(sub, "/r/aww");
            Assert.AreEqual(aww, RssService.GetFeedURLForSubreddit("/R/aww", out sub));
            Assert.AreEqual(sub, "/r/aww");
            Assert.AreEqual(aww, RssService.GetFeedURLForSubreddit("/AWW", out sub));
            Assert.AreEqual(sub, "/r/aww");
            Assert.AreEqual(csgo, RssService.GetFeedURLForSubreddit("GlobalOffensive", out sub));
            Assert.AreEqual(sub, "/r/globaloffensive");

            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global Offensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global.Offensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global-Offensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global*Offensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global?Offensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global??Offensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global>Offensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("Global??Off>ensive", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit(".", out sub));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedURLForSubreddit("---", out sub));

            Assert.IsNull(RssService.GetFeedURLForSubreddit("FOOASDSADSANDJSKANDSKJANDSKAD", out sub));
        }
예제 #2
0
        public async Task UnsubscribeAsync(CommandContext ctx,
                                           [Description("Subreddit.")] string sub)
        {
            if (RssService.GetFeedURLForSubreddit(sub, out string rsub) == null)
            {
                throw new CommandFailedException("That subreddit doesn't exist.");
            }

            await this.Database.RemoveSubscriptionByNameAsync(ctx.Channel.Id, rsub);

            await this.InformAsync(ctx, $"Unsubscribed from {Formatter.Bold(rsub)}", important : false);
        }
예제 #3
0
        public async Task SubscribeAsync(CommandContext ctx,
                                         [Description("Subreddit.")] string sub)
        {
            string url = RssService.GetFeedURLForSubreddit(sub, out string rsub);

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

            if (!await this.Database.TryAddSubscriptionAsync(ctx.Channel.Id, url, rsub))
            {
                throw new CommandFailedException("You are already subscribed to this subreddit!");
            }

            await this.InformAsync(ctx, $"Subscribed to {Formatter.Bold(rsub)}", important : false);
        }
예제 #4
0
        public Task ExecuteGroupAsync(CommandContext ctx,
                                      [Description("Subreddit.")] string sub = "all")
        {
            string url = RssService.GetFeedURLForSubreddit(sub, out string rsub);

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

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

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

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }