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?

More than 1 year has passed since last update.

Discord WebHookにおける{"embeds": ["0"]}エラー

Posted at

はじめに

DiscordのWebHookを用いてEmbedを送信するときに、なぜか正しいはずなのにAPIのレスポンスで{"embeds": ["0"]}というエラーが返ってきたため、原因の解明と注意点についての覚え書き。

実行環境など

  • 言語はPHPを使用
  • Discord-PHPなどの特別なライブラリは未使用
  • JSONを直書きではなく、自前のプログラムで配列をエンコードしている

このエラーの原因

基本的にEmbedと呼ばれる埋め込みは、一回で最大10個送れます。
つまりこのエラー{"embeds": ["0"]}はそのEmbedsの一つ目に問題があるよということ。
私の場合、Embedを一つだけ送るときにこのエラーが出ていたので紛れもなくそのEmbedがおかしいということになる。

以下はstackoverflowの投稿を簡単に和訳して抜粋したもの。
情報の信憑性の有無に関しては今回スルー。
引用元の投稿はこちらから

1. 無効な要素が含まれている

主に値のフォーマットが間違っているパターンと値そのものがおかしいパターンの二通り。

  • timestampの要素がISO8601に準していない
  • colorがdecimal値(10進数)じゃなくて、"red""#ff00ff"などである
  • icon_url avatar_url urlなどのリンクが要求される要素がhttps://http://で始まっていない
  • ブール値であるべき要素で別の型の要素が渡されている
  • 空またはnull値がある
  • 未定義の値がある

2. 特定の要素の制限を超えている

文字数や含められる要素の制限を超過してしまっているパターン。

  • titleの文字数制限 256 を超えている
  • descriptionの文字数制限 2048 を超えている
  • fieldnameの文字数制限 256 を超えている
  • fieldvalueの文字数制限 1024 を超えている
  • footerの文字数制限 2048 を超えている
  • authorの文字数制限 256 を超えている
  • fieldの数が 25 を超えている
  • 全体の合計文字数が 6000 を超えている

私の場合

》原因

  • icon_url avatar_url urlなどのリンクが要求される要素がhttps://http://で始まっていない
  • 空またはnullがある

》そもそもなぜそうなったのか

私は上項でも述べたようにJSONを直書きではなく、自前のプログラムで配列をエンコードしている
以下がエラーを誘発させていた埋め込みを作る箇所の修正前のコードである。

public function buildEmbed() : array
{
    $embed = [];
    $embed["title"] = $this->title;
    $embed["description"] = $this->description;
    $embed["url"] = $this->url;
    $embed["timestamp"] = $this->timestamp;
    $embed["color"] = $this->color;
    $embed["footer"] = $this->footer;
    $embed["image"] = $this->image;
    $embed["thumbnail"] = $this->thumbnail;
    $embed["author"] = $this->author;
    $embed["fields"] = $this->fields;
    return $embed;
}

当初の目的として、様々な用途で使うためにAPIのようなものとして実装をしていたが、初期値である空配列[]や空文字""がどんな状況であれ確実に埋め込みの要素として含まれてしまうことが原因だった。
すべてのプロパティが初期値である場合のみ条件分岐で$embedへ格納しない方法をとることで無事再稼働させた。

さいごに

とてつもなく初歩的なことでミスをしていたのが恥ずかしい。
でも誰かのためになればいいと思ったので覚え書きとして残した。

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?