LoginSignup
3
5

More than 5 years have passed since last update.

0からdiscordのbotを作ってみる

Last updated at Posted at 2018-03-10

discord用のbotをさくっと作れるという記事をみて、node.jsのお勉強を兼ねて環境の構築とHellow world.ぐらいまでをお試ししてみた結果をメモします。

Bot用のアカウントを登録する

まずdiscordにbot用のアカウントを追加します。
ただ9割9分9厘こちらの記事を参考にしたので、そのまま参考にします。
https://liginc.co.jp/370260

node.jsのinstall

アクセストークンを書き換えて、botをdiscordのサーバーに登録できるところまで終わったら続いてnode.jsのinstallです。

今回は常駐のbotを作りたいなーというのが最初の動機なので、VPSで動かすことを目的としています。
0から始めるのタイトル通り、node.jsなんて入ってすらいないのでざくざく環境を構築します。

基本はこちらのサイトを参考に手なりでコマンドをがしがし投入。
https://qiita.com/akippiko/items/3708016fc43da088021c

nvm、nvmからnodeのインストール。

$ nvm ls-remote
        v0.1.14
        v0.1.15
        v0.1.16
        v0.1.17
            :
        v8.10.0   (Latest LTS: Carbon)
            :
         v9.7.0
         v9.7.1
         v9.8.0

$ nvm install 8.10.0
Downloading and installing node v8.10.0...
Downloading https://nodejs.org/dist/v8.10.0/node-v8.10.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.10.0 (npm v5.6.0)
Creating default alias: default -> 8.10.0 (-> v8.10.0)

最新のリビジョンだと9まであるけど、
とりあえずお試しなのでLTSの8.10.0を選択。

他はサイトを参考に手なりでポチポチと選択を進めて行けば問題なくインストールまで終わりました。

hello, world.

しゃれた見出しですがやることはプログラムを走らせて、コンソール文でテキストをゲロるだけ。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('hello,world!\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

サンプルソースをそのまま使ったらずっこける。
というかVPSで起動しているんだからローカルのIPを指定していたらダメなのは当然よね…。
というわけでiptablesを確認して空いているポートを確認

$ sudo iptables -L
listen(12345, '12.3.45.123');

VPSのIPとiptablesで確認した空いているポートにlistenの引数を書き換え。
改めて確認してみたところ無事「hello,world!」が表示されて無事動作確認完了。

discord botの作成

node.jsのインストールまではできたので前準備までは完了。
https://qiita.com/akippiko/items/3708016fc43da088021c
再度ページに戻って手順通りに
手なりでnode/npmのバージョンを調べてインストールしたバージョン通りであることをチェック。

$ node -v
v8.10.0
$ npm -v
5.6.0

問題なさそうなのでそのままdiscord.jsのインストールに踏み切ってみたもののWARNの出力。

npm install discord.js --save
npm WARN saveError ENOENT: no such file or directory, open '/home/.../package.json'
npm WARN enoent ENOENT: no such file or directory, open '/home/.../package.json'
npm WARN discord.js@11.3.2 requires a peer of bufferutil@^3.0.3 but none is installed. You must install peer dependencies yourself.
npm WARN discord.js@11.3.2 requires a peer of erlpack@discordapp/erlpack but none is installed. You must install peer dependencies yourself.
npm WARN discord.js@11.3.2 requires a peer of node-opus@^0.2.7 but none is installed. You must install peer dependencies yourself.
npm WARN discord.js@11.3.2 requires a peer of opusscript@^0.0.6 but none is installed. You must install peer dependencies yourself.
npm WARN discord.js@11.3.2 requires a peer of sodium@^2.0.3 but none is installed. You must install peer dependencies yourself.
npm WARN discord.js@11.3.2 requires a peer of libsodium-wrappers@^0.7.3 but none is installed. You must install peer dependencies yourself.
npm WARN discord.js@11.3.2 requires a peer of uws@^9.14.0 but none is installed. You must install peer dependencies yourself.
npm WARN sample-bot No description
npm WARN sample-bot No repository field.
npm WARN sample-bot No README data
npm WARN sample-bot No license field.

ここは調べてみてもいまいち原因がわからなかったのと、
ERRORがないのであんま気にしなくてよいらしいというグーグル先生の知見からスルーすることを決意。

で、サンプルプログラムをそのままコピペして起動。
無事readyの出力が出て一安心かと思いきや、
キーワードを飛ばしたところそんな関数ねーわハゲという熱いエラーコードを吐く始末。

/home/sample-bot/bot.js:30
        channel.reply(reply_text)
                ^
TypeError: channel.reply is not a function

channelをダンプしてみた感じ、replyがいなさそうなので、
リファレンスを見てみた感じからreplyをmessageから叩くように修正。
https://discord.js.org/#/

//        channel.reply(reply_text)
        message.reply(reply_text)
ready...
Sent message: こんばんわ。username様。

無事レスポンスを返してくれるっぽいところまで確認。

すごい詰まり方をするポイントは特になかったけれど、channelの中にreplyがいなかったのはちょこっとだけ罠だったので、少しだけ気を付けたほうが良いかも。

3
5
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
3
5