Discord.py-创建类别(如果不存在)

时间:2020-09-29 16:08:23

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

如果类别不存在,我正在尝试创建一个类别。我曾尝试通过数据库进行此操作,但问题是我无法在该类别内创建文本通道,或者无法获取类别的ID,因为它位于元组而不是int。

因此,每当我对表情符号做出反应时,它将在TEXT-CHANNEL上由机器人创建的类别on_reaction_add()中创建一个APPLICATIONS,这是我当前的代码:< / p>

class Changelog(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print('Application is loaded')

    @commands.group(invoke_without_command=True)
    async def application(self, ctx):
        embed = discord.Embed(title="Application Commands",
                              description="Channel: <#channel>", color=0)
        await ctx.send(embed=embed)

    @application.command()
    async def channel(self, ctx, channel: discord.TextChannel):
        if ctx.message.author.guild_permissions.administrator:

            db = sqlite3.connect('main.sqlite')
            cursor = db.cursor()
            cursor.execute(
                f'SELECT channel_id FROM application WHERE guild_id = {ctx.guild.id}')
            result = cursor.fetchone()
            if result is None:
                sql = ('INSERT INTO application(guild_id, channel_id) VALUES(?,?)')
                val = (ctx.guild.id, channel.id)
                await ctx.send(f'Message has been sent and channel has been set to {channel.mention}')
            elif result is not None:
                sql = ('UPDATE application SET channel_id = ? WHERE guild_id = ?')
                val = (channel.id, ctx.guild.id)
                await ctx.send(f'Message has been sent and channel has been updated to {channel.mention}')
            cursor.execute(sql, val)
            db.commit()
            youtube = ':play_pause:'
            staff = ':envelope_with_arrow:'
            embed = discord.Embed(title="ReefCraft Applications", color=0)
            embed.add_field(
                name="** **", value=f"{youtube} YouTube Application\n\n{staff} Staff Application", inline=False)
            embed.add_field(name="\n\nInformation",
                            value="Reacting to one of the emotes will create a new text-channel, where you will write your applicaiton!")
            reaction_message = await channel.send(embed=embed)
            for emoji in emojis:
                await reaction_message.add_reaction(emoji)
            cursor.close()
            db.close()

    @commands.Cog.listener()
    async def on_reaction_add(self, reaction, user):
        emoji = reaction.emoji
        db = sqlite3.connect('main.sqlite')
        cursor = db.cursor()
        cursor.execute(f'SELECT channel_id FROM application WHERE 1')
        channel = cursor.fetchone()
        channel_id = self.client.get_channel(int(channel[0]))
        #category = cursor.fetchone()
        #category_id = category[0]
        guild = user.guild
        categoryTest = None
        if categoryTest is None:
            categoryTest = await user.guild.create_category('Testing')
        elif categoryTest is not None:
            pass

        if user.bot:
            return

        if emoji == "\U0001F4E9":
            await channel_id.send("You clicked the Staff Application")
            print(categoryTest)
            await guild.create_text_channel("Staff", category=categoryTest)
        elif emoji == "\U000023EF":
            await channel_id.send("You clicked the Youtube Application")
        else:
            return


def setup(client):
    client.add_cog(Changelog(client))

1 个答案:

答案 0 :(得分:0)

您可以使用discord.utils.get查找指定的类别

    guild = ctx.guild

    category = discord.utils.get(guild.categories, name='NAME')

    if category is None: #If there's no category matching with the `name`
        await guild.create_category('NAME') #Creates the category

    else: #Else if it found the categoty
        await ctx.send('Found') #Dosent make the category, instead it pass the creation of the category

discord.utils.get文档