Here is a code snippet of the class.
‘from discord import ButtonStyle, SelectOption, Embed
from discord.ui import Button, View
import discord
Import the necessary functions from akashasql.py
from akashasql import getchocobits, addchocobits
class StoreView(View):
def init(self):
super().init()
self.selected_item = None
async def fetch_user_chocobits(self, user_id):
# Fetch user's chocobits from the database
return getchocobits(user_id)
@discord.ui.select(
placeholder="Choose an item to buy...",
options=[
SelectOption(label="Test1", description="Costs 100 chocobits", value="100"),
SelectOption(label="Test2", description="Costs 200 chocobits", value="200"),
],
)
async def select_callback(self, select, interaction):
user_id = interaction.user.id
user_chocobits = await self.fetch_user_chocobits(user_id)
self.selected_item = select.values[0]
price = int(self.selected_item)
if user_chocobits < price:
await interaction.response.send_message("You do not have enough chocobits.", ephemeral=True)
self.stop()
else:
# Clear the select menu before adding a new item
self.clear_items()
self.add_item(Button(label="Confirm Buy", style=ButtonStyle.green))
# Update the message with the new view
await interaction.response.edit_message(content=f"Selected item costs {price} chocobits. Confirm purchase?", view=self)
@discord.ui.button(label="Confirm Buy", style=ButtonStyle.green)
async def confirm_callback(self, button, interaction):
user_id = interaction.user.id
price = int(self.selected_item)
addchocobits(user_id, -price) # Deduct the price from user's chocobits
user_chocobits = await self.fetch_user_chocobits(user_id) - price
await interaction.response.send_message(f"Purchase confirmed! You now have {user_chocobits} chocobits left.", ephemeral=True)
self.stop()
`
This is meant to put a embed in the discord server where uses can use “ChocoBis” to buy various items (test1 and test2). However when I select a item to buy, it gives me this error:
user_id = interaction.user.id
^^^^^^^^^^^^^^^^
AttributeError: ‘Select’ object has no attribute ‘user’
I have no clue whats going on
I expected it to go to the next screen, but unfortunately the bot is “Not responding” and the error message is still here.
This is my first time on stack exchange so sorry for the poor formatting
Chocolate Fudge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.