【问题标题】:discord.py discord_components interaction faileddiscord.py discord_components 交互失败
【发布时间】:2021-12-20 08:44:06
【问题描述】:

我想做一个不和谐的机器人,他的功能之一是商店, 对于商店菜单,我使用了不和谐组件,它循环到用 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

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

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

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

    如果您想做其他事情,还有其他可能性记录在 here

    【讨论】:

    • 在 2.0 版本中的不和谐组件 InteractionType 被删除并且py response = await self.client.wait_for("button_click") if response.component.label == "-&gt;": position_in_shop_items +=1 await response.send(type=1) elif response.component.label == "&lt;-": position_in_shop_items -=1 不起作用
    • await response.respond(type=1) 这就是你必须这样做,你读错了文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2022-01-19
    • 2023-03-16
    • 2011-08-23
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多