I am trying to create some buttons with Discord.Py where the labels change. I’ve created a discord.ui.View
and then inside that I have an __init__
which accepts a name
and id
value. When I call the class
, and add a break point on that I can see that the values are assigned to self.modelName
and self.modelId
, however, the button generated completely ignores these values, and uses the default values.
My class is defined as the following:
class AlphaStrikeCardButtons(discord.ui.View):
modelName = "supernova 2"
modelId = "3133"
#defining constructor
def __init__(self,name,id):
super().__init__()
self.modelName = name
self.modelId = f'asc{id}'
@discord.ui.button(label=modelName, custom_id=modelId, style=discord.ButtonStyle.blurple)
#@discord.ui.button(label='supernova 5', custom_id='asc8132', style=discord.ButtonStyle.blurple)
async def alphaStrikeCard(self, interaction: discord.Interaction, button: discord.ui.Button):
cardPath = f'./tmp/asc{modelId}.png' #If it already exists, we don't need to download it again
if os.path.isfile(cardPath) == False:
cardUrl = f'http://masterunitlist.info/Unit/Card/{self.modelId}'
print(cardUrl)
urlretrieve(cardUrl,cardPath)
cardFile = discord.File(cardPath,filename=f'{self.modelId}.png')
await interaction.response.send_message(file=cardFile, ephemeral=False)
self.stop()
Then then I call the class using:
ASCB = AlphaStrikeCardButtons(name=modelName,id=modelId)
await interaction.response.send_message("Please click the button:",ephemeral=True,view=ASCB)
The values of modelName
and modelId
in are 'Supernova 5'
and '8132'
, however, the button prompted displayed “Supernova 2” and has a custom_id
of asc3133
, which is the default values. I confirmed the values are assigned by adding a breakpoint and hovering over the variable:
You can clearly see that self.modelName
has the value Supernova 5
; so it was correctly assigned.
If I don’t assign values (assign None
) to modelName
and modelId
at the start then when I call the class I get an even more confusing error:
In data.components.0.components.0.label: This field is required
So it’s saying I’m not even passing the value, but I most certainly am.
Why is Python ignoring the values set to the variables modelName
and modelId
when I’ve confirmed that the values are being assigned?