1

I'm trying to make the variables position_of_page and position_in_inv defined for the all code.

The essence of my code is that an inventory is created and placed in embed, then I send a message using the send function and pass the embed and view arguments. In the last of them, a class is passed, the essence of which is to create buttons for inventory management under the message. These buttons should change the values of the variables position_of_page and position_in_inv, which allow you to navigate through the inventory.

But this, unfortunately, does not work, I get an error

Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ui\view.py", line 359, in _scheduled_task
    await item.callback(interaction)
  File "inventory.py", line 55, in up
    position_in_inv -= 1
NameError: name 'position_in_inv' is not defined

My code:

import discord

url = "https://i.imgur.com/GGbF3JQ.png"
async def info(msg, client, position_of_page = 0, position_in_inv = 0):
    gamer_inv = [{7:1}, {7:1}, {7:1}]
 
    while True:
        if position_of_page == 3:
            position_of_page = 0
        if position_of_page == -1:
            position_of_page = 2
        if position_in_inv == len(gamer_inv[position_of_page]):
            position_in_inv = 0
        if position_in_inv == -1:
            position_in_inv = len(gamer_inv[position_of_page]) - 1
 
        sign = ['Items', 'Ingridients', 'Potions']
 
        page = ''.join(str('' if i[0] == position_of_page else '') for i in tuple(enumerate('' * 3)))
 
        items = ''
        for pos, i in enumerate(gamer_inv[position_of_page].items()):
            if position_in_inv == pos:
                items += f'**{i[0]}** - {i[1]}\n'
            else:
                items += f'{i[0]} - {i[1]}\n'
        embed = discord.Embed(title=f"Bag {str(msg.author.name)}\n",
        description = page + f'\n__{sign[position_of_page]}__:\n'+ items, 
        color = 0xFFBA00)
 
        embed.set_thumbnail(url=url)

        class Buttons(discord.ui.View):
            @discord.ui.button(label='', style=discord.ButtonStyle.secondary)
            async def slide_left(self, button: discord.ui.Button, interaction: discord.Interaction):
                global position_of_page
                global position_in_inv
                position_of_page -= 1
                position_in_inv = 0
        
            @discord.ui.button(label='', style=discord.ButtonStyle.secondary)
            async def slide_right(self, button: discord.ui.Button, interaction: discord.Interaction):
                global position_of_page
                global position_in_inv
                position_of_page += 1
                position_in_inv = 0
        
            @discord.ui.button(label='', style=discord.ButtonStyle.secondary)
            async def up(self, button: discord.ui.Button, interaction: discord.Interaction):
                global position_in_inv
                position_in_inv -= 1
        
            @discord.ui.button(label='', style=discord.ButtonStyle.secondary)
            async def down(self, button: discord.ui.Button, interaction: discord.Interaction):
                global position_in_inv
                position_in_inv += 1
 
        try:
            await message.edit(embed = embed, view = Buttons())
        except Exception:     
            message = await msg.channel.send(embed = embed, view = Buttons())  
alxbavy
  • 160
  • 2
  • 11
  • You didn't declare the variables as `global` in the outer function. – kaya3 Aug 10 '21 at 22:07
  • You're getting the error because you don't define your `position*` variables until after you try to use them. – MattDMo Aug 10 '21 at 22:07
  • You should normally define classes at top level anyway. What problem are you trying to solve doing things this way? – Karl Knechtel Aug 10 '21 at 22:11
  • When dealing with nested functions (which in this context means the methods of the class defined within the outer function), you can also use `nonlocal` to have a variable refer to a variable in the enclosing namespace. – Blckknght Aug 10 '21 at 22:13
  • Karl Knechtel, I don't know, I thought it would help. In general, I'm trying to make it so that clicking on the button changes the value of variables in the entire code, so that I can access them from the entire code. – alxbavy Aug 10 '21 at 22:14
  • Blckknght, I didn't understand a little. How to implement it? – alxbavy Aug 10 '21 at 22:16

0 Answers0