0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MTGのカードをあいまい検索するDiscordのBotをつくった

0
Posted at

昔作って放置してたやつを作り直しました。
おっぱいチャレンジにも成功。

image2.png

その他の例

image1.png

image3.png

なんで赤ばっかりなの。

image4.png

使い方

DiscordBotの用意

  1. Discordにログインし、https://discord.com/developers/applications にアクセスします

  2. 新しいアプリケーションをクリック、名前をつけて新規作成します
    d1.png

  3. 左側の「概要」から「Bot」を選択、「トークンをリセット」を選択して、トークンをリセットしたのち新しく生成されたトークンをコピーします。メモしておいてください
    d2.png

  4. 下の方に移動し、Message Content Intent の項目をオンにします
    d3.png

5.保存します。

ソースコードの用意

Github(https://github.com/sakots/mtg_img_searcher)からクローンしてディレクトリに移動します。

git clone git@github.com:sakots/mtg_img_searcher.git
cd mtg_img_searcher

インストールしていない場合uvをインストールします。

linuxやmacの場合

curl -LsSf https://astral.sh/uv/install.sh | sh

windowsの場合

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

必要なライブラリをインストールします。

uv add discord.py bs4 urllib3 python-dotenv requests

.envというファイルを作成し、以下のように入力し保存します。

TOKEN=(ここにさっきメモしたDiscordのBotトークンを入れる)

以下で起動します。

uv run python yahoo.py

工夫

検索語句 + (MTG|マジック|ギャザ) + (カード|card) でMTGのカードを絞っています。

query = f"+{command} (MTG or マジック or ギャザ or magic) (カード or card)"

さらに画像の縦横比がカードのものから遠い場合弾いています。
CARD_ASPECT_TOLERANCEである程度の差をカバーしています。

CARD_ASPECT_RATIO = 63 / 88
CARD_ASPECT_TOLERANCE = 0.08
def _is_card_aspect_ratio(image_bytes):
  size = _read_image_size(image_bytes)
  if size is None:
    return False

  width, height = size
  if width <= 0 or height <= 0:
    return False

  return abs((width / height) - CARD_ASPECT_RATIO) <= CARD_ASPECT_TOLERANCE

終わりに

楽しんでください。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?