Utility Functions

Compilation of Utility Functions

class LineTimer[source]

A Context Manager to profile a set of lines of code.

Parameters
  • logger (CustomLogger) – The logger to use.

  • message (Optional[str]) – Prefix to add to the profile output.

  • profile_it (Optional[bool]) – Verbose profiling?

class ImageCacher[source]

A TTL Cache for images created in a command for a user.

Parameters
  • user (discord.Member) – The user to cache images for.

  • kwargs (Dict[str, Any]) – Additional Keyword Arguments.

property keys: Tuple[str, Tuple[str, Any]]

Returns a hashable key for the cache.

Returns

Hashable Key

Return type

Tuple[str, Tuple[str, Any]]

expire()[source]

Expire the cache.

register()[source]

Registers the image url to the cache.

Parameters

img_url (str) – The image url to register.

property cached: Optional[str]

Returns the cached image URL if it exists.

Returns

The cached image URL.

Return type

Optional[str]

class EmbedFieldsConfig[source]

A config class for get_embed().

class FieldConfig[source]

Config for single Embed Field.

parent

The parent EmbedFieldsConfig

inline

Whether the field is inline or not.

highlight

Whether to wrap the field in a code block.

highlight_lang

The language to highlight the field in.

get()[source]

Returns the value of the attribute. Checks for master overrides in the parent.

Parameters

attr (str) –

Return type

Any

highlight

Whether to wrap the fields in a codeblock.

inline

Whether to display the fields inline.

field_config_map

A mapping of field names to FieldConfig objects.

get()[source]

Returns the config for a field.

Parameters

key (str) – The key to get the value of.

Returns

The value of the key.

Return type

Union[bool, EmbedFieldsConfig.FieldConfig]

dedent()[source]

Strips whitespaces from the left of every line.

Parameters

message (str) – The message to dedent.

Returns

The dedented message.

Return type

str

async dm_send()[source]

Attempts to send message to the User’s DM. In case of fallback, sends in the original channel.

Parameters
  • message (discord.Message) – The message to send.

  • user (discord.Member) – The user to send the message to.

  • content (Optional[str]) – The content of the message.

  • embed (Optional[discord.Embed]) – The embed to send.

  • kwargs (Dict[str, Any]) – Additional Keyword Arguments.

Returns

The message sent.

Return type

discord.Message

get_ascii()[source]

Returns the ascii art for a text.

Parameters

msg (str) – The message to convert to ascii art.

Returns

The ascii art.

Return type

str

get_embed()[source]

Creates a Discord Embed with appropriate color, title and description.

Parameters
  • content (Optional[str]) – The content of the embed.

  • embed_type (Optional[str]) – The type of embed., default is info.

  • title (Optional[str]) – The title of the embed.

  • footer (Optional[str]) – The footer of the embed.

  • image (Optional[str]) – The image url for the embed.

  • thumbnail (Optional[str]) – The thumbnail url for the embed.

  • color (Optional[int]) – The color of the embed.

  • no_icon (Optional[bool]) – If True, no icon will be shown.

  • fields (Optional[Dict[str, str]]) – The fields of the embed.

  • fields_config (Optional[EmbedFieldsConfig]) – The fields config of the embed.

Returns

The embed

Return type

discord.Embed

get_enum_embed()[source]

Creates a Discord Embed with prettified iterable as description.

Parameters
  • iterable (Iterable) – The iterable to be used as description.

  • embed_type (str) – The type of embed., default is info.

  • title (Optional[str]) – The title of the embed.

  • custom_ext (bool) – If True, won’t be wrapped in Markdown codeblock.

  • color (Optional[int]) – The color of the embed.

Returns

The embed

Return type

discord.Embed

get_formatted_time()[source]

Converts total seconds into a human readable format.

Parameters
  • tot_secs (int) – The total seconds to be converted.

  • show_hours (bool) – If True, hours will be shown.

  • show_mins (bool) – If True, minutes will be shown.

Returns

The formatted time.

Return type

str

get_modules()[source]

Returns a list of all the Commands Modules.

Parameters

ctx (bot.PokeGambler) – The PokeGambler client object.

Returns

A list of all the command modules.

Return type

List[Commands]

get_modules_from_path()[source]

Returns a list of all the modules using file path. :param fname: The name of the file to be loaded. :type fname: str :param sort_order: The order in which the modules should be sorted. :type sort_order: Optional[List[str]] :return: A string concatenated list of all the command modules. :rtype: str

Parameters
  • fname (str) –

  • sort_order (Optional[List[str]]) –

Return type

str

is_admin()[source]

Checks if user is an admin in the official server.

Parameters

user (discord.Member) – The user to be checked.

Returns

True if the user is an admin.

Return type

bool

is_dealer()[source]

Checks if user is a PokeGambler Dealer.

Parameters

user (discord.Member) – The user to be checked.

Returns

True if the user is a PokeGambler Dealer.

Return type

bool

is_owner()[source]

Checks if the user is an owner of PokeGambler.

Parameters
Returns

True if the user is an owner of PokeGambler.

Return type

bool

img2file()[source]

Convert a PIL Image into a discord File.

Parameters
  • img (PIL.Image.Image) – The PIL Image to be converted.

  • fname (str) – The filename of the file.

  • ext (str) – The extension of the file.

Returns

The discord File object.

Return type

discord.File

async online_now()[source]

Notifies on Discord, that PokeGambler is ready.

Parameters

ctx (bot.PokeGambler) – The PokeGambler client object.

parse_command()[source]

Parses a message to obtain the command, args and kwargs.

Parameters
  • prefix (str) – The prefix of the command.

  • msg (str) – The message to be parsed.

Returns

A dictionary with the command, args and kwargs.

Return type

Dict

prettify_discord()[source]

Prettification for iterables like guilds and channels.

Parameters
  • ctx (bot.PokeGambler) – The PokeGambler client object.

  • iterable (List[str]) – The iterable to be prettified.

  • mode (str) – Guild or Channel?

Returns

The prettified string.

Return type

str

showable_command()[source]

Checks if a command is accessible to a user based on roles.

Parameters
  • ctx (bot.PokeGambler) – The PokeGambler client object.

  • cmd (Callable) – The command to be checked.

  • user (discord.Member) – The user to check the command for.

Returns

True if the command is accessible, False otherwise.

Return type

bool

get_commands()[source]

Get a list of all showable commands for a given Module.

Parameters
  • ctx (bot.PokeGambler) – The PokeGambler client object.

  • user (discord.Member) – The user to check the commands for.

  • module (Commands) – The module to get the commands from.

  • roles (Optional[List[str]]) – The roles to check the commands for.

Returns

A list of all showable commands.

Return type

str

async wait_for()[source]

Modified version of discord.Client.wait_for(). Checks the history once upon timeout.

Parameters
  • chan (discord.TextChannel) – The channel to wait for messages in.

  • ctx (bot.PokeGambler) – The PokeGambler client object.

  • event (str) – The event to wait for., defaults to message.

  • init_msg (Optional[discord.Message]) – Checks history after this message.

  • check (Optional[Callable]) – Checks the message against this function.

  • timeout (Optional[Union[float, str]]) – The timeout to wait for.

Returns

The message that was received.

Return type

discord.Message