Kirill_Safe, [04.11.2024 19:55]
Помоги  с кодом, когда я выбираю "Добавить" и там предмет то он добавляется в embed а из кнопки dropdown он пропадает(так не должно быть(

def translate_to_russian(category, item):
    for key, value in names_data[category].items():
        if key.startswith(item):
            return value
    return item

def calculate_explosive(selected_items):
    results = {}

    for explosive, details in raid_data.items():
        if explosive in ['doors_hp', 'walls_hp']:
            continue

        total_amount_needed = 0

        for item_name, count in selected_items.items():
            item_health = (
                raid_data['doors_hp'].get(item_name, 0)
                if 'door' in item_name else raid_data['walls_hp'].get(item_name, 0)
            ) * count

            if 'door' in item_name:
                damage = details['damage']['doors'].get(item_name, 0)
            else:
                damage = details['damage']['walls'].get(item_name, 0)
            
            if damage:
                if explosive == "explosive_ammo":
                    amount_needed = (item_health + damage - 1) // damage
                    amount_needed = (amount_needed + 1) // 2
                else:
                    amount_needed = (item_health + damage - 1) // damage

                total_amount_needed += amount_needed
        
        if total_amount_needed > 0:
            results[explosive] = {
                "amount": total_amount_needed,
                "craft": {}
            }
            craft_info = details.get('craft', {})
            for resource, amount in craft_info.items():
                results[explosive]['craft'][resource] = amount * total_amount_needed

    return results

class RaidCalculatorView(discord.ui.View):
    
    def init(self):
        super().init()
        self.selected_items = {}
        self.add_select.options = [
            discord.SelectOption(label="Деревянная дверь", value="wood_door"),
            discord.SelectOption(label="Металлическая дверь", value="metal_door"),
            discord.SelectOption(label="Гаражка", value="garage"),
            discord.SelectOption(label="МВК дверь", value="hqm_door"),
            discord.SelectOption(label="Деревянная стенка", value="wood_wall"),
            discord.SelectOption(label="Каменная стенка", value="stone_wall"),
            discord.SelectOption(label="Металлическая стенка", value="metal_wall"),
            discord.SelectOption(label="МВК стенка", value="hqm_wall"),
        ]
        self.remove_select.options = [discord.SelectOption(label="Ничего", value="none")]

    @discord.ui.select(placeholder="Добавить")
    async def add_select(self, select, interaction):
        selected_item = select.values[0]
        
        if selected_item in self.selected_items:
            self.selected_items[selected_item] += 1
        else:
            self.selected_items[selected_item] = 1

        await self.update_select_options()
        await self.update_embed(interaction)

    @discord.ui.select(placeholder="Убрать")
    async def remove_select(self, select, interaction):
        selected_item = select.values[0]
        
        if selected_item in self.selected_items:
            if self.selected_items[selected_item] > 1:
                self.selected_items[selected_item] -= 1
            else:
                del self.selected_items[selected_item]
        
        await self.update_select_options()
        await self.update_embed(interaction)

    async def update_select_options(self):
        self.add_select.options = [
            discord.SelectOption(label=translate_to_russian('doors_ru' if 'door' in option.value else 'walls_ru', option.value), value=option.value)
            for option in self.add_select.options
            if option.value not in self.selected_items
        ]
        
        self.remove_select.options = [
            discord.SelectOption(label=translate_to_russian('doors_ru' if 'door' in item else 'walls_ru', item), value=item) 
            for item in self.selected_items
        ]

Kirill_Safe, [04.11.2024 19:55]
if not self.remove_select.options:
            self.remove_select.options.append(discord.SelectOption(label="Ничего", value="none"))

    async def update_embed(self, interaction):
        results = calculate_explosive(self.selected_items)
        
        embed = Embed(
            title="Калькулятор рейдов",
            description="\n".join(
                f"{translate_to_russian('doors_ru' if 'door' in item else 'walls_ru', item)} (x{count})"
                for item, count in self.selected_items.items()
            ),
            color=discord.Color.from_rgb(94, 174, 235),
            timestamp=datetime.now(pytz.timezone('Asia/Novosibirsk'))
        )
        
        for explosive, info in results.items():
            embed.add_field(
                name=f"{translate_to_russian('explosive_ru', explosive)} x{info['amount']}",
                value="\n".join(f"{translate_to_russian('crafting_ru', res)} x{amt}" for res, amt in info['craft'].items()),
                inline=True
            )
        
        await interaction.response.edit_message(embed=embed, view=self)

@bot.command()
async def calculate(ctx):
    view = RaidCalculatorView()
    embed = Embed(
        title="Калькулятор рейдов",
        description="Ничего не выбрано!",
        color=discord.Color.from_rgb(94, 174, 235),
        timestamp=datetime.now(pytz.timezone('Asia/Novosibirsk'))
    )
    await ctx.send(embed=embed, view=view)