5
4

More than 3 years have passed since last update.

ファイルを送るとパスワード付きzipを返してくれるDiscordBot

Last updated at Posted at 2020-03-30

はじめに

パスワード付きのzipファイルを扱う際、個人だと7-Zip、Lhaplus、zipコマンドなど使う方法があるが、正直面倒くさい。
そこで、pythonでzipを扱うpyminizipモジュールがあったので、discordにファイルを投げるとパスワード付きのzipを返してくれるbotを作りました。

📗目次

実行環境

bot作成

コード

使い方

課題

🔧実行環境

Raspbian GNU/Linux 9

python 3.5.3

discord.py 1.2.5

requests 2.23.0

pyminizip 0.2.4

📦 bot作成

こちらに詳細が載った記事があります。

Discord Botアカウント初期設定ガイド for Developer

Pythonで実用Discord Bot(discordpy解説)

コード

botを作成できたら、main.pyとconfig.pyを作成し、下記のコードを貼り付けます。

main.py
# -*- coding:utf8 -*-

# インストールした discord.py を読み込む
import discord
# 自分のBotのアクセストークンをconfig.pyに書いておく
# config.pyは同じディレクトリ内に配置
import config
import requests
# pythonでパスワード付きzipを作成するモジュール
import pyminizip
import os

# -----------------------------------クラス分けしたほうがいい------------------------------------
# ファイルをダウンロードする関数
# import requests
def download(title, url):
    try:
        r = requests.get(url)
        # openの中で保存先のパス(ファイル名を指定)
        with open("添付ファイルの保存先のディレクトリのパス" + title, mode='w') as f:
            f.write(r.text)
    except requests.exceptions.RequestException as err:
        print(err)
# --------------------------------------------------------------------------------

# 自分のBotのアクセストークンをconfig.pyに書いておく
TOKEN = config.TOKEN

# 接続に必要なオブジェクトを生成
client = discord.Client()

# 起動時に動作する処理
@client.event
async def on_ready():
    # 起動したらターミナルにログイン通知が表示される
    print('ログインしました')

# メッセージ受信時に動作する処理
@client.event
async def on_message(message):
    # メッセージ送信者がBotだった場合は無視する
    if message.author.bot:
        return
    # ファイルを添付して、「/zip」が含まれていたらローカルに保存
    # テキストに「/zip」が含まれているか判定
    if "/zip" in message.content.split():
        text = message.content.split()
        # ifで分岐 パスワード入力があるかどうか
        try:
            password = text[1]

            # 投稿されたファイルの詳細を取得
            file = message.attachments[0]
            # ファイルを保存
            # 編集しなくていい
            download(file.filename, file.url)
            await message.channel.send("パスワード付き" + file.filename + ".zipを返します")

            # 保存したファイルをパスワード付きzipに変換
            # IndexError: list index out of range
            # ()内を編集してください
            pyminizip.compress("添付ファイルを保存したディレクトリのパス" + file.filename,"/","zipファイルを保存したいディレクトリのパス" + file.filename + ".zip",password,9)

            # zipしたファイルを送り返す
            await message.channel.send(file=discord.File("zipファイルを保存したディレクトリのパス" + file.filename + ".zip"))
            # ファイルの削除
            # 権限をbotに与えないと使えないエラー発生
            # discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
            await message.delete()

            # 保存したファイルを削除
            os.remove("zipファイルを保存したディレクトリのパス" + file.filename + ".zip")
            os.remove("添付したファイルを保存したディレクトリのパス" + file.filename)

        # パスワード入力がなかった場合エラー
        except IndexError:
            await message.channel.send('パスワードを入力してください')


# Botの起動とDiscordサーバーへの接続
client.run(TOKEN)

同じmain.pyと同じディレクトリ内に、config.pyを作成する。

config.py
# -*- coding:utf8 -*-

TOKEN = "自分のbotのアクセストークン"

使い方

コメント 2020-03-30 225048.png
botのいるサーバーにファイルを投げるときに、「/zip パスワード」とコメント入れると、パスワードの部分に書いた文字列がパスワードに設定されたzipがbotから返ってきます。
コメント 2020-03-30 225130.png
写真の例でいうと、main.py.zipには、passwordという文字列がパスワードに設定されています。

課題

現在discordでは、8MBを超えるファイルを貼ることができないという点と、フォルダーを貼ることができないという点で、簡単なファイルの圧縮、パスワード設定にしか使えないので、解決方法を模索していきたいと思います。

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