【问题标题】:How can I check if a text channel is already in a category? Discord Rewrite API for Bot如何检查文本频道是否已在某个类别中?机器人的 Discord 重写 API
【发布时间】:2018-06-30 07:11:37
【问题描述】:

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

知道我能做什么吗?

【问题讨论】:

  • 您遇到的具体问题是什么?当您尝试运行代码时是否遇到错误,您只是没有看到任何结果...
  • @qspitzer 设法找到了解决方案。基本上发生的事情是即使我正在检查类别中的票证名称并告诉它如果找到具有该名称的票证不要创建具有特定名称的新票证,它仍在创建具有该名称的文本通道我不想要它。我设法通过将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())) 来修复它

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2021-01-24
      • 2021-11-03
      • 2019-04-24
      相关资源
      最近更新 更多