Connect to Twitch chat with multiple clients

Started rewriting BlushyFace.Twitch.Chat into BlushyFace.Twitch.Chat.V2 because while v1 is a rewrite ported from my .NET FX version I wasn’t totally happy with it and decided to just rewrite it in it’s entirely. This is a work in progress but the basic client is fully functional. The client features are based on what I need so if you have suggestions what you would like me to add let me know with a comment below or join our discord.

The example shows how to create multiple clients, client is a user that is logged in to chat and able to receive & send chat messages while client2 is anonymous and can only receive chat messages. Requires .NET Core 3.x to run.

Features
– connect to twitch chat
– support for various capabilities
– send & receive messages
– reconnect on disconnect
– subscribe to various events
– lightweight

 

Connected clients can be identified by the login name.
In this instance there are 2 clients, 1 named blushyface and one named justinfan12345 both in the channel #blushyfacebot

// edit, seems the used WP plugin to highlight code eats greater / lower characters so the examples might not work properly.

For example -> List<string> Channels = new List<string>(); becomes List Channels = new List(); so change where needed until I have a better plugin that does not do that (?).

 

Client example

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using BlushyFace.Common;
using BlushyFace.Examples.Events;
using BlushyFace.Twitch.Chat.V2.Chat;
using BlushyFace.Twitch.Chat.V2.Chat.ResponseObjects;


namespace BlushyFace.Examples
{
    public static class ChatV2
    {
        private static List Clients = new List();
        private static List ChannelsToJoin = new List();

        public static async void Run()
        {
            ChannelsToJoin = new List
                             {
                                 "blushyface",
                                 "blushyfacebot",
                             };

            var client = new Client(userName: "blushyface", token: "oauth:...")
            {
                // register capabilities to receive additional messages
                RegisterCapRequestCommands = true,
                RegisterCapRequestTags = true,
                RegisterCapRequestMembership = true
                // ReceiveRawMessageOnly = true // outputs the raw incoming Twitch chat messages
            };

            // create another client
            var client2 = new Client()
            {
                RegisterCapRequestCommands = true,
                RegisterCapRequestTags = true,
                RegisterCapRequestMembership = true,
            };

            Clients.Add(client);
            Clients.Add(client2);

            // prepare the clients
            foreach (var twitchClient in Clients)
            {
                // subscribe clients to events.
                twitchClient.GlobalUserStateEventHandler += (sender, e) => ChatV2Events.Client_OnGlobalUserStateEventHandler(twitchClient, e);
                twitchClient.NumericEventHandler += (sender, e) => ChatV2Events.Client_OnNumericEventHandler(twitchClient, e);
                twitchClient.CapEventHandler += (sender, e) => ChatV2Events.Client_OnCapEventHandler(twitchClient, e);
                twitchClient.JoinEventHandler += (sender, e) => ChatV2Events.Client_OnJoinEventHandler(twitchClient, e);
                twitchClient.PartEventHandler += (sender, e) => ChatV2Events.Client_OnPartEventHandler(twitchClient, e);
                twitchClient.PrivMsgEventHandler += (sender, e) => ChatV2Events.Client_OnPrivMsg(twitchClient, e);
                twitchClient.WhisperEventHandler += (sender, e) => ChatV2Events.Client_OnWhisperEventHandler(twitchClient, e);
                twitchClient.ClearChatEventHandler += (sender, e) => ChatV2Events.Client_OnClearChatEventHandler(twitchClient, e);
                twitchClient.ClearMsgEventHandler += (sender, e) => ChatV2Events.Client_OnClearMsgEventHandler(twitchClient, e);
                twitchClient.UserNoticeEventHandler += (sender, e) => ChatV2Events.Client_OnUserNoticeEventHandler(twitchClient, e);
                twitchClient.NoticeEventHandler += (sender, e) => ChatV2Events.Client_OnNoticeEventHandler(twitchClient, e);
                twitchClient.HostTargetEventHandler += (sender, e) => ChatV2Events.Client_OnHostTargetEventHandler(twitchClient, e);
                twitchClient.RoomStateEventHandler += (sender, e) => ChatV2Events.Client_OnRoomStateEventHandler(twitchClient, e);
                twitchClient.UserStateEventHandler += (sender, e) => ChatV2Events.Client_OnUserStateEventHandler(twitchClient, e);
            }

            // connect clients manually
            var isClientConnected = await client.Connect();

            // join channels after connecting to Twitch chat
            if (isClientConnected)
                await JoinChannels();

            // sends a message to it's own channel
            await client.SendMessage(client.LoginName, $"Hi i'm connected with client {client.LoginName}!");

            //var isClient2Connected = await client2.Connect();
            //if (isClient2Connected)
            //    await JoinChannels();

        }

        public static async Task JoinChannels()
        {
            foreach (var twitchClient in Clients)
            {
                foreach (var channel in ChannelsToJoin)
                {
                    await twitchClient.Join(channel);
                }
            }
        }
    }
}


EventHandlers example

using System;
using System.Collections.Generic;
using System.Text;
using BlushyFace.Common;
using BlushyFace.Twitch.Chat.V2.Chat;
using BlushyFace.Twitch.Chat.V2.Chat.ResponseObjects;

namespace BlushyFace.Examples.Events
{
    internal static class ChatV2Events
    {
        internal static void Client_OnGlobalUserStateEventHandler(Client client, GlobalUserState e)
        {            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            Console.WriteLine($"> [GLOBALUSERSTATE] {message}");
        }

        internal static void Client_OnUserStateEventHandler(Client client, UserState e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            Console.WriteLine($"> [USERSTATE] {message}");
        }

        internal static async void Client_OnHostTargetEventHandler(Client client, HostTarget e)
        {
            if (e.IsHosting)
                await client.Join(e.Target);

            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Host) ? $"[{e.Host}] " : "";
            message += $"Is hosting: {e.IsHosting.BoolToYesNo()} ";
            message += !string.IsNullOrEmpty(e.Target) ? $"[{e.Target}] " : "";
            message += !string.IsNullOrEmpty(e.Viewers) ? $"[{e.Viewers}] " : "";
            Console.WriteLine($"> [HOSTTARGET]{message}");
        }

        internal static void Client_OnNoticeEventHandler(Client client, Notice e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            message += !string.IsNullOrEmpty(e.Message) ? $"[{e.Message}] " : "";
            Console.WriteLine($"> [NOTICE] {message}");
        }

        internal static void Client_OnCapEventHandler(Client client, Cap e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Message) ? $"[{e.Message}] " : "";
            Console.WriteLine($"> [CAP] {message}");
        }

        internal static void Client_OnRoomStateEventHandler(Client client, RoomState e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Message) ? $"[{e.Message}] " : "";
            Console.WriteLine($"> [ROOMSTATE] {message}");
        }

        internal static void Client_OnUserNoticeEventHandler(Client client, UserNotice e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            message += !string.IsNullOrEmpty(e.SystemMessage) ? $"[{e.SystemMessage}] " : "";
            Console.WriteLine($"> [USERNOTICE] {message}");
        }

        internal static void Client_OnNumericEventHandler(Client client, Numeric e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Message) ? $"[{e.Message}] " : "";
            Console.WriteLine($"> [NUMERIC] {message}");
        }

        internal static void Client_OnClearMsgEventHandler(Client client, ClearMsg e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            message += !string.IsNullOrEmpty(e.UserName) ? $"[{e.UserName}] " : "";
            message += !string.IsNullOrEmpty(e.UserMessage) ? $"[{e.UserMessage}] " : "";
            Console.WriteLine($"> [CLEARMSG] {message}");
        }

        internal static void Client_OnClearChatEventHandler(Client client, ClearChat e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            message += !string.IsNullOrEmpty(e.UserName) ? $"[{e.UserName}] " : "";
            Console.WriteLine($"> [CLEARCHAT] {message}");
        }

        internal static void Client_OnJoinEventHandler(Client client, Join e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            message += !string.IsNullOrEmpty(e.UserName) ? $"[{e.UserName}] " : "";
            Console.WriteLine($"> [JOIN] {message}");
        }

        internal static void Client_OnPartEventHandler(Client client, Part e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            message += !string.IsNullOrEmpty(e.UserName) ? $"[{e.UserName}] " : "";
            Console.WriteLine($"> [PART] {message}");
        }


        internal static void Client_OnPrivMsg(Client client, PrivMsg e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.Channel) ? $"[{e.Channel}] " : "";
            message += !string.IsNullOrEmpty(e.Tags.GetTag("badges")) ? $"[{e.Tags.GetTag("badges")}] " : "";
            message += !string.IsNullOrEmpty(e.UserName) ? $"[{e.UserName}] " : "";
            message += !string.IsNullOrEmpty(e.UserMessage) ? $"[{e.UserMessage}] " : "";

            Console.WriteLine($"> [PRIVMSG] {message}");
        }

        internal static async void Client_OnWhisperEventHandler(Client client, Whisper e)
        {
            var message = string.Empty;
            message += !string.IsNullOrEmpty(client.LoginName) ? $"[{client.LoginName}] " : "";
            message += !string.IsNullOrEmpty(e.From) ? $"[{e.From}] " : "";
            message += !string.IsNullOrEmpty(e.To) ? $"[{e.To}] " : "";
            message += !string.IsNullOrEmpty(e.Message) ? $"[{e.Message}] " : "";

            await client.SendWhisper(e.From, $"Did you whisper: {e.Message} ?");
            Console.WriteLine($"> [WHISPER] {message}");
        }
    }
}

Herrow, I'm BlushyFace and here you can read about the (silly) adventures of Blushies, community projects and live stream highlights!
Consider a tip if you enjoy my content, thank you for the support!

Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Site Footer

LIVE NOW! CLICK TO VIEW.
CURRENTLY OFFLINE
@Blushyface | All Rights Reserved - Blushyface.com