1

我正在尝试在我的机器人上的 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"): 但它没有用。

知道我能做什么吗?

4

2 回答 2

0

我设法找到了解决方案。基本上发生的事情是我正确地检查了它,但它正在用大写字母搜索命令发送者的名字,但票的名字是小写的。所以我所做的就是改变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()))

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

于 2018-01-22T16:18:17.377 回答
0

这可能是一个区分大小写的问题:

elif discord.utils.get(channel.channels, name=f"{ctx.author.name.lower()}-ticket"):
于 2020-05-17T10:21:33.867 回答