如何在 nodejs tmijs 聊天机器人中创建正常运行时间命令

How to make an uptime command in nodejs tmijs chat bot

如何使用 tmi.js 制作正常运行时间命令,我认为它会很简单;

client.on("chat", function(channel, user, message, self, uptime){ 
client.say("CHANNEL", "Channel has been live for " + uptime
})

您将如何执行此命令,我给出的示例不起作用,请您告诉我。

看起来频道元数据在 Twitch WebSocket API. If you want to get that information, you'll need to go through the "New Twitch API" and use the /streams 端点上不可用。

您还需要 Client-ID 才能向此端点发出请求。您可以按照此处的说明获取一个:Apps & Authentication Guide.

获得 Client-ID 后,您可以提出请求。我正在使用 node-fetch 模块来简化请求。此示例将获取 2 个最活跃的流。您可以调整查询字符串参数以获得合适的流。

const querystring = require("querystring"),
    fetch = require("node-fetch");

const CLIENT_ID = "YOUR_CLIENT_ID";
const STREAMS_URL = "https://api.twitch.tv/helix/streams";

const qs = querystring.stringify({
    first: 2
});

const qUrl = `${STREAMS_URL}?${qs}`;

const fetchArgs = {
    headers: {
        "Client-ID": CLIENT_ID
    }
};

fetch(qUrl, fetchArgs)
    .then(res => res.json())
    .then(data => console.log(data))
    .catch(err => console.error(err));

这将打印出如下内容:

{
    data: [{
            id: '28378863024',
            user_id: '19571641',
            game_id: '33214',
            community_ids: [],
            type: 'live',
            title: 'Morning Stream! | @Ninja on Twitter and Insta ;)',
            viewer_count: 107350,
            started_at: '2018-04-18T14:58:45Z',
            language: 'en',
            thumbnail_url: 'https://static-cdn.jtvnw.net/previews-ttv/live_user_ninja-{width}x{height}.jpg'
        },
        {
            id: '28379115264',
            user_id: '22859264',
            game_id: '32399',
            community_ids: [Array],
            type: 'live',
            title: 'LIVE: Astralis vs. Space Soldiers - BO1 - CORSAIR DreamHack Masters Marseille 2018 - Day 1',
            viewer_count: 54354,
            started_at: '2018-04-18T15:28:44Z',
            language: 'nl',
            thumbnail_url: 'https://static-cdn.jtvnw.net/previews-ttv/live_user_dreamhackcs-{width}x{height}.jpg'
        }]
}

started_at 属性 是流开始的时间戳。请记住,这个 API 是有速率限制的,所以你应该缓存 started_at 这样你就不会立即 运行 没有请求。