|
1 |
| -# Botkit: A Framework for Building Discord Bots with py-cord |
| 1 | +# Botkit: An Advanced Framework for Crafting Sophisticated Discord Bots with py-cord |
2 | 2 |
|
3 | 3 | ## What is Botkit?
|
4 | 4 |
|
5 |
| -Botkit is a framework for building Discord bots. |
6 |
| -It is designed to simplify the process of creating and managing Discord bots, |
7 |
| -by providing a modular architecture and a set of useful tools and utilities |
8 |
| -so that you can focus on building your bot's functionality. |
9 |
| -We use it ourselves to build our own bots, and we hope you find it useful too! |
| 5 | +Botkit is a comprehensive framework designed for developing feature-rich, production-ready Discord bots. It goes beyond basic bot creation by offering a suite of advanced tools and integrations that empower developers to build sophisticated, scalable, and maintainable bots. |
| 6 | + |
| 7 | +Key features include: |
| 8 | + |
| 9 | +- **Robust Internationalization (i18n)**: Built-in support for multi-language bots, allowing seamless localization across diverse Discord communities. |
| 10 | +- **Bot Listing Integration**: Streamlined processes for managing your bot's presence on popular bot listing websites, enhancing discoverability. |
| 11 | +- **Advanced Error Handling**: Sentry-compatible error tracking, enabling real-time monitoring and debugging of your bot in production environments. (Sentry is an application monitoring platform that helps developers identify and fix crashes in real time.) |
| 12 | +- **Uptime Status Posting**: Automated systems for reporting your bot's operational status, crucial for maintaining user trust and meeting service level agreements. |
| 13 | +- **Modular Architecture**: A flexible, extension-based structure that facilitates easy feature addition and management. |
| 14 | + |
| 15 | +While Botkit provides a rich set of advanced features, it maintains a balance between functionality and efficiency, offering a powerful yet not overly cumbersome development experience. |
10 | 16 |
|
11 | 17 | ## What Botkit is NOT
|
12 | 18 |
|
13 |
| -Botkit is not a pre-built Discord bot. Instead, it is a starting point for building your own custom Discord bot. You’re expected to edit and modify the provided code to suit your specific requirements. |
| 19 | +Botkit is not a pre-built, out-of-the-box Discord bot solution. It's a sophisticated framework and starting point for developers looking to create advanced, custom Discord bots. Botkit is designed for those who need more than basic functionality and are ready to leverage its powerful features to create truly unique and capable bots. |
14 | 20 |
|
15 | 21 | ## Features
|
16 | 22 |
|
@@ -118,6 +124,116 @@ schema = {
|
118 | 124 | ```
|
119 | 125 | We really encourage you to follow these instructions, even if you’re coding privately, as it will make your code more readable and maintainable in the long run.
|
120 | 126 |
|
| 127 | +## Internationalization (i18n) |
| 128 | + |
| 129 | +Botkit provides robust support for internationalization, allowing you to create multi-language Discord bots with ease. Here's how to implement and use translations in your extensions: |
| 130 | + |
| 131 | +### Translation File Structure |
| 132 | + |
| 133 | +Each extension can have its own `translations.yml` file located at `src/extensions/EXT_NAME/translations.yml`. This file follows a specific structure: |
| 134 | + |
| 135 | +```yaml |
| 136 | +commands: |
| 137 | + command_name: |
| 138 | + name: |
| 139 | + en-US: Command Name |
| 140 | + fr: Nom de la Commande |
| 141 | + # ... other languages |
| 142 | + description: |
| 143 | + en-US: Command description |
| 144 | + fr: Description de la commande |
| 145 | + # ... other languages |
| 146 | + options: |
| 147 | + option_name: |
| 148 | + name: |
| 149 | + en-US: Option Name |
| 150 | + fr: Nom de l'Option |
| 151 | + # ... other languages |
| 152 | + description: |
| 153 | + en-US: Option description |
| 154 | + fr: Description de l'option |
| 155 | + # ... other languages |
| 156 | + strings: |
| 157 | + response_key: |
| 158 | + en-US: Response text in English |
| 159 | + fr: Texte de réponse en français |
| 160 | + # ... other languages |
| 161 | +
|
| 162 | +strings: |
| 163 | + string1: |
| 164 | + en-US: General string in English |
| 165 | + fr: Chaîne générale en français |
| 166 | + # ... other general strings |
| 167 | +``` |
| 168 | + |
| 169 | +> [!NOTE] |
| 170 | +> The top-level `strings` section (outside of `commands`) is what gets mapped to `config["translations"]`. This section is for general strings not directly tied to specific commands. |
| 171 | + |
| 172 | +### Nested Commands and Sub-commands |
| 173 | + |
| 174 | +For command groups and sub-commands, you can nest the structure using the `commands` key: |
| 175 | + |
| 176 | +```yaml |
| 177 | +commands: |
| 178 | + parent_command: |
| 179 | + name: |
| 180 | + en-US: Parent Command |
| 181 | + # ... other languages |
| 182 | + description: |
| 183 | + en-US: Parent command description |
| 184 | + # ... other languages |
| 185 | + commands: |
| 186 | + sub_command: |
| 187 | + name: |
| 188 | + en-US: Sub Command |
| 189 | + # ... other languages |
| 190 | + description: |
| 191 | + en-US: Sub-command description |
| 192 | + # ... other languages |
| 193 | + # ... options, strings, etc. |
| 194 | +``` |
| 195 | + |
| 196 | +This structure can be nested further for sub-sub-commands if needed. |
| 197 | + |
| 198 | +### Accessing Translations in Code |
| 199 | + |
| 200 | +1. For slash commands, options, and their descriptions, Botkit automatically applies the correct translations based on the guild's preferred locale. |
| 201 | + |
| 202 | +2. For other strings, you can access translations using the `apply_locale` function: |
| 203 | + |
| 204 | +```python |
| 205 | +from src.i18n import apply_locale |
| 206 | +
|
| 207 | +# In your command or event handler: |
| 208 | +translations = apply_locale(config["translations"], message.guild.preferred_locale) |
| 209 | +response = translations.some_key.response_text |
| 210 | +
|
| 211 | +# You can also specify a default locale: |
| 212 | +translations = apply_locale(config["translations"], message.guild.preferred_locale, default="en-US") |
| 213 | +``` |
| 214 | + |
| 215 | +3. In slash commands, translations are available via the `ctx.translations` object: |
| 216 | + |
| 217 | +```python |
| 218 | +from src import custom |
| 219 | +@discord.slash_command(name="ping") |
| 220 | +
|
| 221 | +async def ping(self, ctx: custom.ApplicationContext): |
| 222 | + response = ctx.translations.response.format(latency=round(self.bot.latency * 1000)) |
| 223 | + await ctx.respond(response) |
| 224 | +``` |
| 225 | +> [!NOTE] |
| 226 | +> The translations available under `ctx.translations` are the ones set under `strings` in the command's translation. |
| 227 | + |
| 228 | +### Best Practices |
| 229 | + |
| 230 | +- Provide translations for all supported languages in your `translations.yml` file. |
| 231 | +- Use meaningful keys for your strings to make the code more readable. |
| 232 | +- Consider using placeholders (e.g., `{latency}`) in your translated strings for dynamic content. |
| 233 | +- Always provide at least an English (en-US) translation as a fallback. |
| 234 | + |
| 235 | +By following these guidelines, you can create a bot that seamlessly adapts to different languages, providing a localized experience for users across various Discord servers. |
| 236 | + |
121 | 237 | ## Using Patch Files
|
122 | 238 |
|
123 | 239 | Botkit supports the use of patch files to modify or extend the functionality of the bot or its dependencies before the main extension code runs. This is particularly useful for applying global changes or monkey-patching existing classes.
|
|
0 commit comments