English 中文(简体)
Discord.py not response to hton activities
原标题:Discord.py not responding to button events

这里是在我的机器人中进行试验指挥。

@tree.command(name="button", description="testing")
async def button(interaction):
    view = discord.ui.View()  # make a view
    style = discord.ButtonStyle.primary  # makes the button blue
    item = discord.ui.Button(style=style, label="Click to continue!", custom_id="continue_button")  # make the button
    view.add_item(item=item)  # add button to view
    await interaction.response.send_message("Waiting until you press the button!", view=view)  # send message with button

    def check(i):
        return i.user.id == interaction.user.id and i.custom_id == "continue_button"

    try:
        interaction, button = await client.wait_for("button_click", check=check, timeout=10)
        # when button is pressed
        await interaction.channel.send("You pressed the button!")
    except asyncio.TimeoutError:
        # button wasn t pressed before timeout
        await interaction.channel.send("You didn t press the button within the specified time.")

然而,该机器人从来就没有被点击。 即使检查没有结果,也没有海关数据等,但bot子的冲销和点击纽州的互动结果失败。 此外,我还要在指挥部内保持顿语和倾听,以便我能够根据各州的用户新闻执行更多的法典。

我期望该机器人发出我点击 but子的讯息,但我总是在把ton子赶到来之后,才有时间。 我已尝试取消检查、停职等,但我无法得知此事。

最佳回答

The issue was caused because there is not a button_click event. Instead, look for interactions that were caused by buttons:

interaction = await client.wait_for( interaction , 
    check=lambda i:
    i.type == discord.InteractionType.component and
    i.user == interaction.user and
    i.channel == interaction.channel, timeout=30)
问题回答

我也正在学习,但我不认为你叫起检查职能/投入一个参数。

我最近也遇到了这一问题。 问题在于<条码><>丁顿_click> 只有当你重新使用<代码>残疾证书/编码>图书馆时,才会存在。

如果你重新做些什么的话,最好使用<条码>查询<>。

此外,你还希望在时间上发出回复信息。 因此最好使用一类。

class MyButtonView(discord.ui.View):
    @discord.ui.button(label="Click to continue!", style=discord.ButtonStyle.primary)
    async def mybutton_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message("You pressed the button!")


@tree.command(name="button", description="testing")
async def button(interaction):
    await interaction.response.send_message("Waiting until you press the button!", view=MyButtonView())  # send message with button

这是纽州的基准。

Verifying user

You want to check that the person who clicks the button is also the one who uses the command. We can achieve this by making our own interaction_check method in the class.

粉碎解释:MyButton 意见类别继承discord.ui.View。 类别,有<代码> 交易_check。 我们通过撰写自己的话,就推翻了这一点。

在这种方法中,只是检查提交人是否是互动使用者。 我们通过提出论点,要求<条码>_init__。

class MyButtonView(discord.ui.View):
    def __init__(self, author):
        self.author = author

    @discord.ui.button(label="Click to continue!", style=discord.ButtonStyle.primary)
    async def mybutton_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message("You pressed the button!")
    
    async def interaction_check(self, interaction: discord.Interaction):
        return self.author.id == interaction.user.id

Since MyButtonView is now expecting the author argument we have to parse that in when sending the button message.

await interaction.response.send_message("...", view=MyButtonView(interaction.user))

Timeout

我们现在想要它发出信息。 同样,我们采用自己的方法<代码>_timeout。 然而,这没有任何论点,因此我们必须在<条码>内穿透<>/条码>。

class MyButtonView(discord.ui.View):
    def __init__(self, interaction):
        self.interaction = interaction

    @discord.ui.button(label="Click to continue!", style=discord.ButtonStyle.primary)
    async def mybutton_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message("You pressed the button!")

    async def interaction_check(self, interaction: discord.Interaction):
        return self.interaction.user.id == interaction.user.id

    async def on_timeout(self): # No interaction argument
        await self.interaction.channel.send("You didn t press the button within the specified time.")

这还取消了对<代码>的的要求。 由于我们可以从<编码>interaction的论点中找到同样的论据。

请注意在<条码>_init、<条码>、>条码>、[自动>、>条码/代码]和丁吨<条码>之间的区别。

Documentation

http://discordpy.reads.io/en/stable/interactions/api.html#discord.ui.Button”rel=“nofollow noreferer”>discord.ui.Button ,

最后说明: 回答如此漫长,希望你能够充分了解你的工作。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签