1ntegrale9

埋め込みメッセージ(Embed)あたりを触っていると、
エラーメッセージ内に _EmptyEmbed というものが現れる場合があります。

例えば、

if 'hello' in message.embeds[0].title

というコードの場合、以下のようなエラーを吐くことがあります。

TypeError: argument of type '_EmptyEmbed' is not iterable

この _EmptyEmbed についてはドキュメントに記載されていないので、
正体はソースコードを読まないと分かりません。
https://github.com/Rapptz/discord.py/blob/master/discord/embeds.py

embeds.py を読むといたるところに EmptyEmbed が現れますが、
このあたりを読むと役割が分かりやすいと思います。

https://github.com/Rapptz/discord.py/blob/c6539bbc615e22a59455179b8526dc6ae264c2a4/discord/embeds.py#L32-L42

class _EmptyEmbed:
    def __bool__(self):
        return False

    def __repr__(self):
        return 'Embed.Empty'

    def __len__(self):
        return 0

EmptyEmbed = _EmptyEmbed()

https://github.com/Rapptz/discord.py/blob/c6539bbc615e22a59455179b8526dc6ae264c2a4/discord/embeds.py#L106-L117

    def __init__(self, **kwargs):
        # swap the colour/color aliases
        try:
            colour = kwargs['colour']
        except KeyError:
            colour = kwargs.get('color', EmptyEmbed)

        self.colour = colour
        self.title = kwargs.get('title', EmptyEmbed)
        self.type = kwargs.get('type', 'rich')
        self.url = kwargs.get('url', EmptyEmbed)
        self.description = kwargs.get('description', EmptyEmbed)

ざっくり言えば、setされていないEmbedの要素を仮置きするオブジェクトです。
なのでEmbedに titleurldescription
あるかを確認したい時は EmptyEmbed 判定をすると良いでしょう。

_EmptyEmbed クラスの定義にある通り、
真偽値(bool)として評価するとFalseが返るので、
そのまま if 文に入れて判定しても良いでしょう。

このように。

if message.embeds[0].title:
    print('title is set')

因みに冒頭部のエラーの原因は何かを念のため説明すると、
title 要素がsetされている場合は正常に動きますが、
そうでない場合に EmptyEmbed を操作してしまうというバグです。

comments powered by Disqus