【问题标题】:Find Subscriber Count of YouTube Channel in Discord.js在 Discord.js 中查找 YouTube 频道的订阅者数量
【发布时间】:2020-10-03 13:37:37
【问题描述】:

我正在制作一个非常简单的 Discord 机器人,用于显示某个 YouTuber 的订阅者数量。 我希望这是自动化的,所以我不必一直更改它。

这是我当前的代码,它将订阅者计数显示为机器人的状态。 如何通过网站/YouTube API 获取频道的订阅人数? (我知道它会被缩写,这很好)。

const Discord = require("discord.js");
const {
  Client,
  MessageAttachment,
  MessageEmbed,
  MemberAdd,
  RichEmbed,
  message
} = require("discord.js");
const client = new Client();
client.on("ready", function() {
  setInterval(function() {
    var subcount
    subcount = "105M"
    client.user.setActivity(subcount + " subscribers" , {
      type: "STREAMING",
      url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
    });
  }, 5000);
});

client.login("...");

【问题讨论】:

标签: javascript node.js youtube discord.js


【解决方案1】:

这是我如何将一堆 youtube 频道 url 翻译成具有 5 个字段的 pojo[],而不是异步:

  public getRows() : RowData[] {
    console.log("Getting row data...");
    let tmpData: RowData[] = [];
    let promises : Promise<ChannelResponse>[] = [];
    for (let ch = 0; ch < this.channelUrls.length; ch++) {
      let tmp_promise = this.http.get<ChannelResponse>(this.channelUrls[ch].url).toPromise();
      promises.push(tmp_promise);
    }
    Promise.all(promises).then((valuesArray) => {
      //console.log('Response: ' + JSON.stringify(valuesArray));
      for (let i = 0; i < valuesArray.length; i++) {
        const item : Item = valuesArray[i].items[0];
        const channel : Channel = this.channelUrls[i];
        tmpData.push({
          channel: channel.name,
          id: channel.id,
          views: Number(item.statistics.viewCount),
          subscribers: Number(item.statistics.subscriberCount),
          videos: Number(item.statistics.videoCount)
        });
      }
    });

要让它工作,你需要这个:

export interface RowData {
  channel: string;
  id: string;
  views: number;
  subscribers: number;
  videos: number;
}

export interface Channel {
  name: string;
  id: string;
  url: string;
}

export interface ChannelResponse {
  kind:     string;
  etag:     string;
  pageInfo: PageInfo;
  items:    Item[];
}

export interface Item {
  kind:       string;
  etag:       string;
  id:         string;
  statistics: Statistics;
}

export interface Statistics {
  viewCount:             string;
  subscriberCount:       string;
  hiddenSubscriberCount: boolean;
  videoCount:            string;
}

export interface PageInfo {
  totalResults:   number;
  resultsPerPage: number;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2015-03-27
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多