Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1.         static void Connect(string name, string token, int amountOfClients, string strOfChannels)
  2.         {
  3.             List<string>[] channels = StringTo2DArray(strOfChannels, amountOfClients);
  4.             var credentials = new ConnectionCredentials(name, token);
  5.             TwitchClient[] clients = new TwitchClient[amountOfClients];
  6.  
  7.             for (int i = 0; i < amountOfClients; i++)
  8.             {
  9.                 int j = i;
  10.                 clients[i] = new TwitchClient(protocol: TwitchLib.Client.Enums.ClientProtocol.TCP);
  11.                 clients[i].OnMessageReceived += ShowAllMessages;
  12.                 clients[i].OnConnected += (sender, e) => JoinChannels(sender, e, channels[j]);
  13.                 clients[i].Initialize(credentials);
  14.                 clients[i].Connect();
  15.             }
  16.         }
  17.  
  18.         private static List<string>[] StringTo2DArray(string str, int num)
  19.         {
  20.             List<string>[] res = new List<string>[num];
  21.             for (int k = 0; k < res.Length; k++)
  22.             {
  23.                 res[k] = new List<string>();
  24.             }
  25.             var splitted = str.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x));
  26.             int i = 0;
  27.             foreach (var item in splitted)
  28.             {
  29.                 res[i].Add(item);
  30.                 if (i == num - 1)
  31.                 {
  32.                     i = 0;
  33.                 }
  34.                 else
  35.                 {
  36.                     i++;
  37.                 }
  38.             }
  39.             return res;
  40.         }
  41.  
  42.         private static void JoinChannels(object sender, OnConnectedArgs e, List<string> channels)
  43.         {
  44.             foreach (var item in channels)
  45.             {
  46.                 ((TwitchClient)sender).JoinChannel(item);
  47.             }
  48.         }
  49.  
  50.         private static void ShowAllMessages(object sender, OnMessageReceivedArgs e)
  51.         {
  52.             Console.WriteLine($"{DateTime.Now} ({e.ChatMessage.Channel})/ {e.ChatMessage.Username}: {e.ChatMessage.Message}");
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement