1
0

More than 1 year has passed since last update.

【第一章】Twitchのチャットボットを作る。~Bot起動まで~

Last updated at Posted at 2023-01-01

はじめに

こんにちは、鈴木と申します。
今回は自分が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を起動してみよう!

index.js
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イベントでメッセージが送信された時の処理などをやっていきます!

次回

リンク

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0