-
Notifications
You must be signed in to change notification settings - Fork 0
Add documentation #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Scyye
wants to merge
3
commits into
master
Choose a base branch
from
documentation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,103 +1,262 @@ | ||
| # Bot Commons | ||
| A framework for creating bots for Discord using [JDA](https://github.com/DV8FromTheWorld/JDA) | ||
| # BotCommons | ||
|
|
||
| A powerful framework for creating Discord bots using [JDA (Java Discord API)](https://github.com/DV8FromTheWorld/JDA) that simplifies common bot development tasks. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| - [Features](#features) | ||
| - [Installation](#installation) | ||
| - [Commands Framework](#commands-framework) | ||
| - [Config Framework](#config-framework) | ||
| - [Menu Framework](#menu-framework) | ||
| - [Cache Framework](#cache-framework) | ||
|
|
||
| ## Features | ||
|
|
||
| BotCommons provides several key features to simplify Discord bot development: | ||
|
|
||
| - **Commands Framework**: Easy creation and management of slash commands with parameter validation | ||
| - **Config Framework**: Simple configuration management for global and per-guild settings | ||
| - **Menu Framework**: Interactive menu system for user interactions | ||
| - **Cache Framework**: Efficient caching of Discord entities like messages, guilds, channels and members | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Maven | ||
|
|
||
| Add the following to your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>dev.scyye</groupId> | ||
| <artifactId>BotCommons</artifactId> | ||
| <version>1.11</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| ### Gradle | ||
|
|
||
| Add the following to your `build.gradle`: | ||
|
|
||
| ```groovy | ||
| dependencies { | ||
| implementation 'dev.scyye:BotCommons:1.11' | ||
| } | ||
| ``` | ||
|
|
||
| # Features | ||
| ## Commands Framework | ||
| To create a command, simply create a class that is annotated with `@MethodCommandHolder(string: Optional group)`; | ||
| Then create a function that is annotated with `@MethodCommand()`. (View the `@MethodCommand` class for more information on the parameters.) | ||
| Then, register the command using `MethodCommandManager.addCommands(CommandHolderClass.class)`. | ||
|
|
||
| **Below is an example of a command, along with how to use parameters:** | ||
| The Commands Framework simplifies the creation and management of slash commands. | ||
|
|
||
| ### Creating Commands | ||
|
|
||
| 1. Create a class annotated with `@CommandHolder` | ||
| 2. Create methods annotated with `@Command` | ||
| 3. Register the commands with the `CommandManager` | ||
|
|
||
| ```java | ||
| import botcommons.commands.*; | ||
|
|
||
| // You can also specify a group for the commands in the holder | ||
| @CommandHolder | ||
| public class PingCommand { | ||
| @Command(name = "ping", help = "Pong!") | ||
| public void execute(GenericCommandEvent event, | ||
| @Param( | ||
| description = "A user", | ||
| type = Param.ParamType.USER | ||
| ) | ||
| // the name of the argument is grabbed from the parameter name | ||
| User user) { | ||
| event.replySuccess("Pong! " + user.getAsMention()).finish(message -> { | ||
| // Success consumer | ||
| }); | ||
| } | ||
| @Command(name = "ping", help = "Pong!") | ||
| public void execute(GenericCommandEvent event, | ||
| @Param( | ||
| description = "A user", | ||
| type = Param.ParamType.USER | ||
| ) | ||
| // the name of the argument is grabbed from the parameter name | ||
| User user) { | ||
| event.replySuccess("Pong! " + user.getAsMention()).finish(message -> { | ||
| // Success consumer | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Setting Up CommandManager | ||
|
|
||
| ```java | ||
| public class Main { | ||
| // ... | ||
| public static void main(String[] args) { | ||
| JDA jda = JDABuilder.createDefault("token") | ||
| .addEventListeners(new CommandManager()) | ||
| .build(); | ||
|
|
||
| CommandManager.addCommands(PingCommand.class); | ||
| } | ||
| // ... | ||
| public static void main(String[] args) { | ||
| JDA jda = JDABuilder.createDefault("token") | ||
| .addEventListeners(new CommandManager()) | ||
| .build(); | ||
|
|
||
| // Initialize the CommandManager | ||
| CommandManager.init(jda); | ||
|
|
||
| // Register commands | ||
| CommandManager.addCommands(PingCommand.class); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Parameter Annotations | ||
|
|
||
| Use the `@Param` annotation to define command parameters: | ||
|
|
||
| ```java | ||
| @Param( | ||
| description = "Description of the parameter", | ||
| required = true, // Default is true | ||
| type = Param.ParamType.USER, // Type of parameter (STRING, INTEGER, USER, etc.) | ||
| autocomplete = true // Enable autocomplete (requires implementing AutoCompleteHandler) | ||
| ) | ||
| ``` | ||
|
|
||
| ## Config Framework | ||
| There are 2 types of configs: `Config` and `GuildConfig`. | ||
|
|
||
| `Config` is a config that is shared across all servers.\ | ||
| `GuildConfig` is a config that is specific to a server. | ||
| The Config Framework provides both global and per-guild configuration management. | ||
|
|
||
| ### Global Config | ||
|
|
||
| ### Config | ||
| To create a config, simply add this to your bot: | ||
| ```java | ||
| public static void main(String[] args) { | ||
| // ... | ||
| Config.botName = "BotName"; // This will be used as the name of the config file | ||
| Config config = Config.makeConfig(new HashMap<>(){{ | ||
| put("key", "value"); | ||
| put("another key", new String[]{"Values can be anything", "Such as lists"}); | ||
| put("a third key", new Player("PlayerName", 1000)); // Or even objects | ||
| // Set the bot name for the config file | ||
| Config.botName = "MyBot"; | ||
|
|
||
| // Create a default config | ||
| Config config = Config.makeConfig(new HashMap<>() {{ | ||
| put("prefix", "!"); | ||
| put("admins", new String[]{"userId1", "userId2"}); | ||
| put("defaultSettings", new HashMap<String, Object>() {{ | ||
| put("welcomeMessage", true); | ||
| put("loggingEnabled", false); | ||
| }}); | ||
| }}); | ||
| // ... | ||
|
|
||
| // Access config values | ||
| String prefix = config.get("prefix", String.class); | ||
| boolean loggingEnabled = ((Map<String, Object>)config.get("defaultSettings")).get("loggingEnabled"); | ||
| } | ||
| ``` | ||
|
|
||
| Then, to get a value, simply call `config#get(String key)`. I recommend storing the `Config` instance in a variable. | ||
| ### Guild Config | ||
|
|
||
| ### GuildConfig | ||
| To create a server config, add this listener to your JDA instance, however you wish to do that.: | ||
| ```java | ||
| public static void main(String[] args) { | ||
| // ... | ||
| JDA jda = JDABuilder.createDefault("token") | ||
| .addEventListeners(new ConfigManager(new HashMap<>(){{ | ||
| put("key", "value"); | ||
| put("another key", new String[]{"Values can be anything", "Such as lists"}); | ||
| put("a third key", new Player("PlayerName", 1000)); // Or even objects | ||
| }})) | ||
| .build(); | ||
| // ... | ||
| JDA jda = JDABuilder.createDefault("token") | ||
| .addEventListeners(new ConfigManager(new HashMap<>() {{ | ||
| put("prefix", "!"); | ||
| put("welcomeMessage", true); | ||
| put("loggingEnabled", false); | ||
| }})) | ||
| .build(); | ||
|
|
||
| // Later, get a guild's config | ||
| Config guildConfig = event.getConfig(); | ||
| boolean welcomeMessage = guildConfig.get("welcomeMessage", Boolean.class); | ||
|
|
||
| // Update a value | ||
| ConfigManager.getInstance().setValue(guildId, "welcomeMessage", false); | ||
| } | ||
| ``` | ||
|
|
||
| ### ***__NOTE:__ This will automatically create the `/sql` and `/config` commands*** | ||
| **Note:** This will automatically create the `/sql` and `/config` commands. | ||
|
|
||
| ## Menu Framework | ||
|
|
||
| # Disclaimer | ||
| EVERYTHING AFTER THIS POINT IS NOT ACCURATE. I WILL UPDATE IT SOON. | ||
| The Menu Framework allows you to create interactive menus with buttons and pagination. | ||
|
|
||
| ### Setting Up MenuManager | ||
|
|
||
| ## Menu Framework | ||
| ### ***__NOTE:__ YOU MUST call `JDA.addEventListener(PaginationListener)` to enable the menu framework.*** | ||
| ```java | ||
| public static void main(String[] args) { | ||
| JDA jda = JDABuilder.createDefault("token") | ||
| .build(); | ||
|
|
||
| // Create menu manager instance | ||
| MenuManager menuManager = new MenuManager(jda); | ||
| } | ||
| ``` | ||
|
|
||
| To create a menu, simply call `PaginatedMenuHandler#addMenu(PaginatedMenuHandler#buildMenu())`, and pass in the data for each page. | ||
| ### Creating and Registering Menus | ||
|
|
||
| You can also reply to commands with a menu, by calling `CommandEvent#replyMenu(PaginatedMenuHandler#buildMenu())`. | ||
| ```java | ||
| // Create a simple menu implementation | ||
| IMenu helpMenu = new IMenu() { | ||
| @Override | ||
| public String getMenuId() { | ||
| return "help_menu"; | ||
| } | ||
|
|
||
| @Override | ||
| public MessageCreateAction generateMenu(Object... args) { | ||
| return new MessageCreateAction() | ||
| .setContent("Help Menu") | ||
| .addActionRow( | ||
| Button.primary("prev", "Previous"), | ||
| Button.primary("next", "Next") | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void handleButtonInteraction(ButtonInteractionEvent event) { | ||
| String buttonId = event.getComponentId(); | ||
| if (buttonId.equals("prev")) { | ||
| // Handle previous button | ||
| } else if (buttonId.equals("next")) { | ||
| // Handle next button | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Register the menu | ||
| menuManager.registerMenu(helpMenu); | ||
|
|
||
| ### ***__NOTE:__ YOU MUST call `JDA.addEventListener(PaginationListener)` to enable the menu framework.*** | ||
| // Send the menu to a channel | ||
| menuManager.sendMenu("help_menu", channelId); | ||
|
|
||
| // Or reply to a command with a menu | ||
| event.replyMenu("help_menu").finish(); | ||
| ``` | ||
|
|
||
| ## Cache Framework | ||
| ### TODO | ||
|
|
||
| The Cache Framework allows efficient caching of Discord entities for improved performance. | ||
|
|
||
| ### Initializing Cache | ||
|
|
||
| ```java | ||
| public static void main(String[] args) { | ||
| JDA jda = JDABuilder.createDefault("token") | ||
| .build(); | ||
|
|
||
| // Initialize cache with various options | ||
| CacheManager.init( | ||
| jda, | ||
| true, // Cache guild members | ||
| true, // Cache mutual guilds | ||
| true, // Cache channel messages | ||
| true, // Cache user messages | ||
| true // Cache users | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### Accessing Cached Data | ||
|
|
||
| ```java | ||
| // Access cached members for a guild | ||
| List<CacheManager.MemberStructure> members = CacheManager.guildMemberCache.get(guildId); | ||
|
|
||
| // Access cached messages | ||
| List<CacheManager.MessageStructure> messages = CacheManager.channelMessageCache.get(channelId); | ||
|
|
||
| // Update the cache to JSON files | ||
| CacheManager.update(); | ||
| ``` | ||
|
|
||
| ## Contributing | ||
|
|
||
| Contributions are welcome! Please feel free to submit a Pull Request. | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under the MIT License - see the LICENSE file for details. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Minor style improvement suggestion.
Consider replacing "feel free to" with a more direct phrase for a more professional tone.
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[style] ~258-~258: Consider using a less common alternative to make your writing sound more unique and professional.
Context: ...ontributing Contributions are welcome! Please feel free to submit a Pull Request. ## License Thi...
(FEEL_FREE_TO_STYLE_ME)
🤖 Prompt for AI Agents