Open16
Rustでを学びつつ、ついでにdiscord botを作成してみる

エンジニア、旅行、ゲーム仲間で良く利用しているdiscord。
そこで活用できるbotを作成してみる物語。

サンプルコードを試す
READMEに書いている手順に従う。
- Install cargo-make cargo install --force cargo-make
- Clone the repository: git clone https://github.com/serenity-rs/serenity.git
- CD into the serenity folder: cd serenity
- Run cargo make 1, where 1 is the number of the example you wish to run; these are:

以下のコマンドを実行したらエラーになった。
DISCORD_TOKEN={発行したDiscord Token} cargo make 1
エラーメッセージは以下の通り。
Client error: Gateway(DisallowedGatewayIntents)

Bot 側の設定の問題だった。
MESSAGE CONTENT INTENT
を有効にしたら、実行できた。

DiscordにBotを追加する
以下のURLにアクセスし、Botを追加する。
https://discord.com/oauth2/authorize?client_id={Application_ID}&scope=bot&permissions=8

Botが追加される。

メッセージにBotが反応するサンプルを試してみる。
DISCORD_TOKEN={発行したDiscord Token} cargo make 17

サンプルコードを読み解きつつ、Rustについて学習してみる。

利用しているパッケージを確認。
Cargo.toml
[dependencies]
serenity = { path = "../../", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "collector"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
dotenv = { version = "0.15.0" }
Tokio
dotenv

Tokioについては、以下のチュートリアルが参考になった。