我的命令不能使用`async def set` |不和谐.py

时间:2021-04-15 07:38:11

标签: python-3.x discord.py

基本上我已经创建了一个命令组,我想在该组中进行设置。基本上,每当有人输入 >prefix 时,都会显示一条消息,说它是命令,然后每当我使用 >prefix set 时,它基本上都会改变它的前缀。问题是我不能将 set 用于 async def。除此之外,我可以使用任何其他东西。为什么会这样,我该如何解决?

我的代码:

    @commands.group(invoke_without_command=True)
    @commands.has_permissions(administrator = True, manage_guild = True)
    @commands.cooldown(1, 3, commands.BucketType.user)
    async def prefix(self, ctx):
        
        try:
            x = ""
            prefix = await self.client.get_prefix(ctx)

            PrefixEmbed = discord.Embed(title = x, description = f"My Prefix for ***__{ctx.guild}__***, is `{prefix}`. For more Information, type `{prefix}Help`.\n\nAvailable Commands:```{prefix}prefix set\n{prefix}prefix reset```")
            PrefixEmbed.set_author(name = "ChizLanks Prefix System", icon_url = self.client.user.avatar_url)
            PrefixEmbed.set_footer(text = f"Guild's ID: {ctx.guild.id}", icon_url = ctx.guild.icon_url)
            PrefixEmbed.timestamp = datetime.datetime.utcnow()
            await ctx.reply(embed = PrefixEmbed)
        
        except discord.Forbidden:
            x = ""
            prefix = await self.client.get_prefix(ctx)

            PrefixEmbed = discord.Embed(title = x, description = f"My Prefix for ***__{ctx.guild}__***, is `{prefix}`. For more Information, type `{prefix}Help`.\n\nAvailable Commands:```{prefix}prefix set\n{prefix}prefix reset```")
            PrefixEmbed.set_author(name = "ChizLanks Prefix System", icon_url = self.client.user.avatar_url)
            PrefixEmbed.set_footer(text = f"Guild's ID: {ctx.guild.id}", icon_url = ctx.guild.icon_url)
            PrefixEmbed.timestamp = datetime.datetime.utcnow()
            await ctx.author.send(embed = PrefixEmbed)

    @prefix.error
    async def k_error(self, ctx, error):
        if isinstance(error, commands.MissingPermissions):
            try:
                x = ""
                prefix = await self.client.get_prefix(ctx)
                PrefixEmbed = discord.Embed(title = x, description = f"My Prefix for ***__{ctx.guild}__***, is `{prefix}`. For more Information, type `{prefix}Help`.")
                PrefixEmbed.set_author(name = "ChizLanks Prefix System", icon_url = self.client.user.avatar_url)
                PrefixEmbed.set_footer(text = f"Guild's ID: {ctx.guild.id}", icon_url = ctx.guild.icon_url)
                PrefixEmbed.timestamp = datetime.datetime.utcnow()
                await ctx.reply(embed = PrefixEmbed)
            except discord.Forbidden:
                x = ""
                prefix = await self.client.get_prefix(ctx)
                PrefixEmbed = discord.Embed(title = x, description = f"My Prefix for ***__{ctx.guild}__***, is `{prefix}`. For more Information, type `{prefix}Help`.")
                PrefixEmbed.set_author(name = "ChizLanks Prefix System", icon_url = self.client.user.avatar_url)
                PrefixEmbed.set_footer(text = f"Guild's ID: {ctx.guild.id}", icon_url = ctx.guild.icon_url)
                PrefixEmbed.timestamp = datetime.datetime.utcnow()
                await ctx.author.send(embed = PrefixEmbed)
        if isinstance(error, commands.CommandOnCooldown):
            await ctx.reply('You are being Rate Limited. Please, try again in `{:.2f}` Second(s).'.format(error.retry_after), delete_after = 5)

    @prefix.command()
    @commands.has_permissions(administrator = True, manage_guild = True)
    @commands.cooldown(1, 3, commands.BucketType.user)
    async def set(self, ctx, prefix):

        db = sqlite3.connect("main.sqlite")
        cursor = db.cursor()
        cursor.execute(f"SELECT prefix FROM prefixes WHERE guild_id = {ctx.guild.id}")
        result = cursor.fetchone()
        if result is None:
            sql = ("INSERT INTO prefixes(guild_id, prefix) VALUES(?,?)")
            val = (ctx.guild.id, prefix)
            await ctx.reply(f"Server Prefix has been set and saved as `{prefix}`")
        elif result is not None:
            sql = ("UPDATE prefixes SET prefix = ? WHERE guild_id = ?")
            val = (prefix, ctx.guild.id)
            await ctx.channel.send(f"Server Prefix has been updated and saved as `{prefix}`.")
        cursor.execute(sql, val)
        db.commit()
        cursor.close()
        db.close()

1 个答案:

答案 0 :(得分:0)

您可以随意命名函数,然后将 name kwarg 传递给装饰器

@prefix.command(name="set")
async def _set(self, ctx):
    ...

我无法在此处重现该问题,因此我无法告诉您为什么不能将名称设置为 set