Utils

Some utility functions for Discord and Python.

ezcord.utils.create_json_file(dictionary, filename='data.json', *, indent=2, description=None, spoiler=False, **kwargs)[source]

Create a discord.File object from a dictionary.

Parameters:
  • dictionary (dict) – The dictionary to convert to a JSON file.

  • filename (str) – The filename to use for the JSON file.

  • indent (int) – The indent to use for the JSON file.

  • description (str | None) – The description to use for the discord file.

  • spoiler (bool) – Whether the Discord file should be a spoiler.

  • **kwargs – Additional keyword arguments for json.dumps().

Return type:

discord.File

ezcord.utils.create_text_file(text, filename='data.txt', **kwargs)[source]

Create a discord.File object from a string.

Parameters:
  • text (str) – The string to convert to a text file.

  • filename (str) – The filename to use for the text file.

  • **kwargs – Keyword arguments for discord.File.

Return type:

discord.File

ezcord.utils.create_html_file(html, filename='data.html', **kwargs)[source]

Create a discord.File object from an HTML string.

Parameters:
  • html (str) – The HTML string to convert to an HTML file.

  • filename (str) – The filename to use for the HTML file.

  • **kwargs – Keyword arguments for discord.File.

Return type:

discord.File

ezcord.utils.random_avatar()[source]

Returns the URL of a random default avatar.

Return type:

str

ezcord.utils.codeblock(content, *, lang='yaml', unit='', interaction=None)[source]

Returns a codeblock with the given content.

Parameters:
  • content (int | str) – The content of the codeblock. If the content is an integer, it will be formatted with commas (or dots if the language is German).

  • lang (str) – The language of the codeblock. Defaults to yaml.

  • unit (str) – The text to display after the given content. This is only used if the content is an integer.

  • interaction (Interaction | None) – The interaction to get the language from. Defaults to None. If not provided, the language will be set to the default language. The language will determine how large numbers are formatted.

Return type:

str

ezcord.utils.ez_autocomplete(values)[source]

A similar function as basic_autocomplete(), but instead of returning options starting with the user’s value, it returns options containing the user’s value.

Parameters:

values (Iterable[str]) – Accepts an iterable of str, a callable (sync or async) that takes a single argument of AutocompleteContext, or a coroutine. Must resolve to an iterable of str.

ezcord.utils.count_lines(directory=None, *, count_empty_lines=True, include_hidden=False, ignored_dirs=None, ignored_files=None)[source]

Counts the total amount of lines in all Python files in the given directory.

Parameters:
  • directory (str | None) – The directory to count the lines in. Defaults to the current working directory.

  • count_empty_lines (bool) – Whether to count empty lines. Defaults to True.

  • include_hidden (bool) – Whether to include directories starting with a dot. Defaults to False.

  • ignored_dirs (list[str] | None) – A list of directories to ignore. By default, venv folders and folders starting with a dot are ignored.

  • ignored_files (list[str] | None) – A list of file patterns to ignore.

Return type:

int

async ezcord.utils.load_message(obj, message_url, error=False)[source]

Get a message from a message URL.

Parameters:
  • obj (Guild | Bot) – The object to use to fetch the message.

  • message_url (str) – The URL of the message.

  • error (bool) – Whether to raise an error if the message couldn’t be found. If this is False, the function will return None if the message couldn’t be found. Defaults to False.

Raises:
Return type:

Message | None