discord.py 赠品机器人问题

问题描述 投票:0回答:1

当前代码中的所有内容都很好,但在“您已经输入了此赠品!”消息中有一个“离开赠品”按钮,如果您已经输入了赠品,可以让您离开赠品。但问题是,当我按下“留下赠品”按钮时,它没有正确更新 🎉 按钮标签,我已经尝试了很多方法。有谁知道解决办法吗?

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

我原以为它会将我从标签中删除,但当我按下按钮时什么也没有发生。我也没有收到任何错误。

python discord discord.py
1个回答
0
投票

您可以更改机器人的标签,

py self.children[0].label = str(len(self.participants)) 
可以精确地操作
GiveawayView
机器人记忆中的设备。 Para que essa alteração surta efeito na mensagem do Discord, você precisa sincronizar a view, o que pode ser feito a mensagem do Discord.

    self.children[0].label = str(len(self.participants))
    # updating view in discord side
    await interaction.edit_original_response(view=self)

© www.soinside.com 2019 - 2024. All rights reserved.