Utils
These are a collection of utility methods that are used throughout the library. There are several more that are not included here, as they are reserved for internal use.
Info
All of these methods can be imported from the interactions.client.utils
namespace
Formatting¶
These methods are to help you format strings for messages.
ansi_block(text)
¶
Formats text for discord message as code block that allows for arbitrary coloring and formatting
Source code in interactions/client/utils/formatting.py
79 80 81 |
|
ansi_format(style=None, color=None, background=None)
¶
Gives format prefix for ansi code block with selected styles
Source code in interactions/client/utils/formatting.py
118 119 120 121 122 123 124 125 |
|
ansi_styled(text, style=None, color=None, background=None)
¶
Formats text for ansi code block with selected styles
Source code in interactions/client/utils/formatting.py
131 132 133 134 135 136 137 138 |
|
bold(text)
¶
Formats text for discord message as bold
Source code in interactions/client/utils/formatting.py
29 30 31 |
|
code_block(text, language)
¶
Formats text for discord message as code block
Source code in interactions/client/utils/formatting.py
74 75 76 |
|
inline_code(text)
¶
Formats text for discord message as inline code
Source code in interactions/client/utils/formatting.py
69 70 71 |
|
italic(text)
¶
Formats text for discord message as italic
Source code in interactions/client/utils/formatting.py
34 35 36 |
|
link_in_embed(text, url)
¶
Makes a clickable link inside Embed object
Source code in interactions/client/utils/formatting.py
59 60 61 |
|
no_embed_link(url)
¶
Makes link in discord message display without embedded website preview
Source code in interactions/client/utils/formatting.py
54 55 56 |
|
quote_line(line)
¶
Formats a text line for discord message as quote
Source code in interactions/client/utils/formatting.py
64 65 66 |
|
spoiler(text)
¶
Formats text for discord message as spoiler
Source code in interactions/client/utils/formatting.py
49 50 51 |
|
strikethrough(text)
¶
Formats text for discord message as strikethrough
Source code in interactions/client/utils/formatting.py
44 45 46 |
|
underline(text)
¶
Formats text for discord message as underlined
Source code in interactions/client/utils/formatting.py
39 40 41 |
|
Attrs Utilities¶
These methods are intended to be used with attrs dataclasses
list_converter(converter)
¶
Converts a list of values to a list of converted values
Source code in interactions/client/utils/attr_converters.py
38 39 40 41 42 43 44 45 46 47 48 |
|
optional(converter)
¶
A modified version of attrs optional converter that supports both None
and MISSING
Type annotations will be inferred from the wrapped converter's, if it has any.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
converter | typing.Callable | The convertor that is used for the non-None or MISSING | required |
Source code in interactions/client/utils/attr_converters.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
timestamp_converter(value)
¶
Converts a datetime, int, float, or str to a Timestamp object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Union[datetime, int, float, str] | The time value to convert | required |
Returns:
Type | Description |
---|---|
Timestamp | A Timestamp object |
Source code in interactions/client/utils/attr_converters.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
attrs_validator(validator, skip_fields=None)
¶
Sets a validator to all fields of an attrs-dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
validator | Callable | The validator to set | required |
skip_fields | list[str] | None | A list of fields to skip adding the validator to | None |
Returns:
Type | Description |
---|---|
Callable[[Any, list[Attribute]], list[Attribute]] | The new fields for the attrs class |
Source code in interactions/client/utils/attr_utils.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
docs(doc_string)
¶
Makes it easier to quickly type attr documentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc_string | str | The documentation string. | required |
Returns:
Type | Description |
---|---|
dict[str, str] | The processed metadata dict |
Source code in interactions/client/utils/attr_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 |
|
str_validator(cls, attribute, value)
¶
Validates that the value is a string. Helps convert and ives a warning if it isn't.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls | Any | The instance of the class. | required |
attribute | attrs.Attribute | The attr attribute being validated. | required |
value | Any | The value being validated. | required |
Source code in interactions/client/utils/attr_utils.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
Misc Utilities¶
Uncategorized utilities, might be useful, might not.
disable_components(*components)
¶
Disables all components in a list of components.
Source code in interactions/client/utils/misc_utils.py
242 243 244 245 246 247 248 249 |
|
escape_mentions(content)
¶
Escape mentions that could ping someone in a string.
Note
This does not escape channel mentions as they do not ping anybody
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content | str | The string to escape | required |
Returns:
Type | Description |
---|---|
str | Processed string |
Source code in interactions/client/utils/misc_utils.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
find(predicate, sequence)
¶
Find the first element in a sequence that matches the predicate.
Example Usage:
1 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predicate | Callable[[T], bool] | A callable that returns a boolean value | required |
sequence | Iterable[T] | A sequence to be searched | required |
Returns:
Type | Description |
---|---|
Optional[T] | A match if found, otherwise None |
Source code in interactions/client/utils/misc_utils.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
find_all(predicate, sequence)
¶
Find all elements in a sequence that match the predicate.
Example Usage:
1 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predicate | Callable[[T], bool] | A callable that returns a boolean value | required |
sequence | Iterable[T] | A sequence to be searched | required |
Returns:
Type | Description |
---|---|
List[T] | A list of matches |
Source code in interactions/client/utils/misc_utils.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
get(sequence, **kwargs)
¶
Find the first element in a sequence that matches all attrs.
Example Usage:
1 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence | Iterable[T] | A sequence to be searched | required |
**kwargs | Any | Keyword arguments to search the sequence for | {} |
Returns:
Type | Description |
---|---|
Optional[T] | A match if found, otherwise None |
Source code in interactions/client/utils/misc_utils.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
get_all(sequence, **kwargs)
¶
Find all elements in a sequence that match all attrs.
Example Usage:
1 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence | Iterable[T] | A sequence to be searched | required |
**kwargs | Any | Keyword arguments to search the sequence for | {} |
Returns:
Type | Description |
---|---|
List[T] | A list of matches |
Source code in interactions/client/utils/misc_utils.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
get_event_name(event)
cached
¶
Get the event name smartly from an event class or string name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event | Union[str, BaseEvent] | The event to parse the name of | required |
Returns:
Type | Description |
---|---|
str | The event name |
Source code in interactions/client/utils/misc_utils.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
get_object_name(x)
¶
Gets the name of virtually any object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | Any | The object to get the name of. | required |
Returns:
Name | Type | Description |
---|---|---|
str | str | The name of the object. |
Source code in interactions/client/utils/misc_utils.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
get_parameters(callback)
¶
Gets all the parameters of a callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback | Callable | The callback to get the parameters of | required |
Returns:
Type | Description |
---|---|
dict[str, inspect.Parameter] | A dictionary of parameters |
Source code in interactions/client/utils/misc_utils.py
177 178 179 180 181 182 183 184 185 186 187 188 |
|
maybe_coroutine(func, *args, **kwargs)
async
¶
Allows running either a coroutine or a function.
Source code in interactions/client/utils/misc_utils.py
235 236 237 238 239 |
|
nulled_boolean_get(data, key)
¶
Gets a boolean value from a dictionary, but treats None as True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data | dict[str, Any] | The dictionary to get the value from | required |
key | str | The key to get the value from | required |
Returns:
Type | Description |
---|---|
bool | The boolean value of the key |
Source code in interactions/client/utils/misc_utils.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
wrap_partial(obj, cls)
¶
🎁 Wraps a commands callback objects into partials.
Note
This is used internally, you shouldn't need to use this function
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj | Any | The command object to process | required |
cls | Any | The class to use in partials | required |
Returns:
Type | Description |
---|---|
Callable | The original command object with its callback methods wrapped |
Source code in interactions/client/utils/misc_utils.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|