【问题标题】:System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'System.IndexOutOfRangeException: '索引超出了数组的范围。'
【发布时间】:2017-05-22 08:40:43
【问题描述】:

首先 - 我知道有人问过这个问题。我几乎不知道 C# 还在学习,很多代码来自教程,所以我希望我能对我的实际代码有更直接的答案。我正在制作一个抽搐机器人。

 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);
        }
    }

得到错误 (System.IndexOutOfRangeException: 'Index was outside the bounds of the array.') 的行如下:

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

任何帮助都会很棒,谢谢。

【问题讨论】:

  • 显示GetChatters的实现
  • @nbokmans 它说它没有实现。我正在使用:using TwitchCSharp.Clients; using TwitchCSharp.Models; 当我将鼠标悬停在“GetChatters”上时,这行代码显示Chatters TwitchROChat.GetChatters(string channel, [TwitchCSharp.Helpers.PagingInfo pagingInfo = null]) 我正在使用已添加到资源中的 TwitchCSharp.dll 文件。
  • 你从哪里得到的 dll?
  • @nbokmans 通过本教程youtube.com/watch?v=bd87CS-Q7V4
  • 因为我无法观看 youtube.. “名称”不应该是频道名称吗? “名称”真的是频道名称吗?

标签: c# bots twitch


【解决方案1】:

已编译的 DLL

我在这里为您生成了一个已编译的 DLL,您可以下载并添加到您的项目中。你可以在这里找到这个:https://dropfile.to/9hzvwVX(更新)

现在您可以像这样为频道获取用户:

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

Chatters 现在将包含活动频道中的用户列表,按等级(管理员、管理员、查看者等)分隔

说明

因为这个问题与我的个人兴趣有关,所以我决定将您正在寻找的功能添加到我在 cmets 中发布的库中:https://github.com/michidk/TwitchCSharp

当然,下载文件总是有点粗略。所以我所做的是添加一个新的 Twitch 客户端实现,因为聊天数据不存储在 Twitch Kraken API 上,而是存储在旧的 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);
        }

    }
}

这个新的 Twitch 客户端使用两种模型将 json 反序列化为:

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;
    }
}

您可以在此处找到可以查看所有更改的存储库:https://github.com/nbokmans/TwitchCSharp/commit/ec38eecf1d0fbcb0b75c5de597a44582a61deb3d

如果您愿意,您可以git clone 上面的存储库并在 Visual Studio 中打开它来自己构建它。

【讨论】:

  • 有什么地方可以私信你吗?
  • 很遗憾,因为您没有 20 声望,但我们无法使用聊天功能。你需要什么?
  • 正如我所说,我真的是 C# 新手。我不知道如何实现您的 dll(顺便说一句非常感谢)以使用我当前的代码。我一直在复制教程,但我知道足以编辑部分代码,我只是不想继续向这个页面发送垃圾邮件。
  • 只需添加它,就像您添加视频中的那个一样。没有区别(尽管视频中的某些部分代码可能与此 DLL 不兼容)。
  • 不是在谈论将它添加到参考中,我更多的是在谈论我需要添加什么代码 var dataClient = new TwitchDataClient(); var chatters = dataClient.GetChannelViewers("someTwitchChannelName"); 我只是添加它并且我很好去还是我需要添加其他代码你'已添加到答案中?
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 2016-06-11
相关资源
最近更新 更多