ゲレの工房

ゲーム好きの中の人が、自分が作りたいアプリを作る記録です

初めてのdiscord bot : discord bot

discord.py を使って、最初のbotを作りましょう。
チャットbotとしては初めて行うのは Hello worldでしょうか。

bot サーバはローカルとします。

では始めましょう

まず、前回作った環境の中に入って、discord.pyをインストールします。

discord_bot $ source bin/activate
(discord_bot) discord_bot $ python3.6 -m pip install discord
Collecting discord
  Could not find a version that satisfies the requirement discord (from versions: )
No matching distribution found for discord

あれ、失敗する。
githubから取ってくるか

(discord_bot) discord_bot $ python3.6 -m pip install -U https://github.com/Rapptz/discord.py/archive/master.zip#egg=discord.py[voice]
Successfully built aiohttp multidict pycparser
Installing collected packages: chardet, multidict, async-timeout, aiohttp, websockets, pycparser, cffi, PyNaCl, discord.py
  Running setup.py install for discord.py ... done
Successfully installed PyNaCl-1.0.1 aiohttp-1.0.5 async-timeout-1.1.0 cffi-1.9.1 chardet-2.3.0 discord.py-0.16.0 multidict-2.1.4 pycparser-2.17 websockets-3.2

うまくいった

次に、サンプルコードを動かしてみましょう

コードはこちらを用います。


blog.sky-net.pw

このコードをまずローカルに持ってきて保存します。
今日はtest.pyと保存します。


ここからは一旦コンソールから離れて、discordappに戻りましょう。
といっても、開発者画面からですが。

discordapp.com

My Appsから、右画面の New Appをクリックします。

f:id:gelehrtecrest:20170311113257p:plain

NEW APPのページでAPP NAMEを設定します。

f:id:gelehrtecrest:20170311113656p:plain

自由につけていいはずですが、ここでは gelehrte-test-botとします。

あとはCreate a Bot Userを押して設定します。

Tokenというのが表示されるのでコピペしてください。(画像では消しています)

f:id:gelehrtecrest:20170311120031p:plain


これを今回作成したtest.pyにコピーします。

client.run('ここにTokenをコピー')

次に、REDIRECT URI(S)を設定します。
今日はローカルで動かすので、

http://localhost 

ほかは後で設定します。


では botを自分のdiscordに入れてみましょう。

スクリーンショットの中にClient IDが書いてあるのがわかりますか?

289950098010406912

って書いてあります。

この場合、以下にアクセスします。URLのclient_id の所に上記のClient IDを入力します。

https://discordapp.com/oauth2/authorize?client_id=289950098010406912&scope=bot

すると、botをどこのサーバに置くか設定画面に出ます。

f:id:gelehrtecrest:20170311121942p:plain

任意のサーバを設定したら保存します。
以下のページに遷移したら成功です。

f:id:gelehrtecrest:20170311122034p:plain

うまく成功していると、discordアプリ画面にbotが現れます。
f:id:gelehrtecrest:20170311122149p:plain


では最後に、botと少しお話ししてみましょう。

f:id:gelehrtecrest:20170311122936p:plain

こんな感じにおうむ返しします。


これで最初のbotができました。



予定:

次は、サーバにbotを置きたいと思います。
どのようにするか考え中。


広告