#Pass Variable To View

1 messages · Page 1 of 1 (latest)

glad tusk
#

Hi all! I have this script here:

class MyView(discord.ui.View): # Create a class called MyView that subclasses discord.ui.View
    @discord.ui.button(label="Ban User", style=discord.ButtonStyle.primary, emoji=":hammer:") # Create a button with the label ":sunglasses: Click me!" with color Blurple
    async def button_callback(self, button, interaction):
        await interaction.response.send_message("You banned a user!") # Send a message when the button is clicked

How can I pass through the user object so that I can ban them? Thanks!

rain tapir
#

Add the user as a parameter in the init function of your class

#

and assign it as an attribute of self in your class

glad tusk
# rain tapir and assign it as an attribute of `self` in your class

I tried this:

class MyView(discord.ui.View): # Create a class called MyView that subclasses discord.ui.View
    def __init__(self,userID):
        self.userID = userID
    @discord.ui.button(label="Ban User", style=discord.ButtonStyle.primary, emoji=":hammer:") # Create a button with the label ":sunglasses: Click me!" with color Blurple
    async def button_callback(self, button, interaction):
        await interaction.response.send_message("Banning user!" + str(self.userID)) # Send a message when the button is clicked
#

With view being

await channel.send(f'{logs.user} has just banned {logs.target} (The time is {logs.created_at}), and their reason for doing so is {logs.reason}', view=MyView(member.id))
#

And I get this error: AttributeError: 'MyView' object has no attribute 'children'

rain tapir
#

Because you're overriding the init function you also need to call super().__init__()

glad tusk
#

ummm

#

So i just add at the end?

rain tapir
glad tusk
rain tapir
#

?tag idw

covert hatchBOT
#

Saying it doesn't work or asking what's wrong with this code? is not helpful for yourself or others.
Describe what you expect and/or tried (with your code), and what isn't going right.
Please provide any errors you get for optimal assistance.

glad tusk
#

AttributeError: 'NoneType' object has no attribute 'userID'

rain tapir
#

Show your code

glad tusk
#

class MyView(discord.ui.View): # Create a class called MyView that subclasses discord.ui.View
    def __init__(self,userID):
        super().__init__().userID = userID
        
    @discord.ui.button(label="Ban User", style=discord.ButtonStyle.primary, emoji="🔨") # Create a button with the label "😎 Click me!" with color Blurple
    async def button_callback(self, button, interaction):
        await interaction.response.send_message("Banning user!" + str(self.userID)) # Send a message when the button is clicked
await channel.send(f'{logs.user} has just banned {logs.target} (The time is {logs.created_at}), and their reason for doing so is {logs.reason}', view=MyView(member.id))
rain tapir
#

wtf

#

What is this?

glad tusk
#

I saw this: super().__init__() and figured you add at end idk

rain tapir
#

I think you need to learn more about OOP in Python. You're clearly just guessing at this point