0

First of all - I know this question has been asked. I hardly know C# still learning, a lot of this code is from a tutorial, so I was hoping if I could have a more of direct answer to my actual code. Im making a twitch bot.

 private void ViewListUpdate()
    {
        ViewerBox.Items.Clear();
        Chatters AllChatters = ChatClient.GetChatters("name");
        chatBox.Text += "Checking the viewer list...";

        foreach (string admin in AllChatters.Admins)
        {
            ViewerBox.Items.Add(admin + Environment.NewLine);
        }

        foreach (string staff in AllChatters.Staff)
        {
            ViewerBox.Items.Add(staff + Environment.NewLine);
        }

        foreach (string globalmod in AllChatters.GlobalMods)
        {
            ViewerBox.Items.Add(globalmod + Environment.NewLine);
        }

        foreach (string moderator in AllChatters.Moderators)
        {
            ViewerBox.Items.Add(moderator + Environment.NewLine);
        }

        foreach (string viewers in AllChatters.Viewers)
        {
            ViewerBox.Items.Add(viewers + Environment.NewLine);
        }
    }

The line that is getting the error (System.IndexOutOfRangeException: 'Index was outside the bounds of the array.') is the following:

Chatters AllChatters = ChatClient.GetChatters("name");

Any help would be great, thanks.

byLimbo
  • 1
  • 1
  • 4
  • 4
    Show the implementation of `GetChatters` – nbokmans May 22 '17 at 08:43
  • @nbokmans Its saying it has no implementations. Im using: `using TwitchCSharp.Clients; using TwitchCSharp.Models;` When I hover over the "GetChatters" This line of code shows `Chatters TwitchROChat.GetChatters(string channel, [TwitchCSharp.Helpers.PagingInfo pagingInfo = null])` I am using a file thats TwitchCSharp.dll that I have added to my resources. – byLimbo May 22 '17 at 09:00
  • Where did you get the dll from? – nbokmans May 22 '17 at 09:07
  • @nbokmans Through this tutorial https://www.youtube.com/watch?v=bd87CS-Q7V4 – byLimbo May 22 '17 at 09:09
  • as I cant watch the youtube.. is "name" not supposed to be the channel name? is "name" really the channel name? – BugFinder May 22 '17 at 09:31
  • It looks like the dll you are using is an extension of this https://github.com/michidk/TwitchCSharp library. Sadly the original library does not include the code to get all users from a channel. However, decompiling the dll shows that it uses this Twitch API endpoint: https://tmi.twitch.tv/group/user/{channel}/chatters to fetch the information you need. You could write your own program to parse data from here. – nbokmans May 22 '17 at 09:33
  • @BugFinder I just put name there. Thats not what I actually run the bot as. – byLimbo May 22 '17 at 11:07
  • @nbokmans Any examples on how this could be done? Im very new to C# so not quite sure how to go about it. – byLimbo May 22 '17 at 11:08

1 Answers1

0

Compiled DLL

I have generated a compiled DLL for you here which you can download and add to your project. You can find this here: https://dropfile.to/9hzvwVX (updated)

Now you can fetch users for a channel like so:

var dataClient = new TwitchTmiClient();
var chatters = dataClient.GetChannelViewers("someTwitchChannelName");

Chatters will now contain a list of users in the active channel, separated by ranks (admin, mod, viewer etc.)

Explanation

Because this question is relevant to my personal interests I decided to add the feature you're looking for to the library I posted in the comments: https://github.com/michidk/TwitchCSharp

Of course, downloading files is always kind of sketchy. So what I did was add a new Twitch client implementation, because chatter data is not stored on the Twitch Kraken API but on the old, https://tmi.twitch.tv API.

namespace TwitchCSharp.Clients
{
    public class TwitchTmiClient : ITwitchClient
    {
        public readonly RestClient restClient;

        public TwitchTmiClient(string url = TwitchHelper.TwitchTmiUrl)
        {
            restClient = new RestClient(url);
            restClient.AddHandler("application/json", new DynamicJsonDeserializer());
            restClient.AddHandler("text/html", new DynamicJsonDeserializer());
            restClient.AddDefaultHeader("Accept", TwitchHelper.twitchAcceptHeader);
        }
        public ViewerList GetChannelViewers(string channel)
        {
            var request = new RestRequest("group/user/{channel}/chatters");
            request.AddUrlSegment("channel", channel.ToLower());
            return restClient.Execute<ViewerList>(request).Data;
        }

        public RestRequest GetRequest(string url, Method method)
        {
            return new RestRequest(url, method);
        }

    }
}

This new Twitch client uses two models to deserialize json into:

namespace TwitchCSharp.Models
{
    public class ViewerList
    {
        [JsonProperty("_links")]
        public Dictionary<string, string> Links;
        [JsonProperty("chatter_count")]
        public int ChatterCount;
        [JsonProperty("chatters")]
        public Chatter Chatters;
    }
}

...

namespace TwitchCSharp.Models
{
    public class Chatter
    {
        [JsonProperty("moderators")] public string[] Moderators;
        [JsonProperty("staff")] public string[] Staff;
        [JsonProperty("admins")] public string[] Admins;
        [JsonProperty("global_mods")] public string[] GlobalMods;
        [JsonProperty("viewers")] public string[] Viewers;
    }
}

A repository where you can see all the changes can be found here: https://github.com/nbokmans/TwitchCSharp/commit/ec38eecf1d0fbcb0b75c5de597a44582a61deb3d

You can git clone above repository and open it in Visual Studio to build it yourself, if you want to.

nbokmans
  • 5,492
  • 4
  • 35
  • 59
  • is there a place for me to contact you privately? – byLimbo May 22 '17 at 12:33
  • Sadly since you do not have 20 reputation yet we cannot use the chat function. What do you need? – nbokmans May 22 '17 at 12:47
  • As I said, Im really new into C#. I dont know how to implement your dll(Thanks a lot for by the way) to work with my current code. Ive been copying the tutorial but I know enough to edit parts of the code and I just didn't want to continue to spam this page. – byLimbo May 22 '17 at 12:56
  • Just add it the same was as you added the one from the video. There's no difference (although some parts of the code from the video may not be compatible with this DLL). – nbokmans May 22 '17 at 13:05
  • not talking about adding it to the reference, im more talking about what code i need to add `var dataClient = new TwitchDataClient(); var chatters = dataClient.GetChannelViewers("someTwitchChannelName");` Do I just add that and im good to go or do i need to add the other code you've added into the answer? – byLimbo May 22 '17 at 13:09
  • If you add the DLL to the project then that's all you need, the other part was just explaining how I implemented it, but that code is hidden in the DLL – nbokmans May 22 '17 at 13:11
  • Didn't work. Not sure if I did it right? all the "AllChatters. in the foreach are underlined in red. https://pastebin.com/SSWx9RMe – byLimbo May 22 '17 at 13:23
  • You are still referring to the old `AllChatters` but the data is saved in the `chatters` variable. Try for example `chatters.Chatters.Admins` instead of `AllChatters.Admins`, or `chatters.Chatters.Viewers` instead of `AllChatters.Viewers`. See https://pastebin.com/W6mNJwqM – nbokmans May 22 '17 at 13:27
  • I'm now getting this error (System.NullReferenceException: 'Object reference not set to an instance of an object.' chatters was null.) on `chatters.Chatters.Admins` when I run it. – byLimbo May 22 '17 at 13:44
  • Can you show your code again and also the full stacktrace (it should be 10+ lines) – nbokmans May 22 '17 at 13:48
  • After some debugging it turns out you have to enter channel names lowercase i.e. `dataClient.GetChannelViewers("bylimbo_");`. So `byLimbo_` yields no results, while `bylimbo_` shows the expected results. To fix, you can: 1) download the new DLL or 2) manually change the parameter to all lowercase – nbokmans May 22 '17 at 14:44
  • I added your new reference `var dataClient = new TwitchDataClient(); Is now getting an error with TwitchDataClient();` Do i need to make a class for it? The error im getting is "The type or namespace name 'TwitchDataClient' could not be found (are you missing a using directive or an assembly reference?)" – byLimbo May 22 '17 at 15:12
  • Crap, sorry, I forgot I changed the name of some classes (I actually submitted my code to the repository I linked above, as an update). Use `TwitchTmiClient` instead of `TwitchDataClient`. Sorry! (code sample in post updated) – nbokmans May 22 '17 at 15:15
  • I really appriciate all the help you given me, but I am still getting the same error on "(System.NullReferenceException: 'Object reference not set to an instance of an object.' chatters was null.) on chatters.Chatters.Admins" I can understand if you're over this and want to stop or something ,haha – byLimbo May 22 '17 at 15:20
  • I really can't reproduce your error. I tried a basic project and it seems to work for me (obviously didn't care about how it looked too much) as you can see here in this image (used a different channel): http://i.imgur.com/GtEYtj6.png - this is the code I used: https://pastebin.com/iSbf37y5 Anyway I have to travel home now, I'll be back in a couple hours. – nbokmans May 22 '17 at 15:34
  • This is what shows up when I run in it. No idea why. Im heading to bed, thanks for all your effort you have put in to help. http://imgur.com/a/fXgbI – byLimbo May 22 '17 at 15:41
  • It still didnt end up working, I just left it. I have a question though unrelated to this question but with the same code. Am I able to message you somewhere else? – byLimbo May 23 '17 at 11:05
  • Feel free to open a new question. Sorry, I don't have alternative means of communication other than SO chat. – nbokmans May 23 '17 at 11:37
  • https://stackoverflow.com/questions/44134600/uptime-twitch-command-twitchbot-c If could help, or give me advice, thatd be great. :) I appreciate all you have done for me so far. – byLimbo May 23 '17 at 12:30
  • So, I am now experiencing a new problem. Everytime I change this name (Yes, # is still kept.) `string[] separator = new string[] { "#bylimbo_ :" };` I get an error of "System.IndexOutOfRangeException: 'Index was outside the bounds of the array." on `string message = readData.Split(separator, StringSplitOptions.None)[1];` – byLimbo May 23 '17 at 14:23