首页 > 解决方案 > discord.py discord_components 交互失败

问题描述

我想制作一个不和谐的机器人,他的一个功能是商店,对于商店菜单,我使用了不和谐的组件,它循环到用 yaml 制作的项目文件,然后附加到 python 中的列表,然后用该项目制作嵌入消息和他的选项,带有前进和后退按钮,当我单击它们时它们会起作用,但是它们需要一段时间才能更新并且我收到“此交互失败”

代码:

with open('items.yaml', 'r') as yaml_file:
    item_list_yaml = yaml.safe_load(yaml_file)

items = []
shop_items = []

for item in item_list_yaml:
    items.append(item)

for item in items:
    new_item_listed = {
    'name':item_list_yaml[f'{item}']['name'],
    'buy_price':item_list_yaml[f'{item}']['buy_price'],
    'amount':item_list_yaml[f'{item}']['amount'],
    'sell_price':item_list_yaml[f'{item}']['sell_price'],
    'item_id':item_list_yaml[f'{item}']['item_id'],
    'emoji':item_list_yaml[f'{item}']['emoji'],
    }
    copy  = new_item_listed.copy()
    shop_items.append(copy)

class Shop(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.cluster  = MongoClient(DB)
        cluster  = MongoClient(DB)
        self.collection = cluster.users.eco
        DiscordComponents(client)
    
    @commands.command(
        name = "shop"
    )
    async def main_shop(self, ctx):
        global position_in_shop_items
        position_in_shop_items = 0
        max_position_in_shop_items = len(shop_items)
        def get_buttons(x, y, z):
            if x == 0:
                label  = "->"
            elif x == y:
                label  = "<-"
            else:
                if z == 1:
                    label = "->"
                elif z == 3:
                    label = "<-"
            return label
        while True:
            start_component =  ([Button(style=ButtonStyle.grey, label="->"), Button(style=ButtonStyle.grey, label="<-")])
            item = shop_items[position_in_shop_items]
            if item['sell_price'] is None:
                embed = discord.Embed(
                    title = "Shop",
                    description = f"""
    {item['emoji']}**{item['name']}**
    **Buy Price: `{item['buy_price']}`<:coins:872444592115568661>**
                    """
                )
            elif item['buy_price'] is None:
                embed = discord.Embed(
                    title = "Shop",
                    description = f"""
    {item['emoji']}**{item['name']}**
    **Sell Price: `{item['sell_price']}`<:coins:872444592115568661>**
                    """
                )
            else:
                embed = discord.Embed(
                    title = "Shop",
                    description = f"""
    {item['emoji']}**{item['name']}**
    **Buy Price: `{item['buy_price']}`<:coins:872444592115568661>**
    **Sell Price: `{item['sell_price']}`<:coins:872444592115568661>**
                    """
                )
            if position_in_shop_items == 0:
                temp = await ctx.send(embed=embed, components = [Button(style=ButtonStyle.grey, label=get_buttons(position_in_shop_items, max_position_in_shop_items, 1))])
            elif position_in_shop_items == max_position_in_shop_items:
                try:
                    await temp.edit(embed=embed, components = [Button(style=ButtonStyle.grey, label=get_buttons(position_in_shop_items, max_position_in_shop_items, 3))])
                except:
                    pass
            else:
                try:
                    await temp.edit(embed=embed, components = [[Button(style=ButtonStyle.grey, label="<-"), Button(style=ButtonStyle.grey, label="->")]])
                except:
                    pass


            response = await self.client.wait_for("button_click")
            if response.component.label == "->":
                position_in_shop_items +=1
            elif response.component.label == "<-":
                position_in_shop_items -=1

标签: pythondiscord.py

解决方案


Discord 期望对创建的交互做出响应,使用不和谐组件,这通过 Interaction.respond() 方法实现,该方法在此处记录。

您有几种响应方法,但如果您只是不想做任何事情,请将 6 作为类型参数。

如果您想做其他事情,这里记录了其他可能性


推荐阅读