蛇ノ目の記

技術のことも。そうでないことも。

Python3.7でDiscord.pyを動かすときの躓きどころと対策

近頃、またDiscord botを作っている。

Python.3.7に上げたことによってDiscord.pyが動かないという現象に見舞われたので対策をメモ。

発生したエラー

このissueと同様のエラーが発生した。

github.com

対策

This library does not support 3.7. This error in particular is caused by async becoming a reserved keyword.

Please use Python 3.4-3.6.

async予約語になったからっぽい。Python3.4-3.6を使って、どうぞ。」

Python3.7からasync予約語になったことが原因のようだ。

バージョンを下げたくなかったのでさらに読み進めてみた。

https://github.com/Rapptz/discord.py/issues/1249#issuecomment-412256277

によれば

Your options are the following:

  • separately upgrade aiohttp and websockets to the latest versions after installing the async branch from GitHub
  • downgrade to Python 3.6
  • switch to the rewrite branch
  • aiohttpとwebsocketsを別々にアップグレードしてから、asyncブランチをインストールする

  • Python3.6にダウングレードする

  • rewriteブランチを使う

とのこと。一番手っ取り早そうなrewriteブランチを使う方法でやってみる。

ちなみに、rewriteブランチを使うという対策は以下のQiitaでも紹介されている(エラーの原因については触れられていない)。

qiita.com

以下のコマンドでDiscord.pyのrewriteブランチをインストール。

pip install git+https://github.com/Rapptz/discord.py.git@rewrite

rewriteブランチでの注意点

これまではbotに発言させる際にclient.send_messageメソッドを使っていたが、rewriteブランチではmessage.channel.sendメソッドを使う。ググって出てくるDiscord.pyの使い方は前者で書かれていることが多いので気をつけよう。