Added Twitch Client
This commit is contained in:
parent
63456de2f7
commit
deda300eb2
45
App.axaml.cs
45
App.axaml.cs
@ -6,13 +6,20 @@ using Avalonia.Data.Core.Plugins;
|
|||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using BetterRaid.ViewModels;
|
using BetterRaid.ViewModels;
|
||||||
using BetterRaid.Views;
|
using BetterRaid.Views;
|
||||||
|
using TwitchLib.Client;
|
||||||
|
using TwitchLib.Client.Events;
|
||||||
|
using TwitchLib.Client.Models;
|
||||||
|
using TwitchLib.Communication.Clients;
|
||||||
|
using TwitchLib.Communication.Models;
|
||||||
|
|
||||||
namespace BetterRaid;
|
namespace BetterRaid;
|
||||||
|
|
||||||
public partial class App : Application
|
public partial class App : Application
|
||||||
{
|
{
|
||||||
|
public static string TwitchChannelName = "";
|
||||||
public static string TokenClientId = "";
|
public static string TokenClientId = "";
|
||||||
public static string TokenClientSecret = "";
|
public static string TokenClientSecret = "";
|
||||||
|
public static string TokenClientAccess = "";
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
@ -22,8 +29,10 @@ public partial class App : Application
|
|||||||
var profilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
var profilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||||
var tokenFilePath = Path.Combine(profilePath, tokenFile);
|
var tokenFilePath = Path.Combine(profilePath, tokenFile);
|
||||||
var tokenFileLines = File.ReadAllLines(tokenFilePath);
|
var tokenFileLines = File.ReadAllLines(tokenFilePath);
|
||||||
TokenClientId = tokenFileLines[0].Split('=')[1];
|
TwitchChannelName = tokenFileLines[0].Split('=')[1];
|
||||||
TokenClientSecret = tokenFileLines[1].Split('=')[1];
|
TokenClientId = tokenFileLines[1].Split('=')[1];
|
||||||
|
TokenClientSecret = tokenFileLines[2].Split('=')[1];
|
||||||
|
TokenClientAccess = tokenFileLines[3].Split('=')[1];
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
@ -31,9 +40,41 @@ public partial class App : Application
|
|||||||
Environment.Exit(1);
|
Environment.Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var creds = new ConnectionCredentials(TwitchChannelName, TokenClientAccess);
|
||||||
|
var clientOptions = new ClientOptions
|
||||||
|
{
|
||||||
|
MessagesAllowedInPeriod = 750,
|
||||||
|
ThrottlingPeriod = TimeSpan.FromSeconds(30)
|
||||||
|
};
|
||||||
|
|
||||||
|
var customClient = new WebSocketClient(clientOptions);
|
||||||
|
var client = new TwitchClient(customClient);
|
||||||
|
|
||||||
|
client.Initialize(creds, TwitchChannelName);
|
||||||
|
client.OnMessageReceived += OnMessageReceived;
|
||||||
|
client.OnConnected += OnConnected;
|
||||||
|
client.OnConnectionError += OnConnectionError;
|
||||||
|
|
||||||
|
client.Connect();
|
||||||
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
AvaloniaXamlLoader.Load(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnConnectionError(object? sender, OnConnectionErrorArgs e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("[ERROR] Twitch Client failed to connect!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnConnected(object? sender, OnConnectedArgs e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("[INFO] Twitch Client connected!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnMessageReceived(object? sender, OnMessageReceivedArgs e)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{e.ChatMessage.DisplayName}: {e.ChatMessage.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
public override void OnFrameworkInitializationCompleted()
|
public override void OnFrameworkInitializationCompleted()
|
||||||
{
|
{
|
||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||||
|
@ -21,5 +21,6 @@
|
|||||||
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
|
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
|
||||||
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" />
|
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" />
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
|
||||||
|
<PackageReference Include="TwitchLib" Version="3.5.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
Reference in New Issue
Block a user