Skip to content

breadcord.helpers

HTTPModuleCog

Bases: ModuleCog

A module cog which automatically creates and closes an aiohttp session.

IndentFormatter

Bases: Formatter

get_prefix_length(record)

Get the length of the prefix of the log message. Will not give wanted results if ANSI is involved.

administrator_check(interaction)async

A discord.py check to ensure that the interaction user is an administrator.

An administrator is either an owner of the bot application as seen on the Discord developer portal, or a user granted administrator privileges under the settings.administrators setting.

This function should not be invoked directly, and should instead be passed to the discord.app_commands.check function and used as a decorator.

Example::

@app_commands.command()
@app_commands.check(breadcord.helpers.administrator_check)
async def do_admin_stuff(self, interaction: discord.Interaction, ...):
    ...

Parameters:

NameTypeDescriptionDefault
interactionInteraction

The current interaction which the check should apply to.

required

make_codeblock(content, lang=None, *, escape_backticks=True)

Wraps a string in a Discord codeblock with optional syntax highlighting and backtick escaping.

Warning

While escape_backticks=True is useful for ensuring that triple backticks within the codeblock content don't break formatting, it will insert invisible zero-width joiner characters into the content. Be aware that this could potentially interfere with syntax highlighting, or copying the codeblock content to clipboard.

Parameters:

NameTypeDescriptionDefault
contentstr

The text content contained within the codeblock.

required
langstr | None

The language code to use for syntax highlighting (e.g. py for Python).

None
escape_backticksbool

Whether to escape triple backticks in the codeblock content - see above warning.

True

Returns:

TypeDescription
str

The resulting codeblock string.

search_for(query, objects, *, key=None, threshold=80, max_results=25)

A custom implementation of a fuzzy search algorithm.

The algorithm works by assigning each string a two-part score: The partial ratio score (a metric for similarity), and how far from the start of the string the optimal alignment is. Results are then sorted by highest partial ratio score first, with a secondary sort of earliest alignment to ensure that matches near the start of the string are shown earlier.

Parameters:

NameTypeDescriptionDefault
querystr

The string to search for.

required
objectsSequence[_T]

The sequence of strings, or other objects if using a key, to search through.

required
keyCallable[[_T], str] | None

An optional function which is called for each object, taking it as a parameter and returning a string to be used in the search process.

None
thresholdfloat

A number between 0 and 100 inclusive which determines the cutoff point for the similarity score. A threshold of 0 means that every object will be returned in the results, whereas a threshold of 100 means that only exact matches will be returned.

80
max_resultsint | None

The maximum number of results to be returned from the search. This can be set to None to return all results which pass the threshold.

25

Returns:

TypeDescription
list[_T]

A list of objects which pass the similarity threshold, in order of decreasing relevance.

simple_button(label=None, disabled=False, style=discord.ButtonStyle.grey, emoji=None, row=None)

A substitute for discord.ui.button which generates a custom ID for you.

The custom ID is generated based off the qualified name of the decorated method, which should ensure that the ID is both unique and idempotent. However, since custom IDs cannot exceed 100 characters in length, this may fail for exceptionally long names.

Parameters:

NameTypeDescriptionDefault
labelstr | None

The label of the button, if any.

None
styleButtonStyle

The style of the button. Defaults to :attr:discord.ButtonStyle.grey.

grey
disabledbool

Whether the button is disabled or not. Defaults to False.

False
emojistr | Emoji | PartialEmoji | None

The emoji of the button. This can be in string form or a :class:discord.PartialEmoji or a full :class:discord.Emoji.

None
rowint | None

The relative row this button belongs to. A Discord component can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you'd like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults to None, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed).

None

simple_transformer(to)

A decorator for discord.py transformers to make them easier to use in type annotations.

Before::

class BooleanTransformer(app_commands.Transformer):
    def transform(self, interaction: discord.Interaction, value: str) -> bool:
        return True if value.strip().lower() in ('true', 'yes', 'y') else False

@app_commands.command()
async def say_hello(
    interaction: discord.Interaction,
    all_caps: app_commands.Transform[bool, BooleanTransformer]
):
    await interaction.response.send_message('HELLO' if all_caps else 'hello')

transformer = BooleanTransformer()
manual_transform = transformer.transform(some_interaction, 'yes')

After::

@breadcord.helpers.simple_transformer(bool)
class BooleanTransformer(app_commands.Transformer):
    def transform(self, interaction: discord.Interaction, value: str) -> bool:
        return True if value.strip().lower() in ('true', 'yes', 'y') else False

@app_commands.command()
async def say_hello(interaction: discord.Interaction, all_caps: BooleanTransformer):
    await interaction.response.send_message('HELLO' if all_caps else 'hello')

manual_transform = BooleanTransformer.transform(some_interaction, 'yes')

Parameters:

NameTypeDescriptionDefault
totype[_T]

The type which the transformer should output.

required
Peekaboo!