如何检查文本通道是否已在类别中?机器人的不和谐重写 API



我正在尝试在我的机器人上的 Discord 上制作票证功能,我想知道如何在某个类别中检查文本频道是否已经存在,如果存在,则不会创建票证。

@bot.command()
async def new(ctx):
    guild = ctx.message.guild
    channel = discord.utils.get(guild.categories, id=404351895121952768)
    if ctx.message.channel != bot.get_channel(402168280149655552):
        tag = await bot.get_channel(402168280149655552).send("{}".format(ctx.message.author.mention))
        wrongchannel_embed = discord.Embed(title="Error:",
                                           description="Use my commands in the {} channel.".format(tag.channel.mention),
                                           color=0xe73c24)
        await ctx.send(embed=wrongchannel_embed)
    elif discord.utils.get(guild.channels, name='{}-ticket'.format(ctx.message.author.name)):
        failed_embed = discord.Embed(title="Failed to create a ticket",
                                     description="You already have a ticket open, please don't try to open a ticket while you already have one.",
                                     color=0xe73c24)
        await ctx.send(embed=failed_embed)
    else:
        print(channel)
        print(guild.categories)
        overwrites = {
            guild.default_role: discord.PermissionOverwrite(read_messages=False),
            ctx.message.author: discord.PermissionOverwrite(read_messages=True)
        }
        ticket_create = await guild.create_text_channel(name='{}-ticket'.format(ctx.message.author.name),
                                                        overwrites=overwrites, category=channel)
        ticket_embed = discord.Embed(title="Ticket",
                                     description="{}nPlease be patient. A member of our support team will be with you shortly.".format(
                                         ctx.message.author.mention),
                                     color=0x15a513)
        ticket_embed.set_footer(text="Ticket requested by {}".format(ctx.message.author),
                                icon_url=ctx.message.author.avatar_url)
        await ticket_create.send(embed=ticket_embed)
        success_embed = discord.Embed(title="Ticket Creation",
                                      description="{}, your ticket was successfully created: {1}.".format(
                                          ctx.message.author.mention, ticket_create.mention),
                                      color=0x15a513)
        await ctx.send(embed=success_embed)

这是我创建票证的代码。我关注的路线是elif discord.utils.get(guild.channels, name='{}-ticket'.format(ctx.message.author.name)):我知道我在那里做错了什么。我也试过: elif "{}-ticket".format(ctx.message.author.name) in discord.utils.get(guild.channels, name="Tickets"):但它没有用。

知道我能做什么吗?

这可能是

一个区分大小写的问题:

elif discord.utils.get(channel.channels, name=f"{ctx.author.name.lower()}-ticket"):

我设法找到了解决方案。基本上发生的事情是我检查正确,但它正在用大写字母搜索命令发送者的名称,但票证的名称是小写的。所以我所做的是将elif discord.utils.get(channel.channels, name="{}-ticket".format(ctx.message.author.name))改为elif discord.utils.get(channel.channels, name="{}-ticket".format(ctx.message.author.name.lower()))

我希望这能帮助你们中的一些人。

最新更新