首页 > 解决方案 > 分配前引用的 pagination_needed

问题描述

    @commands.command()
    async def tags(self, ctx):
        global names
        names = []
        async with aiosqlite.connect('s137_guild_values') as db:
            async with db.execute('SELECT name FROM tag_list WHERE guild_id = ?', (ctx.guild.id,)) as cursor:
                async for row in cursor:
                    value = row
                    value = list(value)
                    names.append(value[0])
        if len(names) != 0:
            STOP = False
            global pagination_dict
            pagination_dict = {}
            global amount_tags
            amount_tags = len(names)
            for x in range(1, self.max_pages):
                if (math.ceil(amount_tags / x) == self.pagination_value):
                    needed_pagination = x
                    break
            current_page = 1
            var = 1
            counter = 0
            counter_all = 0
            counter_all_eq = len(names)
            index = 0
            while True:
                pagination_dict['Page {}'.format(var)] = {}
                msg = ""
                for x in range(0, (len(names))):
                    msg += '• {}\n'.format(names[index])
                    names.remove(names[index])
                    counter += 1
                    counter_all += 1
                    if (len(names) == 0) and (counter != self.pagination_value):
                        counter = self.pagination_value
                    if counter == self.pagination_value:
                        counter = 0
                        pagination_dict['Page {}'.format(var)]['Content'] = msg
                        msg = ""
                        var += 1
                        break
                if (var >= needed_pagination) and (counter_all == counter_all_eq):
                    break
            txt = "Current Page: {}/{}".format("1", needed_pagination)
            embed = discord.Embed(title="Tags ({})".format(amount_tags), description=pagination_dict['Page {}'.format(str(current_page))]['Content'], timestamp=ctx.message.created_at, colour=discord.Colour.blue())
            embed.set_footer(text=txt)
            msg = await ctx.send(embed=embed)
            for ele in self.emojis:
                await msg.add_reaction(ele)
            async def Main(ctx, msg, needed_pagination, current_page, STOP, embed, txt):
                current_page = 1
                STOP = False
                while STOP == False:
                    def check(reaction, user):
                        return (str(reaction.emoji) and user == ctx.author) and (msg.id == reaction.message.id)

                    try:
                        reaction, user = await self.client.wait_for('reaction_add', timeout=60.0, check=check)
                    except asyncio.TimeoutError:
                        await msg.delete()
                        await ctx.message.delete()
                        STOP = True
                    if reaction.emoji == '⏪':
                        if current_page != 1:
                            current_page -= 1
                            embed = discord.Embed(title="Tags ({})".format(amount_tags), description=pagination_dict['Page {}'.format(current_page)]['Content'], timestamp=ctx.message.created_at, colour=discord.Colour.blue())
                            txt = "Current Page: {}/{}".format(current_page, needed_pagination)
                            embed.set_footer(text=txt)
                            await msg.edit(embed=embed)
                            await msg.remove_reaction(reaction, ctx.author)
                        else:
                            await msg.remove_reaction(reaction, ctx.author)
                    elif reaction.emoji == '⏩':
                        if current_page != (needed_pagination):
                            current_page += 1
                            embed = discord.Embed(title="Tags ({})".format(amount_tags), description=pagination_dict['Page {}'.format(current_page)]['Content'], timestamp=ctx.message.created_at, colour=discord.Colour.blue())
                            txt = "Current Page: {}/{}".format(current_page, needed_pagination)
                            embed.set_footer(text=txt)
                            await msg.edit(embed=embed)
                            await msg.remove_reaction(reaction, ctx.author)
                        else:
                            await msg.remove_reaction(reaction, ctx.author)
                    elif reaction.emoji == '⏹️':
                        await msg.delete()
                        await ctx.message.delete()
                        STOP = True
            x = 0
            if x == 0:
                future = asyncio.ensure_future(Main(ctx, msg, needed_pagination, current_page, False, embed, txt))
                x += 1
        else:
            await ctx.send("There Are No Tags On This Server!")

我自己编写了这一切,但是我遇到了一个问题,它说在分配之前引用了“pagination_needed”。

我不知道它为什么这么说。

一段时间以来,我一直试图找出问题所在,所以如果有人知道我会很高兴。我很想尽快开始尝试一些方法来解决这个问题,因为我一直渴望让这个命令再次工作。所以任何建议都会很棒。

标签: pythonvariablesdiscorddiscord.pypython-3.8

解决方案


我通过添加两个不同的功能来修复它。

一个在结果上加 1 math.ceil,另一个不加。

这行得通。


推荐阅读