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:
Name | Type | Description | Default |
---|---|---|---|
interaction | Interaction | 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:
Name | Type | Description | Default |
---|---|---|---|
content | str | The text content contained within the codeblock. | required |
lang | str | None | The language code to use for syntax highlighting (e.g. | None |
escape_backticks | bool | Whether to escape triple backticks in the codeblock content - see above warning. | True |
Returns:
Type | Description |
---|---|
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:
Name | Type | Description | Default |
---|---|---|---|
query | str | The string to search for. | required |
objects | Sequence[_T] | The sequence of strings, or other objects if using a key, to search through. | required |
key | Callable[[_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 |
threshold | float | 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_results | int | None | The maximum number of results to be returned from the search. This can be set to | 25 |
Returns:
Type | Description |
---|---|
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:
Name | Type | Description | Default |
---|---|---|---|
label | str | None | The label of the button, if any. | None |
style | ButtonStyle | The style of the button. Defaults to :attr: | grey |
disabled | bool | Whether the button is disabled or not. Defaults to | False |
emoji | str | Emoji | PartialEmoji | None | The emoji of the button. This can be in string form or a :class: | None |
row | int | 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 |
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:
Name | Type | Description | Default |
---|---|---|---|
to | type[_T] | The type which the transformer should output. | required |