Parsers and Converters

Functions and classes used to convert str into more useful classes. This work very well with discord.ext.commands commands and can be used as typehints for arguments. Function type parsers can work with any strings.

Function parameters are not generally documented here because they’re the same, a string.

class dpytools.parsers.MemberUserProxy

Tries to convert the argument first to discord.Member and then to discord.User, if it cannot be found returns discord.Object

If the bot cannot find the member or user object, the argument must be an id ìnt.

Returns

Return type

Union[discord.Member, discord.User, discord.Object]

Raises

BadArgument – if member or user cannot be found and and the argument cannot be converted to int:

await convert(ctx, argument)

This does the actual conversion

Parameters
  • ctx (Context) – The invocation context that the argument is being used in.

  • argument (str) – The argument that is being converted.

Raises
  • CommandError – A generic exception occurred when converting the argument.

  • BadArgument – The converter failed to convert the argument.

class dpytools.parsers.Trimmer(max_length: int, end_sequence: str = '...')

Callable Class that trims strings to fit a maximum length

Parameters
  • max_length (ìnt) – Maximum length of the string

  • end_sequence (str) – Character sequence that denote that the full message is actually longer than the returned string

__call__(string: str)str

This turns the class into a callable object that parses the argument This is the actual parser

Returns

The processed string

Return type

str

dpytools.parsers.to_lower(string: str)str

Converts :string: to lower case. Intended to be used as argument converter.

Returns

string to lower case

Return type

str

dpytools.parsers.to_month(string: str)int

This converter takes a string and checks if it contains a valid month of the year

Parameters

string (str) – Can be the full name, the shorter three leters conventional name or the number of the month

Returns

The number of the selected month

Return type

int

Example

from dpytools.parsers import to_month
@bot.command(name='month')
async def somemonth(ctx, month: to_month):
    print(month)

# user's input: "jan"
>>> 1
# user's input "february"
>>> 2
# user's input "5"
>>> 5
dpytools.parsers.to_spongebob_case(string: str)str

Converts a given string to spongebob case (alternating caps)

Returns

New string in sarcastic (spongebob) case

Return type

str

dpytools.parsers.to_timedelta(string: str)datetime.timedelta

Converts a string with format <number>[s|m|h|d|w] to timedelta object <number> must be convertible to float.

Uses regex to match groups and consumes all groups into one timedelta.

Units:
  • s: second

  • m: minute

  • h: hour

  • d: day

  • w: weeks

Parameters

string (str) – format <number>[s|m|h|d|w].

Returns

the argument converted to a timedelta object

Return type

timedelta

Raises
  • ValueError – If string number cannot be converted to float

  • InvalidTimeString – If string isn’t in the valid form.

Example

from dpytools.parsers import to_timedelta
@bot.command(name='time')
async def _time(ctx, time: to_timedelta):
    print(time)

# user's input: "2h30m"
>>> timedelta(hours=2, minutes=30)
dpytools.parsers.to_upper(string: str)str

Converts :string: to upper case. Intended to be used as argument converter.

Returns

String to upper case

Return type

str