はじめに
こんにちは、鈴木と申します。
今回は自分がtwurpleを拡張して作った、@suzuki3jp/twitch.js
というパッケージを使って、Twitchのチャットボットを作ってみようと思います。
npmに公開されているパッケージ名は@suzuki3jp/twitch.js
ですが、長いので以降twitch.js
と呼びます
この記事はJavaScript, Node.jsがある程度わかる方向けの記事です。
twitch.js ってなに?
twitch.jsは下記のような特徴を持つNode.jsのパッケージです。
- twurpleを拡張して、使いやすく・わかりやすくしてある。
-
discord.jsを参考に作られた。
- そのため、discord.js使用者には使いやすい構造
Twtichアカウントのセットアップ
今回の本題はチャットボットを作ることなので詳しくは触れません。各自調べながらclient id, client secret, token, refresh token
の四つを取得してください。
リンク
・Twitch Developer Console - Twitchアプリケーションを作る(二段階認証必要)
・Twitch Authentication Docs - Twitchトークンを取得する
twitch.jsをインストールする
npm i @suzuki3jp/twitch.js
Botを起動してみよう!
const { TwitchClient } = require("@suzuki3jp/twitch.js");
// クライアントのオプション定義
const authOptions = {
accessToken: "token",
refreshToken: "refresh token",
clientId: "client id",
clientSecret: "client secret",
};
const clientOptions = { channels: ["hoge"] };
// クライアント定義
const client = new TwitchClient(authOptions, clientOptions);
// readyイベント
client.on("ready", () => {
console.log("twitch client is ready!");
});
client.login();
> node index.js
twitch client is ready!
このプログラムはコメントに書いてある通りクライアントを定義し、ready
イベントが発生したときに"twitch client is ready!"とコンソールに出力するプログラムです。
今回はこれで以上です!次回はmessageCreate
イベントでメッセージが送信された時の処理などをやっていきます!
次回