Skip to content

Instantly share code, notes, and snippets.

@itssimple
Last active January 20, 2021 06:43
Show Gist options
  • Save itssimple/d5afe8a21ddfe52772e07f8dc0b9d433 to your computer and use it in GitHub Desktop.
Save itssimple/d5afe8a21ddfe52772e07f8dc0b9d433 to your computer and use it in GitHub Desktop.
This is how you use Reflection to change TwitchLib-servers.
public class FDGTClient : WebSocketClient
{
public FDGTClient() :base()
{
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
var urlField = base.GetType().BaseType.GetFields(bindingFlags).FirstOrDefault(f => f.Name.Contains("Url"));
urlField.SetValue(this, "wss://irc.fdgt.dev:443");
}
}
void Main()
{
// Initialize the client as normal, but without any oauth
TwitchClient client = new TwitchClient(new FDGTClient());
client.Initialize(new ConnectionCredentials("fdgttest", "", "wss://irc.fdgt.dev:443", disableUsernameCheck: true), "fdgt");
var channel = string.Empty;
client.OnJoinedChannel += (sender, args) =>
{
channel = args.Channel;
};
client.OnLog += (sender, args) => {
Console.WriteLine(args.Data);
};
client.Connect();
var lastLine = string.Empty;
while(lastLine != "exit")
{
lastLine = Console.ReadLine();
client.SendMessage(channel, lastLine);
}
client.Disconnect();
}
/* Installed Nuget-packages are only TwitchLib */
using TwitchLib.Client;
using TwitchLib.Communication.Clients;
using TwitchLib.Communication.Models;
using TwitchLib.Client.Models;
using TwitchLib.Client.Enums;
using TwitchLib.Communication.Interfaces;
void Main()
{
// Initialize the client as normal, but without any oauth
TwitchClient client = new TwitchClient(protocol: ClientProtocol.WebSocket);
client.Initialize(new ConnectionCredentials("fdgttest", "", disableUsernameCheck: true), "fdgt");
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
// Fetch the private field _client from the TwitchClient instance and then grab the field that contains "Url", since it doesn't have a public setter
IClient _client = (IClient)client.GetType().GetField("_client", bindingFlags).GetValue(client);
var serverField = _client.GetType().GetFields(bindingFlags).FirstOrDefault(f => f.Name.Contains("Url"));
// Set the new value
serverField.SetValue(_client, "wss://irc.fdgt.dev:443");
var channel = string.Empty;
client.OnJoinedChannel += (sender, args) =>
{
channel = args.Channel;
};
client.OnLog += (sender, args) => {
Console.WriteLine(args.Data);
};
client.Connect();
var lastLine = string.Empty;
// Lets us input commands manually for testing of events, until we enter "exit"
while(lastLine != "exit")
{
lastLine = Console.ReadLine();
client.SendMessage(channel, lastLine);
}
client.Disconnect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment