discord.py Giveaway Bot Issue

Everything currently in the code is fine, but on the “You’ve already entered this giveaway!” message there is a “Leave Giveaway” button that lets you leave the giveaway if you already entered it. But the problem is when I press the “Leave Giveaway” button It does not update the 🎉 button label correctly, I’ve tried many thing. Does anybody know a fix?

class GiveawayView(discord.ui.View):
    def __init__(self, required_messages=None, required_role=None, host=None):
        super().__init__(timeout=None)
        self.participants = {}
        self.required_messages = required_messages
        self.required_role = required_role
        self.host = host

    @discord.ui.button(label="0", emoji="🎉", style=discord.ButtonStyle.blurple, custom_id="enter_giveaway")
    async def enter_giveaway(self, interaction: discord.Interaction, button: discord.ui.Button):
        user = interaction.user

        if user.id in self.participants:
            leave_button = discord.ui.Button(label="Leave Giveaway", style=discord.ButtonStyle.red, custom_id="leave_giveaway")
            leave_button.callback = self.leave_giveaway
            await interaction.response.send_message(
                "You've already entered this giveaway!",
                view=discord.ui.View().add_item(leave_button),
                ephemeral=True
            )
            return

        if self.required_role and self.required_role not in user.roles:
            await interaction.response.send_message(f"You must have the {self.required_role.mention} role to enter!", ephemeral=True)
            return

        if self.required_messages:
            user_message_count = await self.count_user_messages_today(interaction.channel, user)
            if user_message_count < self.required_messages:
                await interaction.response.send_message(f"You must have sent at least {self.required_messages} messages today to enter!", ephemeral=True)
                return

        self.participants[user.id] = user
        button.label = str(len(self.participants))
        await interaction.response.edit_message(view=self)
        await interaction.followup.send("🎉 Giveaway successfully entered!", ephemeral=True)

    @discord.ui.button(label="Participants", emoji="👥", style=discord.ButtonStyle.gray, custom_id="view_participants")
    async def view_participants(self, interaction: discord.Interaction, button: discord.ui.Button):
        if not self.participants:
            await interaction.response.send_message("No participants have entered yet!", ephemeral=True)
            return

        participants_list = list(self.participants.values())
        participant_mentions = "n".join([f"{participant.mention} (ID: {participant.id})" for participant in participants_list])

        embed = discord.Embed(title="Giveaway Participants", description=participant_mentions or "No participants yet")
        await interaction.response.send_message(embed=embed, ephemeral=True)

    async def leave_giveaway(self, interaction: discord.Interaction):
        user = interaction.user

        if user.id not in self.participants:
            await interaction.response.send_message("You are not in this giveaway.", ephemeral=True)
            return

        del self.participants[user.id]
        self.children[0].label = str(len(self.participants))

        leave_button = discord.ui.Button(label="Leave Giveaway", style=discord.ButtonStyle.red, disabled=True)
        leave_view = discord.ui.View(timeout=0)
        leave_view.add_item(leave_button)

        await interaction.response.edit_message(view=leave_view)
        await interaction.followup.send("🎉 You have left the giveaway!", ephemeral=True)

    async def count_user_messages_today(self, channel: discord.TextChannel, user: discord.Member):
        count = 0
        today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
        async for message in channel.history(limit=None, after=today):
            if message.author == user:
                count += 1
        return count

I expected it to just remove me from the label but nothing happened when I press the button. I am also not getting any error.

New contributor

disrangedcoder64 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật