python 按下按钮时尝试检查文本通道是否已存在

enyaitl3  于 2023-01-01  发布在  Python
关注(0)|答案(1)|浏览(114)

我有一个创建按钮的命令,该按钮应该为您和目标角色(通常是mods)创建一个新的私有文本通道,在第二次单击它时,我希望它确认现有的文本通道(因为它是以按钮点击器命名的),这样它就不会创建重复。
我的问题是,它不承认重复。
以下是按钮的设置

class MyView(discord.ui.View): 
    def __init__(self):
        super().__init__(timeout=None)
        self.value = None

    @discord.ui.button(label="Create new private channel", style=discord.ButtonStyle.gray)
    async def menu1(self, interaction: discord.Interaction, button: discord.ui.Button):
        
        guild = interaction.guild
        targeted_role = guild.get_role(1056649402330132551)
        
        overwrites = {
         guild.default_role: discord.PermissionOverwrite(read_messages=False),
         targeted_role: discord.PermissionOverwrite(read_messages=True),
         interaction.user: discord.PermissionOverwrite(read_messages=True)
        }

        name = "Bot-made channel for " + interaction.user.__str__()
        cat = discord.utils.get(guild.categories, name = 'brendan')
        all_channels = interaction.channel.category.channels

        channel_exists = discord.utils.get(interaction.channel.category.text_channels, name= name)
        if not channel_exists:
            await guild.create_text_channel(name= "Bot-made channel for " + interaction.user.name, category=cat, overwrites=overwrites)
        else:
            await interaction.response.send_message("You have already created a private channel.", ephemeral = True)

在这段代码中,我一直在纠结于检查通道的逻辑,我想这可能是因为它不承认在同一个按钮上创建了新通道,但调试清楚地表明,.get()检索到的所有文本通道总是在更新。

channel_exists = discord.utils.get(interaction.channel.category.text_channels, name= name)
        if not channel_exists:
            await guild.create_text_channel(name= "Bot-made channel for " + interaction.user.name, category=cat, overwrites=overwrites)
        else:
            await interaction.response.send_message("You have already created a private channel.", ephemeral = True)

我在讨论简单地撤销点击按钮的用户对按钮原始频道的查看权限。
无论如何,我是Python的新手,所以任何的建议都将不胜感激。谢谢你的时间。

ds97pgxw

ds97pgxw1#

第一个问题是交互参数在按钮参数之后,所以async def menu1(self, interaction: discord.Interaction, button: discord.ui.Button):应该是async def menu1(self, button: discord.ui.Button, interaction: discord.Interaction):
此外,不一致的文本频道是小写的,不包含空格,这就是为什么当你搜索频道时,它没有给予任何东西。
下面是修改后的代码,展示了我将如何修复它。我确实测试了它,它工作正常。我还让它发送一条消息,提到频道/现有频道,但如果你想删除它。

class MyView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)
        self.value = None

    @discord.ui.button(
        label="Create new private channel", style=discord.ButtonStyle.gray
    )
    async def menu1(self, button: discord.ui.Button, interaction: discord.Interaction):
        ROLE = 1056649402330132551
        CATEGORY_NAME = "brendan"

        guild = interaction.guild
        targeted_role = guild.get_role(ROLE)

        overwrites = {
            guild.default_role: discord.PermissionOverwrite(read_messages=False),
            targeted_role: discord.PermissionOverwrite(read_messages=True),
            interaction.user: discord.PermissionOverwrite(read_messages=True),
        }

        name = f"Bot made channel for {interaction.user.name}"
        name = name.lower().replace(" ", "-") # because discord channels are lowercase no spaces

        cat = discord.utils.get(guild.categories, name=CATEGORY_NAME)  # get category
        channel_exists = discord.utils.get(cat.channels, name=name) # check if channel with same name exists in category

        if channel_exists is None:  # if it doesnt exist create channel
            channel = await guild.create_text_channel(
                name=name,
                category=cat,
                overwrites=overwrites,
            )
            await interaction.response.send_message(
                f"A channel has been made for you: {channel.mention}",
                ephemeral=True,
            )

        # if it does exist just tell user to use that one
        await interaction.response.send_message(
            f"You have already have a private channel: {channel_exists.mention}",
            ephemeral=True,
        )

我还有一个建议是,不要使用用户名interaction.user.name,你应该考虑使用他们的ID。这是因为他们的用户可以改变他们的名字,但不能改变他们的不一致ID。

相关问题