From 59e3357be82d74d460d7baa7527d7a13783eeedd Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Sun, 24 Sep 2017 11:12:58 -0400 Subject: [PATCH] moah commands --- src/StreamElements.Net/AuthRestClient.cs | 109 +++++++++++++++--- src/StreamElements.Net/Models/Bot.cs | 4 +- src/StreamElements.Net/Models/BotTimer.cs | 52 +++++++++ src/StreamElements.Net/Models/ChatStats.cs | 96 +++++++++++++++ .../Models/ChatStatsSettings.cs | 43 +++++++ .../Models/Results/BotCommandResult.cs | 20 ++++ .../Models/Results/BotTimerResult.cs | 19 +++ .../Models/Results/ChatStatsSettingsResult.cs | 19 +++ .../Models/Results/IStreamElementsResult.cs | 19 +++ .../Rest/IAuthenticatedStreamElementsApi.cs | 7 ++ .../Rest/IGenericRestEndpoint.cs | 12 +- .../Rest/IStreamElementsClient.cs | 15 +++ src/StreamElements.Net/RestClient.cs | 25 ++++ 13 files changed, 415 insertions(+), 25 deletions(-) create mode 100644 src/StreamElements.Net/Models/BotTimer.cs create mode 100644 src/StreamElements.Net/Models/ChatStats.cs create mode 100644 src/StreamElements.Net/Models/ChatStatsSettings.cs create mode 100644 src/StreamElements.Net/Models/Results/BotCommandResult.cs create mode 100644 src/StreamElements.Net/Models/Results/BotTimerResult.cs create mode 100644 src/StreamElements.Net/Models/Results/ChatStatsSettingsResult.cs create mode 100644 src/StreamElements.Net/Models/Results/IStreamElementsResult.cs create mode 100644 src/StreamElements.Net/Rest/IStreamElementsClient.cs diff --git a/src/StreamElements.Net/AuthRestClient.cs b/src/StreamElements.Net/AuthRestClient.cs index bb7c81f..f964eae 100644 --- a/src/StreamElements.Net/AuthRestClient.cs +++ b/src/StreamElements.Net/AuthRestClient.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Threading.Tasks; using Refit; using StreamElements.Net.Models; +using StreamElements.Net.Models.Results; using StreamElements.Net.Rest; namespace StreamElements.Net @@ -12,8 +13,10 @@ namespace StreamElements.Net public class AuthRestClient : RestClient { private string JwtToken { get; } - private IAuthenticatedStreamElementsApi Client { get; } - private IGenericRestEndpoint BotCommandClient { get; } + private IAuthenticatedStreamElementsApi AuthClient { get; } + private IGenericRestEndpoint BotCommandClient { get; } + private IGenericRestEndpoint BotTimerClient { get; } + public AuthRestClient(string jwtToken) { this.JwtToken = jwtToken; @@ -21,14 +24,15 @@ namespace StreamElements.Net { throw new ArgumentNullException(nameof(jwtToken)); } - this.Client = BuildHttpClient(); - BotCommandClient = BuildHttpClient>("bot/commands"); + this.AuthClient = BuildHttpClient(); + BotCommandClient = BuildHttpClient>("bot/commands"); + BotTimerClient = BuildHttpClient>("bot/timers"); } /// /// Returns a list of activities sorted by createdAt. /// /// - public Task> GetActivities() => Client.GetActivitiesAsync(); + public Task> GetActivities() => AuthClient.GetActivitiesAsync(); /// /// Returns a single activity @@ -41,18 +45,18 @@ namespace StreamElements.Net { throw new ArgumentNullException(nameof(id)); } - return Client.GetActivityAsync(id); + return AuthClient.GetActivityAsync(id); } /// /// Returns an object with information about the bot. /// /// - public Task GetBotAsync() => Client.GetBotAsync(); + public Task GetBotAsync() => AuthClient.GetBotAsync(); /// /// Returns the bots action log. These logs include command updates. /// /// - public Task> GetBotLogsAsync() => Client.GetBotLogsAsync(); + public Task> GetBotLogsAsync() => AuthClient.GetBotLogsAsync(); /// /// Make the bot do something /// @@ -61,7 +65,7 @@ namespace StreamElements.Net public Task PostBotActionAsync(BotActionEnum action) { var actionParsed = Enum.GetName(typeof(BotActionEnum), action); - return this.Client.PostBotActionAsync(actionParsed); + return this.AuthClient.PostBotActionAsync(actionParsed); } /// /// Makes the bot send a message in current user's the channel. @@ -74,13 +78,13 @@ namespace StreamElements.Net { throw new ArgumentNullException(nameof(message)); } - return Client.PostBotSayAsync(new{ message } ); + return AuthClient.PostBotSayAsync(new{ message } ); } /// /// Returns an array of users with levels in your channel. /// /// - public Task GetBotLevels() => Client.GetBotLevelsAsync(); + public Task GetBotLevels() => AuthClient.GetBotLevelsAsync(); /// /// Create a new permission for the current channel. /// @@ -91,35 +95,104 @@ namespace StreamElements.Net { throw new ArgumentNullException(nameof(userName)); } - return Client.PostBotLevelAsync(new { username = userName, level = botEnum}); + return AuthClient.PostBotLevelAsync(new { username = userName, level = botEnum}); } - public Task> GetBotCommandsAsync() => BotCommandClient.GetAllAsync(); + public Task> GetBotCommandsAsync() => BotCommandClient.GetAllAsync(); - public Task GetBotCommandAsync(string id) + public Task GetBotCommandAsync(string id) { if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id)); return BotCommandClient.GetOneAsync(id); } - public Task PostBotCommand(BotCommand command) + public Task PostBotCommand(BotCommand command) { if(command == null) throw new ArgumentNullException(nameof(command)); return BotCommandClient.CreateAsync(command); } - public Task PutBotCommand (string id, BotCommand command) + public Task PutBotCommand (string id, BotCommand command) { if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id)); if(command == null) throw new ArgumentNullException(nameof(command)); return BotCommandClient.UpdateAsync(id, command); } - public Task DeleteCommand(string id) + public Task DeleteBotCommand(string id) { - if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id)); + if(string.IsNullOrWhiteSpace(id)) + { + throw new ArgumentNullException(nameof(id)); + } return BotCommandClient.DeleteAsync(id); } + /// + /// Returns an array of timers + /// + /// + public Task> GetBotTimers() => this.BotTimerClient.GetAllAsync(); + + /// + /// Returns a single timer + /// + /// + /// + public Task GetBotTimer(string id) + { + if(string.IsNullOrWhiteSpace(id)) + { + throw new ArgumentNullException(nameof(id)); + } + return this.BotTimerClient.GetOneAsync(id); + } + /// + /// Update a single timer + /// + /// + public Task UpdateBotTimer(string id, BotTimer timer) + { + if(string.IsNullOrWhiteSpace(id)) + { + throw new ArgumentNullException(nameof(id)); + } + if(timer == null) + { + throw new ArgumentNullException(nameof(timer)); + } + return this.BotTimerClient.UpdateAsync(id, timer); + } + + public Task CreateBotTimer(BotTimer timer) + { + if(timer == null) + { + throw new ArgumentNullException(nameof(timer)); + } + return this.BotTimerClient.CreateAsync(timer); + } + + public Task DeleteBotTimer(string id) + { + if(string.IsNullOrWhiteSpace(id)) + { + throw new ArgumentNullException(nameof(id)); + } + return BotTimerClient.DeleteAsync(id); + } + + public Task GetChatStats => this.AuthClient.GetChatStatsSettings(); + + public Task UpdateChatStats(ChatStatistics statsistics) + { + if(statsistics == null) + { + throw new ArgumentNullException(nameof(statsistics)); + } + return this.AuthClient.UpdateChatStatsSettings(statsistics); + } + + public override T BuildHttpClient(string pathSegment = null) { var builder = new UriBuilder("https://api.streamelements.com/kappa/v1"); diff --git a/src/StreamElements.Net/Models/Bot.cs b/src/StreamElements.Net/Models/Bot.cs index 2deea76..d330bfa 100644 --- a/src/StreamElements.Net/Models/Bot.cs +++ b/src/StreamElements.Net/Models/Bot.cs @@ -21,7 +21,7 @@ namespace StreamElements.Net.Models public bool Enabled { get; set; } } - public class Stats + public class BotStats { [JsonProperty("commands")] @@ -41,7 +41,7 @@ namespace StreamElements.Net.Models public Bot Bot { get; set; } [JsonProperty("stats")] - public Stats Stats { get; set; } + public BotStats Stats { get; set; } } } \ No newline at end of file diff --git a/src/StreamElements.Net/Models/BotTimer.cs b/src/StreamElements.Net/Models/BotTimer.cs new file mode 100644 index 0000000..b7ebe42 --- /dev/null +++ b/src/StreamElements.Net/Models/BotTimer.cs @@ -0,0 +1,52 @@ +using Newtonsoft.Json; + +namespace StreamElements.Net.Models +{ + public class Offline + { + + [JsonProperty("interval")] + public int Interval { get; set; } + + [JsonProperty("enabled")] + public bool Enabled { get; set; } + } + + public class Online + { + + [JsonProperty("interval")] + public int Interval { get; set; } + + [JsonProperty("enabled")] + public bool Enabled { get; set; } + } + + public class BotTimer + { + + + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("message")] + public string Message { get; set; } + + [JsonProperty("_id")] + public string Id { get; set; } + + [JsonProperty("chatLines")] + public int ChatLines { get; set; } + + [JsonProperty("offline")] + public Offline Offline { get; set; } + + [JsonProperty("online")] + public Online Online { get; set; } + + [JsonProperty("enabled")] + public bool Enabled { get; set; } + } + +} \ No newline at end of file diff --git a/src/StreamElements.Net/Models/ChatStats.cs b/src/StreamElements.Net/Models/ChatStats.cs new file mode 100644 index 0000000..8ab4b0d --- /dev/null +++ b/src/StreamElements.Net/Models/ChatStats.cs @@ -0,0 +1,96 @@ +using System.Collections.Generic; +using Newtonsoft.Json; + +namespace StreamElements.Net.Models +{ + public class Chatter + { + + [JsonProperty("key")] + public string Key { get; set; } + + [JsonProperty("amount")] + public int Amount { get; set; } + } + + public class Chatlines + { + + [JsonProperty("chatters")] + public IList Chatters { get; set; } + + [JsonProperty("total")] + public string Total { get; set; } + } + + public class Twitch + { + + [JsonProperty("key")] + public string Key { get; set; } + + [JsonProperty("amount")] + public int Amount { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + } + + public class Bttv + { + + [JsonProperty("key")] + public string Key { get; set; } + + [JsonProperty("amount")] + public int Amount { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + } + + public class Emotes + { + + [JsonProperty("twitch")] + public IList Twitch { get; set; } + + [JsonProperty("bttv")] + public IList Bttv { get; set; } + } + + public class Command + { + + [JsonProperty("key")] + public string Key { get; set; } + + [JsonProperty("amount")] + public int Amount { get; set; } + } + + public class ChatStats + { + + [JsonProperty("status")] + public int Status { get; set; } + + [JsonProperty("channel")] + public string Channel { get; set; } + + [JsonProperty("amount")] + public int Amount { get; set; } + + [JsonProperty("chatlines")] + public Chatlines Chatlines { get; set; } + + [JsonProperty("emotes")] + public Emotes Emotes { get; set; } + + [JsonProperty("commands")] + public IList Commands { get; set; } + + [JsonProperty("hashtags")] + public IList Hashtags { get; set; } + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/Models/ChatStatsSettings.cs b/src/StreamElements.Net/Models/ChatStatsSettings.cs new file mode 100644 index 0000000..872a7e9 --- /dev/null +++ b/src/StreamElements.Net/Models/ChatStatsSettings.cs @@ -0,0 +1,43 @@ +using Newtonsoft.Json; + +namespace StreamElements.Net.Models +{ + public class ChatStatsSettings + { + [JsonProperty("stats")] + public ChatStatistics Stats { get; set; } + + [JsonProperty("twitchchat")] + public bool Twitchchat { get; set; } + + [JsonProperty("ignored_chatters")] + public string IgnoredChatters { get; set; } + + [JsonProperty("messages_per_second")] + public bool MessagesPerSecond { get; set; } + + [JsonProperty("total_messages")] + public bool TotalMessages { get; set; } + + [JsonProperty("emoteflow")] + public bool Emoteflow { get; set; } + } + public class ChatStatistics + { + + [JsonProperty("commands")] + public int Commands { get; set; } + + [JsonProperty("hashtags")] + public int Hashtags { get; set; } + + [JsonProperty("bttv")] + public int Bttv { get; set; } + + [JsonProperty("twitch")] + public int Twitch { get; set; } + + [JsonProperty("top_chatters")] + public int TopChatters { get; set; } + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/Models/Results/BotCommandResult.cs b/src/StreamElements.Net/Models/Results/BotCommandResult.cs new file mode 100644 index 0000000..6f88c23 --- /dev/null +++ b/src/StreamElements.Net/Models/Results/BotCommandResult.cs @@ -0,0 +1,20 @@ +using System; +using Newtonsoft.Json; + +namespace StreamElements.Net.Models.Results +{ + public class BotCommandResult : BotCommand, IStreamElementsResult + { + [JsonProperty("updatedAt")] + System.DateTime IStreamElementsResult.UpdatedAt { get; set; } + + [JsonProperty("createdAt")] + System.DateTime IStreamElementsResult.CreatedAt { get; set; } + + [JsonProperty("_user")] + string IStreamElementsResult.User { get; set; } + + [JsonProperty("_username")] + string IStreamElementsResult.Username { get; set; } + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/Models/Results/BotTimerResult.cs b/src/StreamElements.Net/Models/Results/BotTimerResult.cs new file mode 100644 index 0000000..8f3b78e --- /dev/null +++ b/src/StreamElements.Net/Models/Results/BotTimerResult.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; + +namespace StreamElements.Net.Models.Results +{ + public class BotTimerResult : BotTimer, IStreamElementsResult + { + [JsonProperty("updatedAt")] + public System.DateTime UpdatedAt { get; set; } + + [JsonProperty("createdAt")] + public System.DateTime CreatedAt { get; set; } + + [JsonProperty("_user")] + public string User { get; set; } + + [JsonProperty("_username")] + public string Username { get; set; } + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/Models/Results/ChatStatsSettingsResult.cs b/src/StreamElements.Net/Models/Results/ChatStatsSettingsResult.cs new file mode 100644 index 0000000..7cffa98 --- /dev/null +++ b/src/StreamElements.Net/Models/Results/ChatStatsSettingsResult.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; + +namespace StreamElements.Net.Models.Results +{ + public class ChatStatsSettingsResult : ChatStatsSettings, IStreamElementsResult + { + [JsonProperty("updatedAt")] + public System.DateTime UpdatedAt { get; set; } + + [JsonProperty("createdAt")] + public System.DateTime CreatedAt { get; set; } + + [JsonProperty("_user")] + public string User { get; set; } + + [JsonProperty("_username")] + public string Username { get; set; } + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/Models/Results/IStreamElementsResult.cs b/src/StreamElements.Net/Models/Results/IStreamElementsResult.cs new file mode 100644 index 0000000..15a23aa --- /dev/null +++ b/src/StreamElements.Net/Models/Results/IStreamElementsResult.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; + +namespace StreamElements.Net.Models.Results +{ + public interface IStreamElementsResult + { + [JsonProperty("updatedAt")] + System.DateTime UpdatedAt { get; set; } + + [JsonProperty("createdAt")] + System.DateTime CreatedAt { get; set; } + + [JsonProperty("_user")] + string User { get; set; } + + [JsonProperty("_username")] + string Username { get; set; } + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs b/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs index 69fd2c5..dd4a5b1 100644 --- a/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs +++ b/src/StreamElements.Net/Rest/IAuthenticatedStreamElementsApi.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Refit; using StreamElements.Net.Models; +using StreamElements.Net.Models.Results; namespace StreamElements.Net.Rest { @@ -30,5 +31,11 @@ namespace StreamElements.Net.Rest [Post("/bot/levels")] Task PostBotLevelAsync([Body] object submit); + + [Get("/chatstats")] + Task GetChatStatsSettings(); + + [Put("/chatstats")] + Task UpdateChatStatsSettings(ChatStatistics stats); } } \ No newline at end of file diff --git a/src/StreamElements.Net/Rest/IGenericRestEndpoint.cs b/src/StreamElements.Net/Rest/IGenericRestEndpoint.cs index aa93d48..8de5608 100644 --- a/src/StreamElements.Net/Rest/IGenericRestEndpoint.cs +++ b/src/StreamElements.Net/Rest/IGenericRestEndpoint.cs @@ -3,19 +3,21 @@ using Refit; namespace StreamElements.Net.Rest { - public interface IGenericRestEndpoint where T : class + public interface IGenericRestEndpoint + where T : class + where TResult : class { [Get("")] - Task> GetAllAsync(); + Task> GetAllAsync(); [Get("/{key}")] - Task GetOneAsync(TKey key); + Task GetOneAsync(TKey key); [Post("")] - Task CreateAsync([Body] T paylod); + Task CreateAsync([Body] T paylod); [Put("/{key}")] - Task UpdateAsync(TKey key, [Body]T payload); + Task UpdateAsync(TKey key, [Body]T payload); [Delete("/{key}")] Task DeleteAsync(TKey key); diff --git a/src/StreamElements.Net/Rest/IStreamElementsClient.cs b/src/StreamElements.Net/Rest/IStreamElementsClient.cs new file mode 100644 index 0000000..92fa332 --- /dev/null +++ b/src/StreamElements.Net/Rest/IStreamElementsClient.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Refit; + +namespace StreamElements.Net.Rest +{ + public interface IStreamElementsApi + { + [Get("/chatstats/search/{channel}")] + Task> SearchChannels (string channel); + + [Get("/chatstats/stats/{channel}")] + Task GetChatStats(string channel); + } +} \ No newline at end of file diff --git a/src/StreamElements.Net/RestClient.cs b/src/StreamElements.Net/RestClient.cs index ff64c8b..8ded7cb 100644 --- a/src/StreamElements.Net/RestClient.cs +++ b/src/StreamElements.Net/RestClient.cs @@ -1,12 +1,37 @@ using System; +using System.Collections.Generic; using System.Net.Http; +using System.Threading.Tasks; using Refit; +using StreamElements.Net.Models; using StreamElements.Net.Rest; namespace StreamElements.Net { public class RestClient { + private IStreamElementsApi Client { get; } + public RestClient() + { + this.Client = BuildHttpClient(); + } + + public Task> SearchChannels(string channel) + { + if(string.IsNullOrWhiteSpace(channel)) + { + throw new ArgumentNullException(nameof(channel)); + } + return Client.SearchChannels(channel); + } + public Task GetChatStats(string channel) + { + if(string.IsNullOrWhiteSpace(channel)) + { + throw new ArgumentNullException(channel); + } + return Client.GetChatStats(channel); + } public virtual T BuildHttpClient(string pathSegment = null) { var builder = new UriBuilder("https://api.streamelements.com/kappa/v1");