LoginSignup
1
3

More than 3 years have passed since last update.

discord.py で 質問箱Bot を作った話

Last updated at Posted at 2020-05-09

はじめに

Discord で 質問箱Bot を作りました。
技術的な話としては、画像のリンクを取得し、転送する話をします。
リポジトリはこちら

作った動機としては

  • 質問に対する心理的安全性を確保する
  • 質問が公開されることで、情報共有の機会を増やす
  • 質問が公開されることで、回答できる人を増やす

などがあります。DM会話問題を改善するきっかけになればいいなと思います

事前準備

  • VMインスタンスを立てる
  • Python3 の環境を作る
  • Gitのパスを通す

これらは記事が出てくると思うので、いい感じにググってください。よくわからなかったら質問してください

使い方

環境によってはpythonpython3に置き換えてください。

$ git clone https://github.com/t4t5u0/question_box.git
$ cd question_bot
$ vim info.json
$ nohup python main.py &

画像の話


@client.event
async def on_message(message):
    for file_ in message.attachments:
        file_url = file_.url
        file_name = file_.filename
        async with aiohttp.ClientSession() as session:
            async with session.get(file_url) as resp:
                if resp.status != 200:
                    return await to_send_channel.send('ファイルを取得できませんでした')
                data = io.BytesIO(await resp.read())
                # 送信部分
                await to_send_channel.send(file=discord.File(data, file_name))

message.attachment には添付されたファイルライクオブジェクトの情報がリストで入っています。
message.attachment.url には画像のurlが格納されています。
公式サンプルを元に、実装しました。

おわりに

store.csv にログを吐き出すようにしています。不要であればコメントアウトしてください

動作例
20200510_002453.jpg

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