#Calling a variable

1 messages · Page 1 of 1 (latest)

bright ingot
#

I wanted short text to be callabe at the callback function

@modal_callback("my_modal")
async def on_modal_answer(ctx: ModalContext, short_text: str):
    channel = await bot.fetch_channel(channel_id=1056842044376621086)
    await ctx.send("Your nickname request has been submitted", ephemeral=True)
    await channel.send(f"Nickname Request: {short_text}\nUser: {ctx.user.mention}")

    button2: list[ActionRow] = [
        ActionRow(
            Button(
                custom_id="approved",
                style=ButtonStyle.GREEN,
                label="Approved",
            ),
            Button(
                custom_id="decline",
                style=ButtonStyle.RED,
                label="Decline",
            )
        )
    ]

    await channel.send(f"Nickname Request: {short_text}\nUser: {ctx.user.mention}", components=button2)

@component_callback("approved")
async def approved_callback(ctx: ComponentContext):
    member = ctx.guild.members
    await member.modify_current_user_nick(guild, nickname=short_text)
    user = bot.get_user(ctx.user.id)
    await user.send("Nickname Updated!")

how can i do that?

bleak chasmBOT
#

Hey! Once your issue is solved, press the button below to close this thread!

bleak chasmBOT
#

Modals are a great way of gathering user input in a form-like manner. Check out this guide for how to use them.

bright ingot
#

@worthy gorge I mean i couldn't get the parameters of my modal function, I'm trying to call it out on my button callback. here's the full code

@prefixed_command(name="name")
async def name(ctx: PrefixedContext):
    button = Button(
    custom_id="nick",
    style=ButtonStyle.GREEN,
    label="Click Me",
    )

    await ctx.send("Click me to change nickname!", components=button)

@component_callback("nick")
async def my_callback(ctx: ComponentContext):
    my_modal = Modal(
        ShortText(label="Nickname", custom_id="short_text"),
        title="Change nickname",
        custom_id="my_modal"
    )
    await ctx.send_modal(modal=my_modal)

@modal_callback("my_modal")
async def on_modal_answer(ctx: ModalContext, short_text: str):
    channel = await bot.fetch_channel(channel_id=1028295120665190410)
    await ctx.send("Your nickname request has been submitted", ephemeral=True)

    button2: list[ActionRow] = [
        ActionRow(
            Button(
                custom_id="approved",
                style=ButtonStyle.GREEN,
                label="Approved",
            ),
            Button(
                custom_id="decline",
                style=ButtonStyle.RED,
                label="Decline",
            )
        )
    ]

    users = ctx.user.id
    await channel.send(f"Nickname Request: {short_text}\nUser: {ctx.user.mention}", components=button2)

@component_callback("approved")
async def approved_callback(ctx: ComponentContext):
    member = guild.get_user(users)
    await member.edit(nickname=nickname)
    user = bot.get_user(ctx.user.id)
    await user.send("Nickname Updated!")
#

I mean is like, how can i make the short_text parameter accesable in the button callback?

halcyon dust
#

You could use bot.wait_for_modal to get the modal result inside of the command callback, which would let you keep access to any variables in there

halcyon dust
#

It's explained a bit in the docs link above

bright ingot
verbal garnet