#Modal Options

1 messages · Page 1 of 1 (latest)

copper geyser
#

Hello, how can I make it so that an option is not mandatory?

Im sorry my english is very bad.

#

I've already tried Google, but I couldn't find anything

#

Is that possible?

#

Happy to ping

mental coral
#

async def abc(ctx, choice = Option(str, 'Wow this is so cool))

#

I think

copper geyser
#

Which option?

mental coral
#

?!

#

I corrected the code slightly, my apologies.

copper geyser
#

all good does that come from the callback or the command?

mental coral
#

b!rtfm pycord Option

mental coral
#

second one

mental coral
copper geyser
#

Thank you anyway..

coral mountain
copper geyser
#

what goes in choice?

#

and thank you for your message

coral mountain
#

the user would input any random string they want in the case of my example

#

you can also set it to take different types like ints, discord.Member, etc.

copper geyser
#

Unfortunately didn't help..

coral mountain
#

oh you meant modals

#

my bad didnt read the title of the post lmao

copper geyser
#

ye

#

im sorry, my english is very very bad

#

i make all to google translator

coral mountain
#
test = discord.ui.InputText(
  style=discord.InputTextStyle.short,
  label="Title",
  required=False
)
#

this should work?

copper geyser
#

I try

#

It worked thanks!

copper geyser
coral mountain
#

what do you mean by watermark?

copper geyser
#

so yk?

coral mountain
#

inside discord.ui.InputText set the placeholder field

#

so

#
test = discord.ui.InputText(
  style=discord.InputTextStyle.short,
  label="Title",
  required=False,
  placeholder="Test"
)
copper geyser
#

thanks

#

you are the best thank you

copper geyser
#

Really really last question can you put modals in cogs?

#

Is that possible?

paper void
#

yes you can

copper geyser
#

So, `class MyModal(discord.ui.Modal):
def init(self, *args, **kwargs) -> None:
super().init(*args, **kwargs)

    self.add_item...` ?
paper void
#

nothing needs to change in the modal class

copper geyser
paper void
#

yeah

#

and then just import it (if it's in another file) in your cog and use it like normal

copper geyser
#

Ok thanks 😄

paper void
#

you're welcome 🙂

copper geyser
#

Hey im sorry what is wrong with the setup?

The Error:

Traceback (most recent call last):
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\cog.py", line 723, in _load_from_module_spec
    setup(self)
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\extensions\Modal.py", line 25, in setup
    bot.add_cog(Modals(bot))
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\ui\modal.py", line 47, in __init__
    loop = asyncio.get_running_loop()
RuntimeError: no running event loop

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\main.py", line 14, in <module>
    bot.load_extension(f"extensions.{filename[:-3]}")
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\cog.py", line 783, in load_extension
    self._load_from_module_spec(spec, name)
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\cog.py", line 728, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.errors.ExtensionFailed: Extension 'extensions.Modal' raised an error: RuntimeError: no running event loop

Process finished with exit code 1
#

The Code

#

i have copy paste made to test

I'm really sorry if I'm annoying you

thin flax
#

Modal Options

paper void
#

Modals isn't a cog

#

and you're trying to add it to your bot as a cog

copper geyser
paper void
#

you can just have both classes in the same file if you want

#

but you need a cog class

copper geyser
paper void
#
class Modals(...):
    pass
class MyCog(...):
    pass```
#

make them both top level indentation

copper geyser
#

and then I can start the bot again? so is it a cog?

paper void
#

yes

copper geyser
paper void
#

do you already have a cog?

#

just put the Modals class in your cog file

copper geyser
#

Ok and?

#

then*

#

I try it easy

copper geyser
# paper void just put the Modals class in your cog file

So i have this:

import discord
from discord.ext import commands
from discord.commands import slash_command, Option


class Modals(discord.ui.Modal):
    def __int__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        self.add_item(discord.ui.InputText(label="Short Input"))
        self.add_item(discord.ui.InputText(label="Long Input", style=discord.InputTextStyle.long))

    async def callback(self, interaction: discord.Interaction):
        embed = discord.Embed(title="Modal Results")
        embed.add_field(name="Short Input", value=self.children[0].value)
        embed.add_field(name="Long Input", value=self.children[1].value)
        await interaction.response.send_message(embeds=[embed])


def setup(bot):
    bot.add_cog(Modals(bot))
paper void
#

that's not a cog

copper geyser
#

the error is still there

copper geyser
paper void
#

you're trying to add a Modal to the bot when it's expecting a Cog

#
import discord
from discord.ext import commands
from discord.commands import slash_command, Option


class Modals(discord.ui.Modal):
    def __int__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        self.add_item(discord.ui.InputText(label="Short Input"))
        self.add_item(discord.ui.InputText(label="Long Input", style=discord.InputTextStyle.long))

    async def callback(self, interaction: discord.Interaction):
        embed = discord.Embed(title="Modal Results")
        embed.add_field(name="Short Input", value=self.children[0].value)
        embed.add_field(name="Long Input", value=self.children[1].value)
        await interaction.response.send_message(embeds=[embed])

class MyCog(commands.Cog):
    pass


def setup(bot):
    bot.add_cog(MyCog(bot))```
paper void
copper geyser
paper void
#

sure

copper geyser
# paper void sure

and the slash command? Can he still go in to run the modal?

import discord
from discord.ext import commands
from discord.commands import slash_command, Option


class Modals(discord.ui.Modal):
    def __int__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        self.add_item(discord.ui.InputText(label="Short Input"))
        self.add_item(discord.ui.InputText(label="Long Input", style=discord.InputTextStyle.long))

    async def callback(self, interaction: discord.Interaction):
        embed = discord.Embed(title="Modal Results")
        embed.add_field(name="Short Input", value=self.children[0].value)
        embed.add_field(name="Long Input", value=self.children[1].value)
        await interaction.response.send_message(embeds=[embed])

    @slash_command()
    async def lolmodal(self, ctx):
        the_modal = Modals(title="Test Modal")
        await ctx.send_modal(the_modal)



class MyCog(commands.Cog):
    pass


def setup(bot):
    bot.add_cog(MyCog(bot))
paper void
#

no, the slash command goes in the cog

copper geyser
#

how?

#

what do you mean?

paper void
#
import discord
from discord.ext import commands
from discord.commands import slash_command, Option


class Modals(discord.ui.Modal):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        self.add_item(discord.ui.InputText(label="Short Input"))
        self.add_item(discord.ui.InputText(label="Long Input", style=discord.InputTextStyle.long))

    async def callback(self, interaction: discord.Interaction):
        embed = discord.Embed(title="Modal Results")
        embed.add_field(name="Short Input", value=self.children[0].value)
        embed.add_field(name="Long Input", value=self.children[1].value)
        await interaction.response.send_message(embeds=[embed])


class MyCog(commands.Cog):
    @slash_command()
    async def lolmodal(self, ctx):
        the_modal = Modals(title="Test Modal")
        await ctx.send_modal(the_modal)


def setup(bot):
    bot.add_cog(MyCog(bot))```
copper geyser
# paper void ```py import discord from discord.ext import commands from discord.commands impo...

But not possible.. I want to apologize again for wasting your time

Ignoring exception in command lolmodal:
Traceback (most recent call last):
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\commands\core.py", line 122, in wrapped
    ret = await coro(arg)
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\commands\core.py", line 825, in _invoke
    await self.callback(self.cog, ctx, **kwargs)
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\extensions\Modal.py", line 24, in lolmodal
    await ctx.send_modal(the_modal)
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\interactions.py", line 815, in send_modal
    await adapter.create_interaction_response(
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\webhook\async_.py", line 213, in request
    raise HTTPException(response, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.components: This field is required

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\bot.py", line 1098, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\commands\core.py", line 331, in invoke
    await injected(ctx)
  File "C:\Users\zReaxrYT\PycharmProjects\Discord\venv\lib\site-packages\discord\commands\core.py", line 128, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.components: This field is required
coral mountain
junior sparrowBOT
#
Modal Results
Short Input

1

Long Input

2

paper void
#

thanks @coral mountain 🙂

coral mountain
#

👍

paper void
#

@copper geyser it should be init, not int ^^