moah commands

This commit is contained in:
Tommy Parnell
2017-09-24 11:12:58 -04:00
parent cc452af65d
commit 59e3357be8
13 changed files with 415 additions and 25 deletions

View File

@@ -4,6 +4,7 @@ using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Refit; using Refit;
using StreamElements.Net.Models; using StreamElements.Net.Models;
using StreamElements.Net.Models.Results;
using StreamElements.Net.Rest; using StreamElements.Net.Rest;
namespace StreamElements.Net namespace StreamElements.Net
@@ -12,8 +13,10 @@ namespace StreamElements.Net
public class AuthRestClient : RestClient public class AuthRestClient : RestClient
{ {
private string JwtToken { get; } private string JwtToken { get; }
private IAuthenticatedStreamElementsApi Client { get; } private IAuthenticatedStreamElementsApi AuthClient { get; }
private IGenericRestEndpoint<BotCommand,string> BotCommandClient { get; } private IGenericRestEndpoint<BotCommand, BotCommandResult, string> BotCommandClient { get; }
private IGenericRestEndpoint<BotTimer, BotTimerResult, string> BotTimerClient { get; }
public AuthRestClient(string jwtToken) public AuthRestClient(string jwtToken)
{ {
this.JwtToken = jwtToken; this.JwtToken = jwtToken;
@@ -21,14 +24,15 @@ namespace StreamElements.Net
{ {
throw new ArgumentNullException(nameof(jwtToken)); throw new ArgumentNullException(nameof(jwtToken));
} }
this.Client = BuildHttpClient<IAuthenticatedStreamElementsApi>(); this.AuthClient = BuildHttpClient<IAuthenticatedStreamElementsApi>();
BotCommandClient = BuildHttpClient<IGenericRestEndpoint<BotCommand, string>>("bot/commands"); BotCommandClient = BuildHttpClient<IGenericRestEndpoint<BotCommand, BotCommandResult, string>>("bot/commands");
BotTimerClient = BuildHttpClient<IGenericRestEndpoint<BotTimer, BotTimerResult, string>>("bot/timers");
} }
/// <summary> /// <summary>
/// Returns a list of activities sorted by createdAt. /// Returns a list of activities sorted by createdAt.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Task<IList<Activity>> GetActivities() => Client.GetActivitiesAsync(); public Task<IList<Activity>> GetActivities() => AuthClient.GetActivitiesAsync();
/// <summary> /// <summary>
/// Returns a single activity /// Returns a single activity
@@ -41,18 +45,18 @@ namespace StreamElements.Net
{ {
throw new ArgumentNullException(nameof(id)); throw new ArgumentNullException(nameof(id));
} }
return Client.GetActivityAsync(id); return AuthClient.GetActivityAsync(id);
} }
/// <summary> /// <summary>
/// Returns an object with information about the bot. /// Returns an object with information about the bot.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Task<BotObj> GetBotAsync() => Client.GetBotAsync(); public Task<BotObj> GetBotAsync() => AuthClient.GetBotAsync();
/// <summary> /// <summary>
/// Returns the bots action log. These logs include command updates. /// Returns the bots action log. These logs include command updates.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Task<List<BotLog>> GetBotLogsAsync() => Client.GetBotLogsAsync(); public Task<List<BotLog>> GetBotLogsAsync() => AuthClient.GetBotLogsAsync();
/// <summary> /// <summary>
/// Make the bot do something /// Make the bot do something
/// </summary> /// </summary>
@@ -61,7 +65,7 @@ namespace StreamElements.Net
public Task<Result> PostBotActionAsync(BotActionEnum action) public Task<Result> PostBotActionAsync(BotActionEnum action)
{ {
var actionParsed = Enum.GetName(typeof(BotActionEnum), action); var actionParsed = Enum.GetName(typeof(BotActionEnum), action);
return this.Client.PostBotActionAsync(actionParsed); return this.AuthClient.PostBotActionAsync(actionParsed);
} }
/// <summary> /// <summary>
/// Makes the bot send a message in current user's the channel. /// Makes the bot send a message in current user's the channel.
@@ -74,13 +78,13 @@ namespace StreamElements.Net
{ {
throw new ArgumentNullException(nameof(message)); throw new ArgumentNullException(nameof(message));
} }
return Client.PostBotSayAsync(new{ message } ); return AuthClient.PostBotSayAsync(new{ message } );
} }
/// <summary> /// <summary>
/// Returns an array of users with levels in your channel. /// Returns an array of users with levels in your channel.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Task<BotLevels> GetBotLevels() => Client.GetBotLevelsAsync(); public Task<BotLevels> GetBotLevels() => AuthClient.GetBotLevelsAsync();
/// <summary> /// <summary>
/// Create a new permission for the current channel. /// Create a new permission for the current channel.
/// </summary> /// </summary>
@@ -91,35 +95,104 @@ namespace StreamElements.Net
{ {
throw new ArgumentNullException(nameof(userName)); throw new ArgumentNullException(nameof(userName));
} }
return Client.PostBotLevelAsync(new { username = userName, level = botEnum}); return AuthClient.PostBotLevelAsync(new { username = userName, level = botEnum});
} }
public Task<List<BotCommand>> GetBotCommandsAsync() => BotCommandClient.GetAllAsync(); public Task<List<BotCommandResult>> GetBotCommandsAsync() => BotCommandClient.GetAllAsync();
public Task<BotCommand> GetBotCommandAsync(string id) public Task<BotCommandResult> GetBotCommandAsync(string id)
{ {
if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id)); if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id));
return BotCommandClient.GetOneAsync(id); return BotCommandClient.GetOneAsync(id);
} }
public Task<BotCommand> PostBotCommand(BotCommand command) public Task<BotCommandResult> PostBotCommand(BotCommand command)
{ {
if(command == null) throw new ArgumentNullException(nameof(command)); if(command == null) throw new ArgumentNullException(nameof(command));
return BotCommandClient.CreateAsync(command); return BotCommandClient.CreateAsync(command);
} }
public Task<BotCommand> PutBotCommand (string id, BotCommand command) public Task<BotCommandResult> PutBotCommand (string id, BotCommand command)
{ {
if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id)); if(string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id));
if(command == null) throw new ArgumentNullException(nameof(command)); if(command == null) throw new ArgumentNullException(nameof(command));
return BotCommandClient.UpdateAsync(id, 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); return BotCommandClient.DeleteAsync(id);
} }
/// <summary>
/// Returns an array of timers
/// </summary>
/// <returns></returns>
public Task<List<BotTimerResult>> GetBotTimers() => this.BotTimerClient.GetAllAsync();
/// <summary>
/// Returns a single timer
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Task<BotTimerResult> GetBotTimer(string id)
{
if(string.IsNullOrWhiteSpace(id))
{
throw new ArgumentNullException(nameof(id));
}
return this.BotTimerClient.GetOneAsync(id);
}
/// <summary>
/// Update a single timer
/// </summary>
/// <returns></returns>
public Task<BotTimerResult> 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<BotTimerResult> 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<ChatStatsSettingsResult> GetChatStats => this.AuthClient.GetChatStatsSettings();
public Task<ChatStatsSettingsResult> UpdateChatStats(ChatStatistics statsistics)
{
if(statsistics == null)
{
throw new ArgumentNullException(nameof(statsistics));
}
return this.AuthClient.UpdateChatStatsSettings(statsistics);
}
public override T BuildHttpClient<T>(string pathSegment = null) public override T BuildHttpClient<T>(string pathSegment = null)
{ {
var builder = new UriBuilder("https://api.streamelements.com/kappa/v1"); var builder = new UriBuilder("https://api.streamelements.com/kappa/v1");

View File

@@ -21,7 +21,7 @@ namespace StreamElements.Net.Models
public bool Enabled { get; set; } public bool Enabled { get; set; }
} }
public class Stats public class BotStats
{ {
[JsonProperty("commands")] [JsonProperty("commands")]
@@ -41,7 +41,7 @@ namespace StreamElements.Net.Models
public Bot Bot { get; set; } public Bot Bot { get; set; }
[JsonProperty("stats")] [JsonProperty("stats")]
public Stats Stats { get; set; } public BotStats Stats { get; set; }
} }
} }

View File

@@ -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; }
}
}

View File

@@ -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<Chatter> 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> Twitch { get; set; }
[JsonProperty("bttv")]
public IList<Bttv> 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<Command> Commands { get; set; }
[JsonProperty("hashtags")]
public IList<object> Hashtags { get; set; }
}
}

View File

@@ -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; }
}
}

View File

@@ -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; }
}
}

View File

@@ -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; }
}
}

View File

@@ -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; }
}
}

View File

@@ -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; }
}
}

View File

@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Refit; using Refit;
using StreamElements.Net.Models; using StreamElements.Net.Models;
using StreamElements.Net.Models.Results;
namespace StreamElements.Net.Rest namespace StreamElements.Net.Rest
{ {
@@ -30,5 +31,11 @@ namespace StreamElements.Net.Rest
[Post("/bot/levels")] [Post("/bot/levels")]
Task<SubmitBotLevelResponse> PostBotLevelAsync([Body] object submit); Task<SubmitBotLevelResponse> PostBotLevelAsync([Body] object submit);
[Get("/chatstats")]
Task<ChatStatsSettingsResult> GetChatStatsSettings();
[Put("/chatstats")]
Task<ChatStatsSettingsResult> UpdateChatStatsSettings(ChatStatistics stats);
} }
} }

View File

@@ -3,19 +3,21 @@ using Refit;
namespace StreamElements.Net.Rest namespace StreamElements.Net.Rest
{ {
public interface IGenericRestEndpoint<T, in TKey> where T : class public interface IGenericRestEndpoint<T, TResult, in TKey>
where T : class
where TResult : class
{ {
[Get("")] [Get("")]
Task<System.Collections.Generic.List<T>> GetAllAsync(); Task<System.Collections.Generic.List<TResult>> GetAllAsync();
[Get("/{key}")] [Get("/{key}")]
Task<T> GetOneAsync(TKey key); Task<TResult> GetOneAsync(TKey key);
[Post("")] [Post("")]
Task<T> CreateAsync([Body] T paylod); Task<TResult> CreateAsync([Body] T paylod);
[Put("/{key}")] [Put("/{key}")]
Task<T> UpdateAsync(TKey key, [Body]T payload); Task<TResult> UpdateAsync(TKey key, [Body]T payload);
[Delete("/{key}")] [Delete("/{key}")]
Task DeleteAsync(TKey key); Task DeleteAsync(TKey key);

View File

@@ -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<List<string>> SearchChannels (string channel);
[Get("/chatstats/stats/{channel}")]
Task<Models.ChatStats> GetChatStats(string channel);
}
}

View File

@@ -1,12 +1,37 @@
using System; using System;
using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
using Refit; using Refit;
using StreamElements.Net.Models;
using StreamElements.Net.Rest; using StreamElements.Net.Rest;
namespace StreamElements.Net namespace StreamElements.Net
{ {
public class RestClient public class RestClient
{ {
private IStreamElementsApi Client { get; }
public RestClient()
{
this.Client = BuildHttpClient<IStreamElementsApi>();
}
public Task<List<string>> SearchChannels(string channel)
{
if(string.IsNullOrWhiteSpace(channel))
{
throw new ArgumentNullException(nameof(channel));
}
return Client.SearchChannels(channel);
}
public Task<ChatStats> GetChatStats(string channel)
{
if(string.IsNullOrWhiteSpace(channel))
{
throw new ArgumentNullException(channel);
}
return Client.GetChatStats(channel);
}
public virtual T BuildHttpClient<T>(string pathSegment = null) public virtual T BuildHttpClient<T>(string pathSegment = null)
{ {
var builder = new UriBuilder("https://api.streamelements.com/kappa/v1"); var builder = new UriBuilder("https://api.streamelements.com/kappa/v1");