Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This Class handles reading and writing of POGOs
- public class FileHandler {
- public void createDirectory(String dirName) {
- File file = new File(dirName);
- if (!file.exists()) {
- file.mkdirs();
- }
- }
- public Object readfromJson(String file, Class<?> objClass){
- Gson gson = new Gson();
- try (Reader reader = new FileReader(file)) {
- Object newObject = gson.fromJson(reader, objClass);
- return newObject;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- public void writetoJson(String file, Object object){
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- try (FileWriter writer = new FileWriter(file)) {
- gson.toJson(object, writer);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //Call this in onGuildcreate event
- //init Custom Commands
- file = "CommandLists/" + readableGuildID + "_CustomCommands.json";
- CustomCommands customCommands = new CustomCommands();
- if (!Files.exists(Paths.get(file))) {
- handler.writetoJson(file, customCommands);
- logger.info("Creating Custom Commands List");
- }
- //to get the data
- //loads the custom commands for the Guild.
- file = "CommandLists/" + readableGuildID + "_CustomCommands.json";
- CustomCommands customCommands = (CustomCommands) handler.readfromJson(file, CustomCommands.class);
- //Custom commands Class as an example
- public class CustomCommands {
- ArrayList<String[]> commands = new ArrayList<String[]>();
- final String commandNotFound = "No Command with that name found.";
- public String createCommand(String userID, String commandName, String response){
- boolean noDuplicate = true;
- for (String[] sA : commands){
- if (sA[1].equalsIgnoreCase(commandName)){
- noDuplicate = false;
- }
- }
- if (noDuplicate) {
- String[] newEntry = new String[3];
- newEntry[0] = userID;
- newEntry[1] = commandName;
- newEntry[2] = response;
- commands.add(newEntry);
- return "Command Added";
- }
- return "A Command with that name already exists, Cannot create command.";
- }
- public String getCommand(String commandName){
- for (String[] cA: commands){
- if (cA[1].equalsIgnoreCase(commandName)){
- return cA[2];
- }
- }
- return commandNotFound;
- }
- public String removeCommand(boolean isMod, String userID, String commandName){
- for (String[] sA : commands){
- if (sA[1].equalsIgnoreCase(commandName)){
- if (sA[0].equalsIgnoreCase("LockedCommand")){
- return "This command cannot be removed";
- }
- if (isMod || sA[0].equals(userID)){
- commands.remove(sA);
- return "Command Removed";
- }
- return "You Do not have Permission to Remove this command";
- }
- }
- return "A command with that name does not exist";
- }
- public String listCommands(){
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append("Here is the List of Custom Commands");
- for (String[] sA : commands){
- stringBuilder.append("\n "+ sA[1]);
- }
- return stringBuilder.toString();
- }
- }
- //how i call the command
- if (message.toString().toLowerCase().startsWith(Globals.CCPrefix.toLowerCase())) {
- StringBuilder CCName = new StringBuilder();
- CCName.append(message.toString());
- CCName.delete(0, Globals.CCPrefix.length());
- String CCResponse = customCommands.getCommand(CCName.toString());
- CCResponse = CCResponse.replaceAll("#author#", message.getAuthor().getName());
- channel.sendMessage(CCResponse);
- }
Advertisement
Add Comment
Please, Sign In to add comment