Skip to content

Commit

Permalink
Big update
Browse files Browse the repository at this point in the history
- Chat bot rework,
- Logging overhaul,
- Beta version of browser based notificaiton view,
- A little bit of audio module rework,
  • Loading branch information
Abev08 committed Apr 19, 2024
1 parent 27d3ea7 commit 999c33f
Show file tree
Hide file tree
Showing 21 changed files with 1,919 additions and 823 deletions.
10 changes: 9 additions & 1 deletion AbevBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<UseWPF>true</UseWPF>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>

Expand All @@ -13,6 +13,8 @@

<ItemGroup>
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118" />
<PackageReference Include="SoundTouch.Net.NAudioSupport.Core" Version="2.3.2" />
</ItemGroup>
Expand All @@ -27,6 +29,12 @@
<None Include="Resources\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<!-- Include server files -->
<ItemGroup>
<EmbeddedResource Include="server\client.html" />
<EmbeddedResource Include="server\client.js" />
</ItemGroup>

<!-- Copy config and runtime files to output directory -->
<!-- <ItemGroup>
<None Include="Secrets.ini" Condition="Exists('Secrets.ini')" CopyToOutputDirectory="PreserveNewest" />
Expand Down
64 changes: 33 additions & 31 deletions AccessTokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net.Http.Headers;
using System.Text;

using Serilog;

namespace AbevBot;

/// <summary> Everything related to access tokens. </summary>
Expand Down Expand Up @@ -144,7 +146,7 @@ public static void UpdateTokens()
/// <summary> Request new access token. </summary>
private static void GetNewOAuthToken()
{
MainWindow.ConsoleWarning(">> Requesting new Twitch OAuth token.");
Log.Information("Requesting new Twitch OAuth token.");

string uri = string.Concat(
"https://id.twitch.tv/oauth2/authorize?",
Expand Down Expand Up @@ -196,7 +198,7 @@ private static void GetNewOAuthToken()
// Something went really wrong, we were requesting new token with fresh authentication and the response was corrupted
throw new Exception("Response was empty or didn't received access token!\nProbably ClientID or ClientPassowrd doesn't match!");
}
MainWindow.ConsoleWarning(response.ToString());
Log.Information(response.ToString());
// Read information from received data
Secret.Data[Secret.Keys.OAuthToken] = response.Token;
Secret.Data[Secret.Keys.OAuthRefreshToken] = response.RefreshToken;
Expand All @@ -218,19 +220,19 @@ private static bool ValidateOAuthToken()

string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { MainWindow.ConsoleWarning($">> Twitch OAuth token validation failed. {ex.Message}"); return false; }
catch (HttpRequestException ex) { Log.Error("Twitch OAuth token validation failed. {ex}", ex); return false; }
var response = AccessTokenValidationResponse.Deserialize(resp);
if (response?.ClientID?.Equals(Secret.Data[Secret.Keys.CustomerID]) == true && response?.ExpiresIn > 0)
{
if (response?.Scopes?.Length != TwitchScopes.Length) { MainWindow.ConsoleWarning(">> Current Twitch OAuth token is missing some scopes."); }
if (response?.Scopes?.Length != TwitchScopes.Length) { Log.Warning("Current Twitch OAuth token is missing some scopes."); }
else
{
BotOAuthTokenExpiration = DateTime.Now + TimeSpan.FromSeconds(response.ExpiresIn.Value) - OAuthTokenExpirationSomething;
MainWindow.ConsoleWarning($">> Twitch OAuth token validation succeeded. Token expiries in {response.ExpiresIn.Value / 3600f} hours.");
Log.Information("Twitch OAuth token validation succeeded. Token expiries in {time} hours.", MathF.Round(response.ExpiresIn.Value / 3600f, 2));
return true;
}
}
else { MainWindow.ConsoleWarning(">> Twitch OAuth token validation failed."); }
else { Log.Error("Twitch OAuth token validation failed."); }

return false;
}
Expand All @@ -241,7 +243,7 @@ public static bool RefreshAccessToken()
{
if (DateTime.Now < BotOAuthTokenExpiration) return false;

MainWindow.ConsoleWarning(">> Refreshing Twitch OAuth token.");
Log.Information("Refreshing Twitch OAuth token.");
using HttpRequestMessage request = new(HttpMethod.Post, "https://id.twitch.tv/oauth2/token");
request.Content = new StringContent(string.Concat(
"client_id=", Secret.Data[Secret.Keys.CustomerID],
Expand All @@ -253,14 +255,14 @@ public static bool RefreshAccessToken()

string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { MainWindow.ConsoleWarning($">> Twitch OAuth token refresh failed. {ex.Message}"); return false; }
catch (HttpRequestException ex) { Log.Error("Twitch OAuth token refresh failed. {ex}", ex); return false; }
var response = AccessTokenResponse.Deserialize(resp);
if (response is null || response.Token is null || response.RefreshToken is null)
{
MainWindow.ConsoleWarning(">> Twitch OAuth token refresh failed. Response was empty or didn't received access token!");
Log.Error("Twitch OAuth token refresh failed. Response was empty or didn't received access token!");
return false;
}
MainWindow.ConsoleWarning(response.ToString());
Log.Information(response.ToString());
// Read information from received data
Secret.Data[Secret.Keys.OAuthToken] = response.Token;
Secret.Data[Secret.Keys.OAuthRefreshToken] = response.RefreshToken;
Expand All @@ -272,7 +274,7 @@ public static bool RefreshAccessToken()
/// <summary> Requests new Spotify OAuth token using Authorization Code Flow. </summary>
private static void GetNewSpotifyOAuthToken()
{
MainWindow.ConsoleWarning(">> Requesting new Spotify OAuth token.");
Log.Information("Requesting new Spotify OAuth token.");

string uri = string.Concat(
"https://accounts.spotify.com/authorize?",
Expand Down Expand Up @@ -321,15 +323,15 @@ private static void GetNewSpotifyOAuthToken()

string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { MainWindow.ConsoleWarning($">> Spotify OAuth token request failed. {ex.Message}"); return; }
catch (HttpRequestException ex) { Log.Error("Spotify OAuth token request failed. {ex}", ex); return; }
SpotifyAccessTokenResponse response = SpotifyAccessTokenResponse.Deserialize(resp);
if (response is null || response.Token is null || response.RefreshToken is null)
{
MainWindow.ConsoleWarning(">> Spotify OAuth token request failed. Response was empty or didn't received access token!\nProbably ClientID or ClientPassowrd doesn't match!");
Log.Warning("Spotify OAuth token request failed. Response was empty or didn't received access token!\nProbably ClientID or ClientPassowrd doesn't match!");
}
else
{
MainWindow.ConsoleWarning(response.ToString());
Log.Information(response.ToString());
// Read information from received data
Secret.Data[Secret.Keys.SpotifyOAuthToken] = response.Token;
Secret.Data[Secret.Keys.SpotifyOAuthRefreshToken] = response.RefreshToken;
Expand All @@ -339,7 +341,7 @@ private static void GetNewSpotifyOAuthToken()
else
{
// Something went wrong
MainWindow.ConsoleWarning(">> Spotify OAuth token request failed. Something went wrong! Response url didn't include code part!");
Log.Warning("Spotify OAuth token request failed. Something went wrong! Response url didn't include code part!");
}
}

Expand All @@ -357,7 +359,7 @@ public static bool RefreshSpotifyAccessToken(bool forceRefresh = false)
if (!Spotify.Working && !forceRefresh) return false;
if (DateTime.Now < SpotifyOAuthTokenExpiration) return false;

MainWindow.ConsoleWarning(">> Refreshing Spotify access token.");
Log.Information("Refreshing Spotify access token.");
using HttpRequestMessage request = new(HttpMethod.Post, "https://accounts.spotify.com/api/token");
request.Content = new StringContent(string.Concat(
"grant_type=refresh_token",
Expand All @@ -368,16 +370,16 @@ public static bool RefreshSpotifyAccessToken(bool forceRefresh = false)

string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { MainWindow.ConsoleWarning($">> Spotify OAuth token refresh failed. {ex.Message}"); return false; }
catch (HttpRequestException ex) { Log.Error("Spotify OAuth token refresh failed. {ex}", ex); return false; }
SpotifyAccessTokenResponse response = SpotifyAccessTokenResponse.Deserialize(resp);
if (response is null || response.Token is null)
{
MainWindow.ConsoleWarning(">> Spotify OAuth token refresh failed. Response was empty or didn't received access token!\nProbably ClientID or ClientPassowrd doesn't match!");
Log.Warning("Spotify OAuth token refresh failed. Response was empty or didn't received access token!\nProbably ClientID or ClientPassowrd doesn't match!");
return false;
}
else
{
MainWindow.ConsoleWarning(response.ToString());
Log.Information(response.ToString());
// Read information from received data
Secret.Data[Secret.Keys.SpotifyOAuthToken] = response.Token;
// When refreshing refresh token stays the same??
Expand All @@ -390,7 +392,7 @@ public static bool RefreshSpotifyAccessToken(bool forceRefresh = false)
/// <summary> Request new access token. </summary>
private static void GetNewDiscordOAuthToken()
{
MainWindow.ConsoleWarning(">> Requesting new Discord OAuth token.");
Log.Information("Requesting new Discord OAuth token.");

string uri = string.Concat(
"https://discord.com/oauth2/authorize?",
Expand Down Expand Up @@ -440,11 +442,11 @@ private static void GetNewDiscordOAuthToken()

string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { MainWindow.ConsoleWarning($">> Discord OAuth token request failed. {ex.Message}"); return; }
catch (HttpRequestException ex) { Log.Error("Discord OAuth token request failed. {ex}", ex); return; }
var response = DiscordTokenResponse.Deserialize(resp);
if (response is null || response.Token is null || response.RefreshToken is null)
{
MainWindow.ConsoleWarning(">> Discord OAuth token request failed. Response was empty or didn't received access token!\nProbably ClientID or ClientPassowrd doesn't match!");
Log.Warning("Discord OAuth token request failed. Response was empty or didn't received access token!\nProbably ClientID or ClientPassowrd doesn't match!");
}
// Read information from received data
Secret.Data[Secret.Keys.DiscordOAuthToken] = response.Token;
Expand All @@ -454,7 +456,7 @@ private static void GetNewDiscordOAuthToken()
else
{
// Something went wrong
MainWindow.ConsoleWarning(">> Discord OAuth token request failed. Response url didn't include code part!");
Log.Warning("Discord OAuth token request failed. Response url didn't include code part!");
}
}

Expand All @@ -473,7 +475,7 @@ public static bool RefreshDiscordAccessToken(bool forceRefresh = false)
if (!Discord.Working && !forceRefresh) return false;
if (DiscordOAuthTokenExpiration == DateTime.MinValue || DateTime.Now < DiscordOAuthTokenExpiration) return false;

MainWindow.ConsoleWarning(">> Refreshing Discord OAuth token.");
Log.Information("Refreshing Discord OAuth token.");
using HttpRequestMessage request = new(HttpMethod.Post, "https://discord.com/api/v10/oauth2/token");
request.Content = new StringContent(string.Concat(
"client_id=", Secret.Data[Secret.Keys.DiscrodClientID],
Expand All @@ -485,19 +487,19 @@ public static bool RefreshDiscordAccessToken(bool forceRefresh = false)

string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { MainWindow.ConsoleWarning($">> Discord OAuth token refresh failed. {ex.Message}"); return false; }
catch (HttpRequestException ex) { Log.Error("Discord OAuth token refresh failed. {ex}", ex); return false; }
if (string.IsNullOrEmpty(resp) || resp.StartsWith("{\"error"))
{
MainWindow.ConsoleWarning($">> Discord OAuth token refresh failed. Response contained an error! Discord integration won't work! Message:\n{resp}");
Log.Warning("Discord OAuth token refresh failed. Response contained an error! Discord integration won't work! Message:\n{resp}", resp);
return false;
}
var response = DiscordTokenResponse.Deserialize(resp);
if (response is null || response.Token is null || response.RefreshToken is null)
{
MainWindow.ConsoleWarning(">> Discord OAuth token refresh failed. Response was empty or didn't received access token! Discord integration won't work!");
Log.Warning("Discord OAuth token refresh failed. Response was empty or didn't received access token! Discord integration won't work!");
return false;
}
MainWindow.ConsoleWarning(response.ToString());
Log.Information(response.ToString());
// Read information from received data
Secret.Data[Secret.Keys.DiscordOAuthToken] = response.Token;
Secret.Data[Secret.Keys.DiscordOAuthRefreshToken] = response.RefreshToken;
Expand All @@ -514,16 +516,16 @@ private static bool ValidateDiscordOAuthToken()
request.Headers.Add("Authorization", $"Bearer {Secret.Data[Secret.Keys.DiscordOAuthToken]}");
string resp;
try { resp = Client.Send(request).Content.ReadAsStringAsync().Result; }
catch (HttpRequestException ex) { MainWindow.ConsoleWarning($">> Discrod OAuth token validation failed. {ex.Message}"); return false; }
catch (HttpRequestException ex) { Log.Error("Discrod OAuth token validation failed. {ex}", ex); return false; }
var response = DiscordMeResponse.Deserialize(resp);
if (response?.ID?.Length > 0 && response?.UserName?.Length > 0)
{
MainWindow.ConsoleWarning($">> Discord OAuth token validation succeeded.");
Log.Information("Discord OAuth token validation succeeded.");
return true;
}
else
{
MainWindow.ConsoleWarning(">> Discrod OAuth token validation failed.");
Log.Warning("Discrod OAuth token validation failed.");
return false;
}
}
Expand Down
Loading

0 comments on commit 999c33f

Please sign in to comment.