如何检查文本频道是否已属于某个类别? Discord重写API for Bot

时间:2018-01-21 15:15:17

标签: python discord discord.py discord.py-rewrite

我试图在我的机器人上使用Discord制作票证功能,我想知道如何在特定类别中检查文本频道是否已经存在,如果存在,那么票证赢了&#39 ;创造。

@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"): 但它没有用。

关于我能做什么的任何想法?

2 个答案:

答案 0 :(得分: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()))

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

答案 1 :(得分:0)

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

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