【Pycord】モーダルのdiscord.InputTextStyleでどのように入力できるかを見てみた

Code

はじまり

リサちゃん
リサちゃん

おお〜、Discordではモーダルまで使えるのかあ〜

135ml
135ml

それじゃあ、今回はPycordでモーダルを作る時に使える
テキスト入力のタイプを見ていくよ

リサちゃん
リサちゃん

うい〜

Discordではモーダル画面が表示できる

巷で有名なチャットサービス、「Discord」では「モーダル画面」を使って、複数行に渡る文章を入力することが出来ます。

そのモーダル画面で利用できる入力タイプは、以下の5種類です。

  • Short Input
  • Long Input
  • Singleline Input
  • Multiline Input
  • Paragraph

それでは、見ていきましょう。

並べてみた

5つの入力タイプを指定して、同じモーダル画面上に並べてみました。

このPycordの公式ガイドにあるEmbedするサンプルコードを参考に並べてみました。

class MyModal(Modal):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.add_item(discord.ui.InputText(
            label="Short Input", style=discord.InputTextStyle.short
        ))
        self.add_item(discord.ui.InputText(
            label="Long Input", style=discord.InputTextStyle.short
        ))
        self.add_item(discord.ui.InputText(
            label="Singleline Input", style=discord.InputTextStyle.short
        ))
        self.add_item(discord.ui.InputText(
            label="Multiline Input", style=discord.InputTextStyle.short
        ))
        self.add_item(discord.ui.InputText(
            label="Paragraph Input", style=discord.InputTextStyle.short
        ))

    async def callback(self, interaction: discord.Interaction):
        embed = discord.Embed(title="Modal Results")
        embed.add_field(name="Short Input", value=self.children[0].value)
        embed.add_field(name="Long Input", value=self.children[1].value)
        await interaction.response.send_message(embeds=)


@bot.command(name="notion", description="aaa", guild_ids=guild_ids)
async def notion(ctx: discord.ApplicationContext
    modal = MyModal(title="Modal via Slash Command")
    await ctx.send_modal(modal)

なんか、大きく2種類に分かれていそうですね。

short, singlelineはほぼ同じ。

1行のみ入力できるようです。ちなみに、4000字の入力制限があります。

long, multiline, paragraphはほぼ同じ。

複数行入力できます。この入力スタイルでも、4000文字の入力制限が設けられているようです。

また、discord.InputTextStyleの詳細は、このページから確認できます。

Enumerations
The API provides some enumerations for certain types of strings to avoid the API from being stringly typed in case the strings change in the future. All enumera...

モーダル画面から送信してみる

そして、モーダル画面から入力して、

送信すると、こんな風にDiscord上で出力されます。

4000字入力するとエラーになる

singlelineに4000文字入力して送信しようとするとエラーになりました。エラーメッセージがこんなことになっていたので、全角だともっと少なくなりますね。

他のテキストスタイルでも同じことになりそうです。

エラー文

discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.embeds.0.fields.0.value: Must be 1024 or fewer in length.

Modalクラスのソースコード

それから、テキスト以外に入力できないのかどうかを調べてみると、Modalクラスのソースコードがありました。

そのソースコードによると、add_itemするときはInputTextしか受け付けないようですね・・・

まあ、スラッシュコマンドを打ち込む時に選択肢を表示できるタイプにして入力すれば良いかな。

おしまい

リサちゃん
リサちゃん

ははあ、まあ大体どんな風に動くのかは分かったよ。

135ml
135ml

現状ではテキストだけだけど、

長い文章を打てるようになるのは有り難いよな

以上になります!

コメント

タイトルとURLをコピーしました