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())