public async Task Subscriptions() { using (SubDBContext DBContext = DBFactory.Create <SubDBContext>()) { SimpleStopWatch watch = new SimpleStopWatch(); string channelId = Context.Channel.Id.ToString(); var cnnSub = DBContext.CNNSubscribers.FromSql("SELECT * FROM CNNSubscribers WHERE Id = {0} LIMIT 1", channelId).AsNoTracking().FirstOrDefault(); var redditSubs = DBContext.RedditSubscribers.FromSql("SELECT * FROM RedditSubscribers WHERE Id = {0}", channelId).AsNoTracking().ToList(); EmbedBuilder embedBuilder = new EmbedBuilder(); EmbedService.BuildFeedbackEmbed(embedBuilder); embedBuilder.Title = $"Subscriptions for {Context.Channel.Name.UpperFirstChar()}"; embedBuilder.Description = "\n\n"; embedBuilder.Description += $":newspaper: CNN Subscription Status: {(cnnSub == null ? "Not subscribed." : "Currently subscribed.").Bold()}\n\n"; embedBuilder.Description += ":monkey_face: Reddit Subscriptions: "; if (redditSubs == null || redditSubs.Count == 0) { embedBuilder.Description += "Not subscribed to any subreddits.".Bold(); } else { foreach (RedditSubscriber sub in redditSubs) { embedBuilder.Description += $"{sub.Subreddit.Bold()}, "; } embedBuilder.Description = embedBuilder.Description.Substring(0, embedBuilder.Description.Length - 2); } TimeSpan timeTook = watch.Stop(); embedBuilder.WithFooter(footer => { footer.Text = $"⏰ {"Generated in:"} {Math.Round(timeTook.TotalMilliseconds)}ms"; }); await ReplyAsync("", embed : embedBuilder.Build()); } }
protected override void Run() { DiscordSocketClient socketClient = Provider.GetService <DiscordSocketClient>(); socketClient.Ready += async() => { using (SubDBContext DBContext = Provider.GetService <DBContextFactory>().Create <SubDBContext>()) { var subscribers = DBContext.RedditSubscribers.FromSql("SELECT * FROM RedditSubscribers").AsNoTracking().ToList(); foreach (RedditSubscriber sub in subscribers) { SocketTextChannel textChannel = Client.GetChannel(ulong.Parse(sub.Id)) as SocketTextChannel; if (textChannel != null) { AddRedditStream(textChannel, sub.Subreddit, sub.Interval, ulong.Parse(sub.Subscriber)); ConsoleEx.WriteColoredLine(LogSeverity.Verbose, ConsoleTextFormat.TimeAndText, $"Attached $[[DarkMagenta]]$Reddit Stream$[[Gray]]$ ($[[Yellow]]${sub.Subreddit}$[[Gray]]$ to text channel $[[White]]${textChannel.Name}."); } else { Console.WriteLine("wtf"); } } } //Some channels I always want added. SocketTextChannel animeIrlChannel = socketClient.GetChannel(247490683194048513) as SocketTextChannel; SocketTextChannel lptChannel = socketClient.GetChannel(241651878633406474) as SocketTextChannel; ulong neosId = 140271751572619265; if (animeIrlChannel != null) { AddRedditStream(animeIrlChannel, "anime_irl", Config.RedditCheckInterval, neosId, RedditType.Image); ConsoleEx.WriteColoredLine(LogSeverity.Info, ConsoleTextFormat.TimeAndText, $"Attached $[[DarkMagenta]]$Reddit Stream$[[Gray]]$ $[[Yellow]]$(anime_irl)$[[Gray]]$ to {animeIrlChannel.Name}."); } if (lptChannel != null) { AddRedditStream(lptChannel, "LifeProTips", Config.RedditCheckInterval, neosId, RedditType.Text); ConsoleEx.WriteColoredLine(LogSeverity.Info, ConsoleTextFormat.TimeAndText, $"Attached $[[DarkMagenta]]$Reddit Stream$[[Gray]]$ $[[Yellow]]$(LifeProTips)$[[Gray]]$ to {animeIrlChannel.Name}."); } await Task.CompletedTask; }; //return Task.CompletedTask; //}; }
public async Task CNNUnsubscribe() { using (SubDBContext DBContext = DBFactory.Create <SubDBContext>()) { string channelId = Context.Channel.Id.ToString(); string userId = Context.User.Id.ToString(); var existingSub = DBContext.CNNSubscribers.FromSql("SELECT * FROM CNNSubscribers WHERE Id = {0} AND Subscriber = {1} LIMIT 1", Context.Channel.Id.ToString(), Context.User.Id.ToString()).AsNoTracking().FirstOrDefault(); if (existingSub == null) { await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("This channel is not subscribed to CNN or you weren't the subscriber."), lifeTime : Config.FeedbackMessageLifeTime); return; } DBContext.CNNSubscribers.Remove(existingSub); await DBContext.SaveChangesAsync(); CNNService.TextChannnels.Remove(Context.Channel as SocketTextChannel); await ReplyAsync("", embed : EmbedService.MakeSuccessFeedbackEmbed($"You have unsubscribed the channel {Context.Channel.Name.Bold()} from CNN breaking news!")); } }
public async Task RedditUnsubscribe([Summary("The desired subreddit")] string subreddit) { using (SubDBContext DBContext = DBFactory.Create <SubDBContext>()) { string channelId = Context.Channel.Id.ToString(); string userId = Context.User.Id.ToString(); var subExists = DBContext.RedditSubscribers.FromSql("SELECT * FROM RedditSubscribers WHERE Id = {0} AND Subscriber = {1} AND Subreddit = {2} LIMIT 1", Context.Channel.Id.ToString(), Context.User.Id.ToString(), subreddit).AsNoTracking().FirstOrDefault(); if (subExists == null) { await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("This channel is not subscribed to that subreddit or you weren't the subscriber."), lifeTime : Config.FeedbackMessageLifeTime); return; } DBContext.RedditSubscribers.Remove(subExists); await DBContext.SaveChangesAsync(); RedditService.RemoveRedditStream(Context.Channel as SocketTextChannel, Context.User as SocketGuildUser, subreddit); await ReplyAsync("", embed : EmbedService.MakeSuccessFeedbackEmbed($"You have unsubscribed the channel {Context.Channel.Name.Bold()} from the subreddit {subreddit.Bold()}!")); } }
public async Task CNNSubscribe() { using (SubDBContext DBContext = DBFactory.Create <SubDBContext>()) { string channelId = Context.Channel.Id.ToString(); string userId = Context.User.Id.ToString(); var subExists = (DBContext.CNNSubscribers.FromSql("SELECT * FROM CNNSubscribers WHERE Id = {0} AND Subscriber = {1} LIMIT 1", Context.Channel.Id.ToString(), Context.User.Id.ToString()).AsNoTracking().FirstOrDefault() != null); if (subExists) { await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("This channel is already subscribed to CNN."), lifeTime : Config.FeedbackMessageLifeTime); return; } var newUser = new CNNSubscriber(channelId, userId, DateTime.Now); await DBContext.CNNSubscribers.AddAsync(newUser); await DBContext.SaveChangesAsync(); CNNService.TextChannnels.Add(Context.Channel as SocketTextChannel); await ReplyAsync("", embed : EmbedService.MakeSuccessFeedbackEmbed($"You have subscribed the channel {Context.Channel.Name.Bold()} to CNN breaking news!")); } }
public async Task RedditSubscribe([Summary("The interval between each check, in minutes.")] int minutes, [Summary("The name of the subreddit.")] string subreddit) { if (minutes < 5) { await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("Your check interval cannot be shorter than five minutes."), lifeTime : Config.FeedbackMessageLifeTime); return; } if (minutes > 1440) { await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("Your check interval cannot be longer than a day."), lifeTime : Config.FeedbackMessageLifeTime); return; } using (SubDBContext DBContext = DBFactory.Create <SubDBContext>()) { string channelId = Context.Channel.Id.ToString(); string userId = Context.User.Id.ToString(); var subExists = (DBContext.RedditSubscribers.FromSql("SELECT * FROM RedditSubscribers WHERE Id = {0} AND Subscriber = {1} AND Subreddit = {2} LIMIT 1", Context.Channel.Id.ToString(), Context.User.Id.ToString(), subreddit).AsNoTracking().FirstOrDefault() != null); if (subExists) { await ReplyAsync("", embed : EmbedService.MakeFailFeedbackEmbed("This channel is already subscribed to the specified subreddit."), lifeTime : Config.FeedbackMessageLifeTime); return; } var newUser = new RedditSubscriber(channelId, userId, subreddit, "Text", minutes, DateTime.Now); await DBContext.RedditSubscribers.AddAsync(newUser); await DBContext.SaveChangesAsync(); RedditService.AddRedditStream(Context.Channel as SocketTextChannel, subreddit, minutes, Context.User.Id); await ReplyAsync("", embed : EmbedService.MakeSuccessFeedbackEmbed($"You have subscribed the channel {Context.Channel.Name.Bold()} to the subreddit {subreddit.Bold()}!")); } }
protected override void Run() { bool firstRun = true; //So articles don't spam when I restart the bot. bool readyFired = false; DiscordSocketClient client = Provider.GetService <DiscordSocketClient>(); client.Ready += () => { if (readyFired) { return(Task.CompletedTask); } readyFired = true; using (SubDBContext DBContext = Factory.Create <SubDBContext>()) { var subscribers = DBContext.CNNSubscribers.FromSql("SELECT * FROM CNNSubscribers"); foreach (CNNSubscriber sub in subscribers) { SocketTextChannel textChannel = client.GetChannel(ulong.Parse(sub.Id)) as SocketTextChannel; if (!TextChannnels.Contains(textChannel)) { TextChannnels.Add(textChannel); } ConsoleEx.WriteColoredLine(Discord.LogSeverity.Verbose, ConsoleTextFormat.TimeAndText, $"Attached $[[DarkMagenta]]$CNNService$[[Gray]]$ to text channel $[[White]]${textChannel.Name}."); } } StaticMethods.FireAndForget(() => { ConsoleEx.WriteColoredLine(Discord.LogSeverity.Info, ConsoleTextFormat.TimeAndText, $"Attached CNNService to $[[Green]]${TextChannnels.Count}$[[Gray]]$ channels."); int checkInterval = Provider.GetService <MathService>().TimeUnitToMilli(TimeUnit.Minutes, Config.CNNCheckInterval); CheckTimer = new Timer(async(e) => { try { string result = await HttpService.Get("http://www.cnn.com/"); Regex articlesRegex = new Regex("siblings:(.*?)(?= ,)"); Match match = articlesRegex.Match(result); string trimmedMatch = match.Value.TrimEnd(',').Replace("siblings: ", ""); Dictionary <string, dynamic> json = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(trimmedMatch)); string articleTitle = json["articleList"][0]["headline"].ToString(); string articleUrl = json["articleList"][0]["uri"].ToString(); if (PostedArticles.FirstOrDefault(x => x == articleUrl) == null) { PostedArticles.Add(articleUrl); if (!firstRun) { foreach (SocketTextChannel textChannel in TextChannnels) { await textChannel.SendMessageAsync($"http://www.cnn.com{articleUrl}"); } } } firstRun = false; await Task.CompletedTask; } catch (Exception ex) { ConsoleEx.WriteColoredLine($"$[[DarkCyan]]$CNNService$[[Gray]]$ has made an exception\n$[[Red]]${ex.Message}"); Console.WriteLine(ex.Message); } CheckTimer.Change(checkInterval, 0); }, null, checkInterval, 0); }); return(Task.CompletedTask); }; }