API: utils¶
utils.EmojiManager
¶
EmojiManager
¶
Loads and resolves emoji placeholders for global and module scopes.
Placeholders follow the pattern :emoji_name:
. This manager supports a
global emoji catalog (single JSON file) and per‑module catalogs.
Source code in utils/EmojiManager.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 79 |
|
__init__(bot, global_path, logger)
¶
Initialize the emoji manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bot
|
The bot instance (for context; not required to resolve emojis). |
required | |
global_path
|
str
|
Directory containing the |
required |
logger
|
Logger
|
Logger for diagnostics. |
required |
Source code in utils/EmojiManager.py
17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
load_global_emojis()
¶
Load global emojis from <global_path>/emojis.json
.
Source code in utils/EmojiManager.py
31 32 33 34 35 36 37 38 39 40 41 42 |
|
load_module_emojis(module_name, module_path)
¶
Load per‑module emojis from <module_path>/emojis.json
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name
|
str
|
Name of the module to associate the catalog. |
required |
module_path
|
str
|
Filesystem path to the module root. |
required |
Source code in utils/EmojiManager.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
replace_emojis(text, module_name=None)
¶
Replace :emoji_name:
placeholders with actual emojis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The source text that may contain placeholders. |
required |
module_name
|
Optional[str]
|
Optional module name to use a module‑specific catalog. |
None
|
Returns:
Type | Description |
---|---|
str
|
The processed text with placeholders substituted. |
Source code in utils/EmojiManager.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
utils.Translator
¶
Translator
¶
High‑level translation service with caching and emoji post‑processing.
This utility
- Resolves the preferred language per guild (with caching).
- Loads and caches global translation files from a shared folder.
- Loads and caches module translation files from each module folder.
- Replaces emoji placeholders using
EmojiManager
(if available). - Exposes convenience helpers to retrieve a translation or a translator function (sync/async).
Notes
- Global translations live under
global_path/*.json
. - Module translations are discovered under
<module>/translations/*.json
unless a different folder is declared in the module's manifest.
Source code in utils/Translator.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 79 80 81 82 83 84 85 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 112 113 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 141 142 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
__init__(bot, global_path, logger)
¶
Initialize the translator and its caches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bot
|
ExtendedClient
|
The extended Discord client. |
required |
global_path
|
str
|
Filesystem path to the global translations directory. |
required |
logger
|
Logger
|
Logger for diagnostics. |
required |
Source code in utils/Translator.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
get_language(guild_id)
async
¶
Return the configured language for a guild (default 'en'
).
Normalizes common aliases like "English"
, "en-US"
, "pt-br"
, etc.
Results are cached per guild id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id
|
str
|
Discord guild id. |
required |
Returns:
Type | Description |
---|---|
str
|
Normalized language code such as |
Source code in utils/Translator.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
get_language_sync(guild_id)
¶
Synchronous variant of get_language
using the local cache only.
If the language for guild_id
is not cached, returns 'en'
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id
|
Optional[str]
|
Guild id (or |
required |
Returns:
Type | Description |
---|---|
str
|
A normalized language code. |
Source code in utils/Translator.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
get_translation(key, language, module_name=None)
¶
Return the processed translation for a dot‑separated key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Hierarchical key such as |
required |
language
|
str
|
Target language code. |
required |
module_name
|
Optional[str]
|
Module name to search in module cache; if omitted, the global cache is used. |
None
|
Returns:
Type | Description |
---|---|
str
|
The resolved translation string, or |
Source code in utils/Translator.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
get_translator(guild_id, module_name=None)
async
¶
Return a callable that resolves translations for a guild asynchronously.
The callable captures the guild's current language and interpolates **kwargs
with standard Python str.format
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id
|
str
|
Guild id to derive the language from. |
required |
module_name
|
Optional[str]
|
Module name for module‑scoped translations. |
None
|
Returns:
Type | Description |
---|---|
Callable[[str], str]
|
A function |
Source code in utils/Translator.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
get_translator_sync(language, module_name=None)
¶
Return a synchronous translator function for a fixed language.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
language
|
str
|
Target language. |
required |
module_name
|
Optional[str]
|
Optional module context. |
None
|
Returns:
Type | Description |
---|---|
Callable[[str], str]
|
A function |
Source code in utils/Translator.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
refresh_translation_cache()
async
¶
Reload global and module translation caches asynchronously.
Scans the global_path
for *.json
files and each loaded module's
translations directory (from its manifest or default "translations"
).
Source code in utils/Translator.py
132 133 134 135 136 137 138 139 140 141 142 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 |
|
update_language_cache(guild_id, language)
¶
Update the local language cache for a guild.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id
|
str
|
Guild id. |
required |
language
|
str
|
Normalized language code to store. |
required |
Source code in utils/Translator.py
85 86 87 88 89 90 91 92 93 |
|
utils.Loader
¶
load_commands_from_folder(bot, folder, base_package, logger)
async
¶
Dynamically loads commands from a specified folder.
:param bot: The bot instance. :param folder: The Path object for the folder containing command files. :param base_package: The base Python package path to use when loading extensions. :param logger: Logger instance for logging progress and errors.
Source code in utils/Loader.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
load_translation(module_name, file_name, language, default_language='en')
¶
Carrega o dicionário de traduções baseado no idioma fornecido.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name
|
str
|
Nome do módulo onde estão as traduções (e.g., "XPSystem"). |
required |
file_name
|
str
|
Nome do arquivo de tradução (sem extensão .json). |
required |
language
|
str
|
Idioma preferido da guilda. |
required |
default_language
|
str
|
Idioma padrão caso o preferido não esteja disponível. |
'en'
|
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
Dicionário de traduções no idioma solicitado. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
Se o arquivo de tradução não existir. |
ValueError
|
Se o JSON não puder ser decodificado ou se o idioma não estiver disponível. |
Source code in utils/Loader.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
utils.InteractionView
¶
InteractionView
¶
Bases: View
, AsyncIOEventEmitter
Interactive Discord UI view that also emits custom events.
This view bridges discord.ui.View
with an async event emitter, allowing
callers to listen for lifecycle events like "end"
(timeout/deletion) while
still using standard Discord UI components (buttons/selects).
Features
- Auto timeout and
"end"
emission with reason"timeout"
. - Listens for
on_message_delete
to end with reason"deleted"
. - Component
custom_id
normalization and namespacing withview_id
. - Ability to clone a view with the same behavior.
Source code in utils/InteractionView.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 79 80 81 82 83 84 85 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 112 113 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 141 142 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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 216 217 218 219 220 221 222 223 224 225 226 227 |
|
__init__(interaction, channel, client, ephemeral=False, filter_func=None, timeout=60, parent=None)
¶
Create an InteractionView
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interaction
|
Interaction
|
The original interaction that spawned this view. |
required |
channel
|
TextChannel
|
Text channel for context/logging. |
required |
client
|
ExtendedClient
|
Extended bot client. |
required |
ephemeral
|
Optional[bool]
|
Whether the response should be ephemeral. |
False
|
filter_func
|
Optional[Callable[[Interaction], bool]]
|
Optional predicate to filter accepted interactions. |
None
|
timeout
|
Optional[int]
|
Timeout (seconds) before this view ends automatically. |
60
|
parent
|
Optional[InteractionView]
|
Optional parent view if this is a clone. |
None
|
Source code in utils/InteractionView.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
clone()
¶
Create a shallow clone of this view.
Returns:
Type | Description |
---|---|
InteractionView
|
A new |
Source code in utils/InteractionView.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
destroy(reason=None)
¶
Tear down the view and unregister listeners.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reason
|
Optional[str]
|
Optional reason for diagnostics (e.g., |
None
|
Source code in utils/InteractionView.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
normalize_custom_id(custom_id)
¶
Strip the view_id suffix from a component custom_id
, if present.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
custom_id
|
str
|
The raw custom id from an interaction component. |
required |
Returns:
Type | Description |
---|---|
str
|
The normalized id without the view suffix. |
Source code in utils/InteractionView.py
156 157 158 159 160 161 162 163 164 165 166 167 |
|
on_timeout()
async
¶
Called by discord.py when the view times out.
Emits the "end"
event with reason "timeout"
and destroys the view.
Source code in utils/InteractionView.py
75 76 77 78 79 80 81 82 |
|
set_extra_filter(filter_func)
¶
Set an additional interaction filter predicate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filter_func
|
Callable[[Interaction], bool]
|
Callable that receives an |
required |
Source code in utils/InteractionView.py
219 220 221 222 223 224 225 226 227 |
|
set_msg_id(msg_id)
¶
Attach a message id to this view instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg_id
|
Optional[str]
|
The Discord message id to associate. |
required |
Source code in utils/InteractionView.py
188 189 190 191 192 193 194 195 |
|
start_timeout()
¶
(Re)start the internal timeout task.
Source code in utils/InteractionView.py
95 96 97 98 99 100 |
|
update(**kwargs)
async
¶
Update the message associated with this view.
Other Parameters:
Name | Type | Description |
---|---|---|
components |
Optional iterable of components (Buttons/Selects) to add. |
Returns:
Type | Description |
---|---|
bool
|
True if the update succeeds, False otherwise. |
Source code in utils/InteractionView.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
utils.MessageView
¶
MessageView
¶
Bases: AsyncIOEventEmitter
Source code in utils/MessageView.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 79 80 81 82 83 84 85 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 |
|
__init__(message, channel, client, filter_func=None, timeout=60000)
¶
MessageView is a utility for managing interactive views tied to messages in a Discord bot.
Source code in utils/MessageView.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
clone()
¶
Create a cloned instance of this MessageView.
Source code in utils/MessageView.py
75 76 77 78 79 80 81 82 83 84 85 |
|
destroy(reason=None)
¶
Destroy this view and clean up listeners.
Source code in utils/MessageView.py
87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
update(view)
async
¶
Update the view with new content, embeds, or components.
Source code in utils/MessageView.py
62 63 64 65 66 67 68 69 70 71 72 73 |
|
create_view(channel, client, view, filter_func=None)
async
¶
Create a MessageView tied to a newly sent message.
Source code in utils/MessageView.py
114 115 116 117 118 119 120 121 |
|
create_view_from_interaction(interaction, client, view, filter_func=None)
async
¶
Create a MessageView tied to an interaction's response.
Source code in utils/MessageView.py
124 125 126 127 128 129 130 131 132 133 134 |
|
create_view_from_message(message, client, filter_func=None)
async
¶
Create a MessageView tied to an existing message.
Source code in utils/MessageView.py
137 138 139 140 141 142 143 |
|
utils.ViewRouter
¶
ViewRouter
¶
Bases: AsyncIOEventEmitter
A class to manage a navigation stack for interactive views.
Source code in utils/ViewRouter.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
__init__(logger, view)
¶
Initialize the ViewRouter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger
|
Logger instance for logging. |
required | |
view
|
The current active view (e.g., InteractionView or similar). |
required |
Source code in utils/ViewRouter.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
clear_stack()
¶
Clear the navigation stack.
Source code in utils/ViewRouter.py
85 86 87 |
|
destroy()
async
¶
Destroy the view and clean up resources.
Source code in utils/ViewRouter.py
101 102 103 |
|
pop()
async
¶
Pop the current page from the navigation stack.
Returns:
Type | Description |
---|---|
The unique ID of the popped page, or None if the stack is empty. |
Source code in utils/ViewRouter.py
71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
push(update)
async
¶
Push a new page onto the navigation stack.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
update
|
Dict[str, Any]
|
A dictionary containing the new view's content and components. |
required |
Returns:
Type | Description |
---|---|
str
|
A unique ID for the pushed page. |
Source code in utils/ViewRouter.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
set_rows(rows)
¶
Set persistent rows to append to every page.
Source code in utils/ViewRouter.py
45 46 47 |
|
set_view(view)
¶
Set the active view managed by the router.
Source code in utils/ViewRouter.py
41 42 43 |
|
update(update)
async
¶
Update the current view without modifying the stack.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
update
|
Dict[str, Any]
|
A dictionary containing the updated view's content and components. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the update was successful, False otherwise. |
Source code in utils/ViewRouter.py
89 90 91 92 93 94 95 96 97 98 99 |
|
utils.components.PaginatorComponent
¶
PaginatorComponent
¶
Bases: AsyncIOEventEmitter
Source code in utils/components/PaginatorComponent.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 79 80 81 82 83 84 85 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 112 113 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 141 142 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 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
add_pagination_controls(page_data)
¶
Adiciona controles de paginação ao layout da página.
Source code in utils/components/PaginatorComponent.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
init()
async
¶
Inicializa o paginator na primeira página.
Source code in utils/components/PaginatorComponent.py
182 183 184 185 186 187 |
|
next_page()
async
¶
Avança para a próxima página programaticamente.
Source code in utils/components/PaginatorComponent.py
158 159 160 161 162 163 164 |
|
next_page_interaction(interaction)
async
¶
Lida com a interação do botão "Próximo".
Source code in utils/components/PaginatorComponent.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
previous_page()
async
¶
Retorna à página anterior programaticamente.
Source code in utils/components/PaginatorComponent.py
166 167 168 169 170 171 172 |
|
previous_page_interaction(interaction)
async
¶
Lida com a interação do botão "Anterior".
Source code in utils/components/PaginatorComponent.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
set_page(page)
async
¶
Define uma página específica como a atual.
Source code in utils/components/PaginatorComponent.py
174 175 176 177 178 179 180 |
|
set_update_function(fn)
¶
Define a função para atualizar as páginas.
Source code in utils/components/PaginatorComponent.py
152 153 154 155 156 |
|
update_view(page)
async
¶
Atualiza a view com a página especificada.
Source code in utils/components/PaginatorComponent.py
104 105 106 107 108 109 110 |
|
create_paginator(view, pages, flags=None, control_style=None)
async
¶
Cria e retorna um componente de paginação.
Source code in utils/components/PaginatorComponent.py
190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
utils.components.EmbedCreatorComponent
¶
embed_creator(view, filter_func, should_complete=True, data=None)
async
¶
Cria um embed interativo para ser configurado pelo usuário, com suporte a traduções globais.
Source code in utils/components/EmbedCreatorComponent.py
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 79 80 81 82 83 84 85 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 112 113 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 141 142 143 144 145 |
|
wait_for_user_input(view, new_view, expiry, filter_func, options=None)
async
¶
Aguarda a entrada do usuário em uma mensagem ou interação.
Source code in utils/components/EmbedCreatorComponent.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|