vutfitdiscord / rubbergod

FITWIDE discord bot
GNU General Public License v3.0
50 stars 43 forks source link

Create custom converters for datetime arguments #1039

Open solumath opened 1 month ago

solumath commented 1 month ago

We have a lot of commands that take as input a date format. At the moment we use function in utils which is good but I think it would be better if we were able to create a custom converter as many disnake types have.

Example would be

@commands.slash_command(name="timeout", description=...)
async def timeout_user(..., endtime: CustomDatetime):
    ...

With this approach we don't have to manually parse the time everytime and the global check would catch errors. :warning: This wont change the discord input field. It would still be plain string.

This is demo of suggested code that would work globally. It is matched by type annotation.

@commands.register_injection
def str_to_datetime(inter, date_input: str) -> CustomDatetime:
    return ...

# then:
@commands.slash_command(name="timeout", description=...)
async def timeout_user(..., endtime: CustomDatetime):
    ...

Another way are local converters

@commands.slash_command(name="timeout", description=...)
async def timeout_user(..., endtime: CustomDatetime = commands.Param(converter=str_to_datetime):
   ...