python - 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
解决方案
推荐阅读
- android - 从 android 服务中的 get SharedPreferences 中获取数据
- python - 将希腊语 HTML 字母转录到 LaTex 时出现问题
- javascript - 修改输入值后如何复制表行并追加
- javascript - 如何在另一个动态div中的动态li中获取span的第一个值
- c# - 使用谷歌地图 api 密钥的 C# Rest 服务在本地工作,但不在服务器上
- ruby-on-rails - Rails 中无限时间范围的问题
- python - 找到一个变量的最大值和最小值并找到对应的值
- r - 如何在R中的同一数据框中合并2列
- android - 如何从另一个类更新片段的用户界面?
- css - 如何在现有的圆形边框周围创建多个边框