Skip to content

Commit

Permalink
#37, update twitter module to allow any twitter account
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Moore committed Dec 11, 2016
1 parent a233e47 commit 832aba1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
22 changes: 22 additions & 0 deletions Disbott/Controllers/Twitter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using Tweetinvi;
using Tweetinvi.Models;

namespace Disbott.Controllers
{
Expand All @@ -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;
}
}
}
51 changes: 14 additions & 37 deletions Disbott/Views/TwitterModule.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
Expand Down

0 comments on commit 832aba1

Please sign in to comment.