#πŸ”’ How can i use a class variable to label a discord button?

62 messages Β· Page 1 of 1 (latest)

plush parcel
#

I have a dictionary

oracle = {
    "odds": {
        "note": "To get your answer, roll a die based on the odds of it being positive.",
        4: "d4 Very Unlikely",
        6: "d6 Unlikely",
        8: "d8 Likely",
        10: "d10 Very Likely",
        12: "d12 Almost Certain",
    },
    "interpret": {
        "1-2": "The answer is β€œNo, and...”",
        "3-4": "The answer is β€œYes, but...”",
        "5+": "The answer is β€œYes, and...”",
    },
}

and my ui.View class

class DiceChoice(discord.ui.View):
    def __init__(self, category: str):
        super().__init__()
        self.value = None

        # default case
        if category == "":
            self.category = {odds}

    @discord.ui.button(label="d4", style=discord.ButtonStyle.red)
    async def d4(
        self, button: discord.ui.Button, interaction: discord.Interaction
    ) -> None:
        self.value = str(d20.roll("d4").result)
        self.stop()

However if i try to use self in place of the "d4" in label= i get an error (screenshot)

pseudo baneBOT
#

@plush parcel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

plush parcel
#

currently testing with just

odds = {
    "note": "",
    4: "d4",
    6: "d6",
    8: "d8",
    10: "d10",
    12: "d12",
}
#

Is the decorator changing the scope to discord?

rare pewter
#

actually, the decorator is not in the method, so selfis not defined

#

to bypass this, you can create a class that inherits from discord.ui.Button, and use super().__init__(label) for example to put a custom label

#

You need to define and async callback method to this class, which will be called when the button will be clicked

plush parcel
rare pewter
#

wdym?

plush parcel
#

Using the @ symbol above my function

#

Hold on let me paste the whole file code

#

!code

pseudo baneBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

plush parcel
#

Err

#

!paste

pseudo baneBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

plush parcel
rare pewter
#

I'll check that

plush parcel
#

no rush

#

and thank you

rare pewter
#

ok I see

plush parcel
#

Goal:

When the user types /roll the default view shows up

When user types /roll [category name] i want to use the same view but change the labels

rare pewter
#

I think, by doing a custom class, it would be even more efficient

plush parcel
#

but use the same interface

#

Can you link me to a github example by chance?

#

but not new to python πŸ™‚

rare pewter
#

I'll do it quickly on a paste

plush parcel
#

cool

#

i guess it could even be a progressive embed using a dropdown

#

but i can figure that out later

#

if this doesnt pan out i'll just make a cog for each category haha

#

but i imagine theres a way, i just dont know enough

rare pewter
#

so

#

the command itself is really simple

#

it just sends an embed with a bunch of dice buttons

plush parcel
#

oh okay

#

and how would i show an embed to show these with the prompt?

rare pewter
#

where do you want to have the descriptors?

#

on the buttons, or in the embed?

plush parcel
#

i was originally thinking the buttons

#

but i see now

#

you're adding things to a view

rare pewter
#

yep

plush parcel
#

which i guess is different than a view class?

rare pewter
#

it is the same principle

#

and the same class

#

just not the same way to use it

#

but both work

plush parcel
#

yeah, okay, i'll see if i can refactor πŸ™‚

rare pewter
#

πŸ‘

#

ping me if you need some help

plush parcel
#

thank you so much @rare pewter

plush parcel
rare pewter
#

I should be online for around 6 hours I think

plush parcel
#

got it

rare pewter
#

and If I'm not, I can answer later

plush parcel
#

!solved

pseudo baneBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.