Skip to content

Commit

Permalink
Added reply limiter and Twitch client
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila committed Jul 8, 2021
1 parent 04fe419 commit 360f9c9
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 19 deletions.
11 changes: 9 additions & 2 deletions Console/Config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ Ratelimit=10
;ignore comments older than this (in seconds)
MaxCommentAge=28800
;maximum replies per thread
CommentLimit=3
CommentLimit=1
;delay between replies (in seconds)
RateComment=86400

[UserAgent]
;app name and version sent as User-Agent (uses defaults when empty)
ApplicationName=
ApplicationVersion=
ApplicationVersion=

[Twitch]
;app credentials from https://dev.twitch.tv/console/apps
AppClientId=
AppClientSecret=
3 changes: 2 additions & 1 deletion Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static void Main()
Settings.ApplicationVersion,
Settings.Ratelimit,
Settings.MaxCommentAge,
Settings.CommentLimit);
Settings.CommentLimit,
Settings.RateComment);

var tokenSource = new CancellationTokenSource();
var task = client.RunAsync(tokenSource.Token);
Expand Down
19 changes: 19 additions & 0 deletions Console/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static Settings()
Ratelimit = TimeSpan.FromSeconds(int.Parse(iniData["Options"]["Ratelimit"]));
MaxCommentAge = TimeSpan.FromSeconds(int.Parse(iniData["Options"]["MaxCommentAge"]));
CommentLimit = int.Parse(iniData["Options"]["CommentLimit"]);
RateComment = TimeSpan.FromSeconds(int.Parse(iniData["Options"]["RateComment"]));

ApplicationName = iniData["UserAgent"]["ApplicationName"];
ApplicationVersion = iniData["UserAgent"]["ApplicationVersion"];
Expand Down Expand Up @@ -57,6 +58,8 @@ private static IList<string> GetFileContent(string value)

public static readonly int CommentLimit;

public static readonly TimeSpan RateComment;

public static readonly string ApplicationName;

public static readonly string ApplicationVersion;
Expand All @@ -68,5 +71,21 @@ private static IList<string> GetFileContent(string value)
public static readonly IList<string> TriggerPhrases;

public static readonly IList<string> Quotes;

public static class Twitch
{
static Twitch()
{
var iniParser = new FileIniDataParser();
IniData iniData = iniParser.ReadFile("Config.ini");

AppClientId = iniData["Twitch"]["AppClientId"];
AppClientSecret = iniData["Twitch"]["AppClientSecret"];
}

public static readonly string AppClientId;

public static readonly string AppClientSecret;
}
}
}
12 changes: 9 additions & 3 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
<EmbedAllSources>true</EmbedAllSources>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>RedditQuoteBot.snk</AssemblyOriginatorKeyFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<!-- NuGet -->
<PropertyGroup>
<PackageId>RedditQuoteBot</PackageId>
<Product>RedditQuoteBot</Product>
<Version>1.1.0</Version>
<VersionSuffix></VersionSuffix>
<Authors>David Sungaila</Authors>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -39,11 +41,15 @@
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>

<!-- Debug builds -->
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<VersionSuffix>debug</VersionSuffix>
</PropertyGroup>

<!-- Release builds -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<SignAssembly>true</SignAssembly>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<DocumentationFile>bin\Release\RedditQuoteBot.xml</DocumentationFile>
</PropertyGroup>

<!-- NuGet Icon -->
Expand Down
9 changes: 6 additions & 3 deletions Core/Models/AccessTokenResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RedditQuoteBot.Core.Models
{
[DebuggerDisplay("Token = {Token}, ExpiresAt = {ExpiresAt}")]
[DebuggerDisplay("Token = {Token}, ExpiresAtLocal = {ExpiresAtLocal}")]
internal class AccessTokenResponse
{
[JsonPropertyName("access_token")]
Expand All @@ -22,15 +22,18 @@ public long ExpiresIn
set
{
_expiresIn = value;
ExpiresAt = DateTime.UtcNow.AddSeconds(_expiresIn);
ExpiresAtUtc = DateTime.UtcNow.AddSeconds(_expiresIn);
}
}

[JsonPropertyName("scope")]
public string? Scope { get; set; }

[JsonIgnore]
public DateTime ExpiresAt { get; private set; } = DateTime.MaxValue;
public DateTime ExpiresAtUtc { get; private set; } = DateTime.MaxValue;

[JsonIgnore]
public DateTime ExpiresAtLocal { get => ExpiresAtUtc.ToLocalTime(); }

public override string? ToString() => Token;
}
Expand Down
43 changes: 43 additions & 0 deletions Core/Models/Twitch/AccessTokenResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Diagnostics;
using System.Text.Json.Serialization;

namespace RedditQuoteBot.Core.Models.Twitch
{
[DebuggerDisplay("Token = {Token}, ExpiresAtLocal = {ExpiresAtLocal}")]
internal class AccessTokenResponse
{
[JsonPropertyName("access_token")]
public string? Token { get; set; }

[JsonPropertyName("refresh_token")]
public string? RefreshToken { get; set; }

[JsonPropertyName("token_type")]
public string? Type { get; set; }

private long _expiresIn;

[JsonPropertyName("expires_in")]
public long ExpiresIn
{
get => _expiresIn;
set
{
_expiresIn = value;
ExpiresAtUtc = DateTime.UtcNow.AddSeconds(_expiresIn);
}
}

[JsonPropertyName("scope")]
public string? Scope { get; set; }

[JsonIgnore]
public DateTime ExpiresAtUtc { get; private set; } = DateTime.MaxValue;

[JsonIgnore]
public DateTime ExpiresAtLocal { get => ExpiresAtUtc.ToLocalTime(); }

public override string? ToString() => Token;
}
}
50 changes: 50 additions & 0 deletions Core/Models/Twitch/StreamData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization;

namespace RedditQuoteBot.Core.Models.Twitch
{
[DebuggerDisplay("UserLogin = {UserLogin}, Title = {Title}, ViewerCount = {ViewerCount}")]
internal class StreamData
{
[JsonPropertyName("id")]
public string? Id { get; set; }

[JsonPropertyName("user_id")]
public string? UserId { get; set; }

[JsonPropertyName("user_login")]
public string? UserLogin { get; set; }

[JsonPropertyName("game_id")]
public string? GameId { get; set; }

[JsonPropertyName("game_name")]
public string? GameName { get; set; }

[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("title")]
public string? Title { get; set; }

[JsonPropertyName("viewer_count")]
public int ViewerCount { get; set; }

[JsonPropertyName("started_at")]
public DateTime StartedAt { get; set; }

[JsonPropertyName("language")]
public string? Language { get; set; }

[JsonPropertyName("thumbnail_url")]
public string? ThumbnailUrl { get; set; }

[JsonPropertyName("is_mature")]
public bool IsMature { get; set; }

[JsonPropertyName("tag_ids")]
public IList<string?>? TagIds { get; set; }
}
}
11 changes: 11 additions & 0 deletions Core/Models/Twitch/StreamsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace RedditQuoteBot.Core.Models.Twitch
{
internal class StreamsResponse
{
[JsonPropertyName("data")]
public IList<StreamData>? Data { get; set; }
}
}
Loading

0 comments on commit 360f9c9

Please sign in to comment.