discord py 2.0 でボタン処理を実装しようとしています。
Pythonバージョン:Python 3.10.4
使用モジュール:discord.py @ git+https://github.com/Rapptz/discord.py@9648986028a9721e1ba733750eb8c30e163dcb2c
こちらのチュートリアルを参考に以下のコードを記載しました。
https://gist.github.com/lykn/bac99b06d45ff8eed34c2220d86b6bf4
Python
1# Note: This is just an "improved" code of what's shown above and that's how it's going to work throughout the gist the above code is going to be refined over and over again. 2import discord 3from discord.ext import commands 4 5intents = discord.Intents.all() 6client = commands.Bot(command_prefix=".", intents=intents) 7 8 9class Buttons(discord.ui.View): 10 def __init__(self, *, timeout=180): 11 super().__init__(timeout=timeout) 12 13 @discord.ui.button(label="Button", style=discord.ButtonStyle.gray) 14 async def blurple_button(self, button: discord.ui.Button, interaction: discord.Interaction): 15 button.style = discord.ButtonStyle.green 16 await interaction.response.edit_message(content=f"This is an edited button response!", view=self) 17 18 19@client.command() 20async def button(ctx): 21 await ctx.send("This message has buttons!", view=Buttons()) 22 23token = "TOKEN" 24client.run(token) 25
.button コマンドを使用すると、ボタン が表示されますが、押してもインタラクションエラーとなっていまいます。
エラーは以下のとおりです。
Error
1Ignoring exception in view <Buttons timeout=180 children=1> for item <Button style=<ButtonStyle.secondary: 2> url=None disabled=False label='Button' emoji=None row=None>: 2Traceback (most recent call last): 3 File "C:\Users\KOMATSU\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ui\view.py", line 414, in _scheduled_task 4 await item.callback(interaction) 5 File "c:\Users\KOMATSU\practice\test.py", line 15, in blurple_button 6 button.style = discord.ButtonStyle.green 7AttributeError: 'Interaction' object has no attribute 'style'
await interaction.response.edit_message(content=f"This is an edited button response!", view=self)
を、await interaction.response.send_message("ボタンがクリックされました", ephemeral=True) に変更しても同様のErrorがでます。
やりたいこと
正常にインタラクションに対してレスポンスを行いたい。
初質問のため、至らないところが多いかと思いますが、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。