Ejemplo n.º 1
0
            public async Task UnsubscribeAsync(CommandContext ctx,
                                               [Description("Channel URL or subscription name.")] string name_url)
            {
                if (string.IsNullOrWhiteSpace(name_url))
                {
                    throw new InvalidCommandUsageException("Channel URL missing.");
                }

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

                string chid = await ctx.Services.GetService <YtService>().ExtractChannelIdAsync(name_url);

                if (!(chid is null))
                {
                    string feedurl = YtService.GetRssUrlForChannel(chid);
                    using (var dc = this.Database.CreateContext())
                    {
                        var sub = dc.RssSubscriptions.SingleOrDefault(s => s.ChannelId == ctx.Channel.Id && s.DbRssFeed.Url == feedurl);
                        if (!(sub is null))
                        {
                            dc.RssSubscriptions.Remove(sub);
                            await dc.SaveChangesAsync();
                        }
                    }
                }

                await this.InformAsync(ctx, "Unsubscribed!", important : false);
            }
Ejemplo n.º 2
0
        public async Task LinkVideo([Remainder][Summary("The search query")] string query)
        {
            var yt       = new YtService();
            var response = await yt.GetVideoLinkAsync(query);

            if (response != null)
            {
                await ReplyAsync(response);

                return;
            }
            await Error("No videos found.");
        }
Ejemplo n.º 3
0
 public async Task InitAsync()
 {
     try {
         string json;
         using (var sr = new StreamReader("Resources/config.json"))
             json = await sr.ReadToEndAsync();
         var cfg = JsonConvert.DeserializeObject <BotConfig>(json);
         this.yt = new YtService(cfg.YouTubeKey);
     } catch {
         Assert.Warn("Config file not found or YouTube key isn't valid (service disabled).");
         this.yt = new YtService(null);
     }
 }
Ejemplo n.º 4
0
            public async Task SubscribeAsync(CommandContext ctx,
                                             [Description("Channel URL.")] string url,
                                             [Description("Friendly name.")] string name = null)
            {
                string chid = await ctx.Services.GetService <YtService>().ExtractChannelIdAsync(url);

                if (chid is null)
                {
                    throw new CommandFailedException("Failed retrieving channel ID for that URL.");
                }

                string feedurl = YtService.GetRssUrlForChannel(chid);

                await this.Database.SubscribeAsync(ctx.Guild.Id, ctx.Channel.Id, feedurl, string.IsNullOrWhiteSpace(name)?url : name);

                await this.InformAsync(ctx, "Subscribed!", important : false);
            }
Ejemplo n.º 5
0
        public void GetRssUrlForChannelTest()
        {
            if (this.yt.IsDisabled())
            {
                Assert.Inconclusive("Service has not been properly initialized.");
            }

            Assert.IsNotNull(YtService.GetRssUrlForChannel("UCuAXFkgsw1L7xaCfnd5JJOw"));

            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel(null));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel(""));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel(" "));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel("\n"));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel("/"));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel("test|"));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel("@4"));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel("user/123"));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel("*user"));
            Assert.Throws <ArgumentException>(() => YtService.GetRssUrlForChannel("* user"));
        }
Ejemplo n.º 6
0
        public async Task PlayAsync(CommandContext ctx,
                                    [RemainingText, Description("desc-audio-query")] string query)
        {
            if (string.IsNullOrWhiteSpace(query))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-query");
            }

            YtService yt = ctx.Services.GetRequiredService <YtService>();

            if (yt.IsDisabled)
            {
                throw new ServiceDisabledException(ctx);
            }

            IReadOnlyList <SearchResult>?res = await yt.SearchAsync(query, 1);

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

            if (!res.Any())
            {
                throw new CommandFailedException(ctx, "cmd-err-res-none");
            }

            string?url = yt.GetUrlForResourceId(res[0].Id);

            if (url is null)
            {
                throw new CommandFailedException(ctx, "cmd-err-yt");
            }

            await this.PlayAsync(ctx, new Uri(url));
        }
Ejemplo n.º 7
0
 public PlayModule(YtService yt, SharedData shared, DBService db)
     : base(yt, shared, db)
 {
     this.ModuleColor = DiscordColor.Grayple;
 }
Ejemplo n.º 8
0
 public PlayModule(YtService yt, SharedData shared, DatabaseContextBuilder db)
     : base(yt, shared, db)
 {
     this.ModuleColor = DiscordColor.Grayple;
 }