【发布时间】: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