项目作者: twitch-js

项目描述 :
A community-centric, community-supported version of tmi.js
高级语言: TypeScript
项目地址: git://github.com/twitch-js/twitch-js.git
创建时间: 2017-12-09T08:09:20Z
项目社区:https://github.com/twitch-js/twitch-js

开源协议:MIT License

下载




[DEPRECATED] TwitchJS

No Maintenance Intended
Coverage status
NPM version
NPM downloads
GitHub issues
GitHub bug issues

A community-centric, community-supported Twitch JavaScript SDK.

Features

  • Aligns with official
    Twitch IRC documentation
  • Forward-compatible, low-level, minimally-assertive architecture
  • Supports Node environments
  • Supports Browsers
  • Supports TypeScript
  • Connect to multiple channels
  • Chat commands
  • Rate limiter

In progress…

Here are the
planned features. If you
feel something is missing, create an issue or submit a PR against the next
branch.

Documentation

  1. Getting started
  2. Authentication
  3. Examples
  4. Interacting with Twitch chat
  5. Making requests to Twitch API
  6. Reference

Getting started

Module bundler (CJS/ESM)

If you are using a module bundler, such as Webpack,
Browserify, or a in a Node environment:

  1. Add TwitchJS to your project:
    1. npm install --save twitch-js
  2. Import TwitchJS

    1. // ES2015 syntax
    2. import TwitchJs from 'twitch-js'
    3. // OR ES5 syntax
    4. var TwitchJs = require('twitch-js')
    5. const twitchJs = new TwitchJs({ username, token })

Browser (IIFE)

If you are not using a module bundler, precompiled builds are available in the
dist folder:

  1. Include a script tag in your HTML:
    1. <script src="//unpkg.com/twitch-js@>2.0.0-beta.31"></script>
  2. Consume the library:
    1. <script type="javascript">
    2. const twitchJs = new window.TwitchJs({ username, token })
    3. </script>

Authentication

Obtaining a client ID

To obtain a client ID, follow the
instructions found in
the Twitch Developers documentation.

Obtaining a token

With a client ID,
tokens may be generated
on behalf of your users.

To quickly get started without a client ID, you may generate token using the
following, community-maintained, sites:

  1. Twitch Token Generator
  2. Twitch Chat OAuth Password Generator

Refreshing tokens

To help with refreshing tokens, an onAuthenticationFailure function may be
provided to the Messaging and API clients. onAuthenticationFailure() must
return a Promise that resolves with the refreshed token. Upon resolution, any
actions that yielded an expired token response will be retried with the new,
refreshed token.

Handling token refresh example

  1. // Optionally, use fetchUtil to help.
  2. import fetchUtil from 'twitch-js/lib/utils/fetch'
  3. const refreshToken = 'eyJfaWQmNzMtNGCJ9%6VFV5LNrZFUj8oU231/3Aj'
  4. const clientId = 'fooid'
  5. const secret = 'barbazsecret'
  6. const onAuthenticationFailure = () =>
  7. fetchUtil('https://id.twitch.tv/oauth2/token', {
  8. method: 'post',
  9. search: {
  10. grant_type: 'refresh_token',
  11. refresh_token: refreshToken,
  12. client_id: clientId,
  13. client_secret: clientSecret,
  14. },
  15. }).then((response) => response.accessToken)
  16. const token = 'cfabdegwdoklmawdzdo98xt2fo512y'
  17. const username = 'ronni'
  18. const twitchJs = new TwitchJs({ token, username, onAuthenticationFailure })
  19. twitchJs.chat.connect().then((globalUserState) => {
  20. // Do stuff ...
  21. })

See
Refreshing access tokens
for more information.

More information

See the
Twitch Developers documentation for
more information on authentication.

Examples

Examples are provided via CodeSandbox:

Contribution guidelines

If you wish to contribute, please see the
CONTRIBUTING
doc.

Special thanks

Schmoopiie and all the
original contributors of
tmi.js.

License

MIT

TwitchJS is not affiliated, associated, authorized, endorsed by, or in any way
officially connected with Twitch, or any of its
subsidiaries or its affiliates. The name “Twitch” as well as related names,
marks, emblems and images are registered trademarks of
Twitch.