From 832aba14001e7effdb21f12863438882f32bf31c Mon Sep 17 00:00:00 2001 From: Thomas Moore Date: Sun, 11 Dec 2016 17:36:03 +0000 Subject: [PATCH] #37, update twitter module to allow any twitter account --- Disbott/Controllers/Twitter.cs | 22 +++++++++++++++ Disbott/Views/TwitterModule.cs | 51 ++++++++++------------------------ 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/Disbott/Controllers/Twitter.cs b/Disbott/Controllers/Twitter.cs index 1307be4..ac45ece 100644 --- a/Disbott/Controllers/Twitter.cs +++ b/Disbott/Controllers/Twitter.cs @@ -1,5 +1,7 @@ using System; +using System.Linq; using Tweetinvi; +using Tweetinvi.Models; namespace Disbott.Controllers { @@ -14,5 +16,25 @@ public static void Authenticate() Environment.GetEnvironmentVariable("twitter_access_token_secret") ); } + + public static string GetUsersLatestTweet(string twitterAccount) + { + Authenticate(); + var userTweets = Timeline.GetUserTimeline(twitterAccount, 1); + var tweets = userTweets as ITweet[] ?? userTweets.ToArray(); + var tweetMsg = tweets[0].FullText; + return tweetMsg; + } + + public static string GetUsersRandomTweet(string twitterAccount) + { + Random rnd = new Random(); + Authenticate(); + var userTweets = Timeline.GetUserTimeline(twitterAccount); + var tweets = userTweets as ITweet[] ?? userTweets.ToArray(); + var selectedTweetNumber = rnd.Next(0, 39); + var tweetMsg = tweets[selectedTweetNumber].FullText; + return tweetMsg; + } } } diff --git a/Disbott/Views/TwitterModule.cs b/Disbott/Views/TwitterModule.cs index 8c0dbfe..2b88fa7 100644 --- a/Disbott/Views/TwitterModule.cs +++ b/Disbott/Views/TwitterModule.cs @@ -1,10 +1,6 @@ -using System; -using System.Linq; -using System.Threading.Tasks; +using System.Threading.Tasks; using Discord.Commands; -using Tweetinvi; -using Tweetinvi.Models; using Disbott.Controllers; @@ -17,50 +13,31 @@ public class TwitterModule : ModuleBase [Remarks("Gets the latest tweet from The Guardian's Twitter Account")] public async Task Headline() { - TwitterController.Authenticate(); - var userTweets = Timeline.GetUserTimeline("guardian", 1); - var tweets = userTweets as ITweet[] ?? userTweets.ToArray(); - var tweetMsg = tweets[0].FullText; - + var tweetMsg = TwitterController.GetUsersLatestTweet("guardian"); await ReplyAsync(tweetMsg); } - [Command("mirin")] - [Remarks("Gets the latest tweet from Mirin Furukawa's Twitter Account")] - public async Task Mirin() + [Command("tweet")] + [Remarks("Gets the latest tweet from specified Twitter Account")] + public async Task Tweet([Remainder]string twitterAccount) { - TwitterController.Authenticate(); - var userTweets = Timeline.GetUserTimeline("furukawamirin", 1); - var tweets = userTweets as ITweet[] ?? userTweets.ToArray(); - var tweetMsg = tweets[0].FullText; - - await ReplyAsync(tweetMsg); + var tweetMsg = TwitterController.GetUsersLatestTweet(twitterAccount); + await ReplyAsync(twitterAccount + ": " + tweetMsg); } - [Command("gumi")] - [Remarks("Gets the latest tweet from Nanase Gumi's Twitter Account")] - public async Task Gumi() + [Command("random-tweet")] + [Remarks("Gets a random tweet from the last 40 tweets on the specified Twitter Account")] + public async Task RandomTweet([Remainder]string twitterAccount) { - TwitterController.Authenticate(); - var userTweets = Timeline.GetUserTimeline("gumi_nanase", 1); - var tweets = userTweets as ITweet[] ?? userTweets.ToArray(); - var tweetMsg = tweets[0].FullText; - - await ReplyAsync(tweetMsg); + var tweetMsg = TwitterController.GetUsersRandomTweet(twitterAccount); + await ReplyAsync(twitterAccount + ": " + tweetMsg); } [Command("gazo")] - [Remarks("NSFW, gets a random image from @idol_gazo twitter account")] + [Remarks("NSFW, gets a random image from the last 40 tweets on @idol_gazo twitter account")] public async Task Gazo() { - Random rnd = new Random(); - TwitterController.Authenticate(); - - var userTweets = Timeline.GetUserTimeline("idol_gazo"); - var tweets = userTweets as ITweet[] ?? userTweets.ToArray(); - var selectedTweetNumber = rnd.Next(0, 39); - var tweetMsg = tweets[selectedTweetNumber].FullText; - + var tweetMsg = TwitterController.GetUsersRandomTweet("idol_gazo"); await ReplyAsync(tweetMsg); } }