Skip to content

Commit

Permalink
Update to v7.0.0 (#20)
Browse files Browse the repository at this point in the history
* Generify concatenator and add converter to it

* Update example

* Remove deprecated code

* Fix line breaks

* Concatenator now only gets a list of values from ArgumentResult that contains the value stored in the optional in case of optional arguments instead of the optional type itself

* Update readme
  • Loading branch information
S3nS3IW00 authored May 12, 2023
1 parent 7802752 commit eea7bad
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 258 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# JCommands ![Maven Central](https://img.shields.io/maven-central/v/io.github.s3ns3iw00/jcommands?label=Version) [![](https://img.shields.io/badge/Javadoc-Latest-green)](https://s3ns3iw00.github.io/JCommands/javadoc/) [![](https://img.shields.io/badge/Javacord-3.8.0-blue)](https://github.com/Javacord/Javacord)
# JCommands [![Maven Central](https://img.shields.io/maven-central/v/io.github.s3ns3iw00/jcommands?label=Latest%20version)](https://github.com/S3nS3IW00/JCommands/releases/latest) [![](https://img.shields.io/badge/Javadoc-Latest-green)](https://s3ns3iw00.github.io/JCommands/javadoc/) [![](https://img.shields.io/badge/Javacord-3.8.0-blue)](https://github.com/Javacord/Javacord)

With this Javacord extension you can create Slash Commands on your Discord server within 1 minute with built-in
- input validating,
Expand Down Expand Up @@ -26,15 +26,15 @@ searchArgument.addAutocomplete(searchAutocomplete);
```
- concatenating
```java
command.addConcatenator(new StringConcatenator(" "), stringArgument, searchArgument);
command.addConcatenator(new StringConcatenator<>(" "), stringArgument, searchArgument);
```

There are so many useful pre-written argument types that can be used while creating a command.

### Example command
This command is created just with about 50 lines of code including the response and error handling. It has a validated
argument with regex, an argument that has two different values that the user can choose from, a number argument that has
a range validation, a normal user mention argument, a normal channel mention argument and an optional attachment argument
This command is created just with about 50 lines of code including the response and error handling. It has a validated
argument with regex, an argument that has two different values that the user can choose from, a number argument that has
a range validation, a normal user mention argument, a normal channel mention argument and an optional attachment argument
with size and extension validation.

![Example](https://imgur.com/swqZYXH.png)
Expand All @@ -43,12 +43,12 @@ with size and extension validation.
## Wiki

You can find all the information about how to use `JCommands` on the [Wiki](https://github.com/S3nS3IW00/JCommands/wiki)
You can find all the information about how to use `JCommands` on the [Wiki](https://github.com/S3nS3IW00/JCommands/wiki)
page.

## Dependencies

This is a Javacord extension. Javacord is a Discord bot framework and it so much easier to create
This is a Javacord extension. Javacord is a Discord bot framework and it so much easier to create
bots with it.

- [Javacord](https://github.com/Javacord/Javacord)
Expand All @@ -74,6 +74,6 @@ dependencies {

## Contact

If you found a bug, or you just want a new feature in the next version, don't hesitate to report it on
the [issues](https://github.com/S3nS3IW00/JCommands/issues) page or feel free to open a pull request. You can contact me through
If you found a bug, or you just want a new feature in the next version, don't hesitate to report it on
the [issues](https://github.com/S3nS3IW00/JCommands/issues) page or feel free to open a pull request. You can contact me through
discord: [🆂🅴🅽🆂🅴🆈#0054](https://discord.com/users/249674530077802496)
12 changes: 11 additions & 1 deletion src/main/java/me/s3ns3iw00/jcommands/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,18 @@ private static void handleCommand(SlashCommandInteraction interaction) {
/* Runs the concatenating process and adds its result to the results at the index of the first argument in the concatenation process
Replaces the concatenated arguments to the result of the concatenation
*/
Optional<ArgumentResultConverter> resultConverter = concatenator.getResultConverter();
ArgumentResult result = new ArgumentResult(concatenator.getResultType(),
concatenator.concatenate(concatenateResults.toArray(new ArgumentResult[0])));
concatenator.concatenate(concatenateResults.stream().map(res -> {
/* Extracts the value from optionals
*/
if (res.get() instanceof Optional) {
Optional<?> resOptional = res.get();

return resOptional.orElse(null);
}
return res.get();
}).filter(Objects::nonNull).toArray()), resultConverter.orElse(null));
results.add(results.indexOf(concatenateResults.get(0)), result);
concatenateResults.forEach(results::remove);
concatenated.put(result, concatenatedArguments);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@
package me.s3ns3iw00.jcommands.argument.concatenation;

import me.s3ns3iw00.jcommands.argument.ArgumentResult;
import me.s3ns3iw00.jcommands.argument.converter.ArgumentResultConverter;

import java.util.Optional;

/**
* Concatenates the arguments' result to a type of value that based on the concatenation process
* The process is implemented by subclasses overriding {@link Concatenator#concatenate(ArgumentResult...)} method
* The process is implemented by subclasses overriding {@link Concatenator#concatenate(Object...)} method
*
* @param <C> the type of the concatenated result
* @param <R> the type of the argument result
*/
public abstract class Concatenator {
public abstract class Concatenator<C, R> {

private final Class<?> resultType;
private final Class<R> resultType;
private ArgumentResultConverter<C, R> resultConverter;

/**
* Constructs the class with the default requirements
*
* @param resultType the type of the result of the concatenation
*/
public Concatenator(Class<?> resultType) {
public Concatenator(Class<R> resultType) {
this.resultType = resultType;
}

Expand All @@ -42,9 +49,17 @@ public Concatenator(Class<?> resultType) {
*
* @return the result of the concatenation
*/
public abstract Object concatenate(ArgumentResult... results);
public abstract C concatenate(Object... results);

public Class<?> getResultType() {
public Class<R> getResultType() {
return resultType;
}

public void convertResult(ArgumentResultConverter<C, R> resultConverter) {
this.resultConverter = resultConverter;
}

public Optional<ArgumentResultConverter<C, R>> getResultConverter() {
return Optional.ofNullable(resultConverter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@
*/
package me.s3ns3iw00.jcommands.argument.concatenation.type;

import me.s3ns3iw00.jcommands.argument.ArgumentResult;
import me.s3ns3iw00.jcommands.argument.concatenation.Concatenator;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
* Represents a type of {@link Concatenator} that concatenates the arguments' result to a {@link String} separated with a delimiter
*
* @param <R> the type of the argument result
*/
public class StringConcatenator extends Concatenator {
public class StringConcatenator<R> extends Concatenator<String, R> {

private final String delimiter;

public StringConcatenator(String delimiter) {
super(String.class);
public StringConcatenator(String delimiter, Class<R> resultType) {
super(resultType);
this.delimiter = delimiter;
}

Expand All @@ -43,8 +44,8 @@ public StringConcatenator(String delimiter) {
* @return the concatenated String
*/
@Override
public Object concatenate(ArgumentResult... results) {
return Arrays.stream(results).map(result -> result.get().toString()).collect(Collectors.joining(delimiter));
public String concatenate(Object... results) {
return Arrays.stream(results).map(Object::toString).collect(Collectors.joining(delimiter));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package me.s3ns3iw00.jcommands.argument.concatenation.type;

import me.s3ns3iw00.jcommands.argument.ArgumentResult;
import me.s3ns3iw00.jcommands.argument.concatenation.Concatenator;

import java.lang.reflect.Constructor;
Expand All @@ -29,16 +28,22 @@
* Represents a type of {@link Concatenator}
* <p>
* Concatenates the arguments' result into a specific type with creating a new instance of it
* See {@link TypeConcatenator#createInstance(Class, ArgumentResult...)} for more information
* See {@link TypeConcatenator#createInstance(Class, Object...)} for more information
*/
public class TypeConcatenator extends Concatenator {
public class TypeConcatenator<C, R> extends Concatenator<C, R> {

public TypeConcatenator(Class<?> resultType) {
public TypeConcatenator(Class<R> resultType) {
super(resultType);
}

/**
* Returns result of {@link TypeConcatenator#createInstance(Class, Object...)}
*
* @param results the results of the arguments
* @return the instance
*/
@Override
public Object concatenate(ArgumentResult... results) {
public C concatenate(Object... results) {
try {
return createInstance(getResultType(), results);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
Expand All @@ -59,7 +64,7 @@ public Object concatenate(ArgumentResult... results) {
* @return the instance of the class
* @throws NullPointerException when the clazz doesn't contain constructor that matches with parameters count, type and order
*/
public static Object createInstance(Class<?> clazz, ArgumentResult... parameters)
private C createInstance(Class<?> clazz, Object... parameters)
throws InvocationTargetException, InstantiationException, IllegalAccessException {
int i = 0;
boolean match = false;
Expand All @@ -71,7 +76,7 @@ public static Object createInstance(Class<?> clazz, ArgumentResult... parameters
boolean paramMatch = true;
while (j < cons.getParameterCount() && paramMatch) {
Class<?> consParam = cons.getParameterTypes()[j];
Class<?> param = parameters[j].get().getClass();
Class<?> param = parameters[j].getClass();
if ((consParam != param) &&
(!consParam.isPrimitive() ||
((consParam == byte.class && param != Byte.class) ||
Expand Down Expand Up @@ -99,7 +104,7 @@ public static Object createInstance(Class<?> clazz, ArgumentResult... parameters
}

if (i < clazz.getConstructors().length) {
return clazz.getConstructors()[i].newInstance(Arrays.stream(parameters).map(ArgumentResult::get).toArray());
return (C) clazz.getConstructors()[i].newInstance(Arrays.stream(parameters).toArray());
} else {
throw new NullPointerException("No constructor found that matches with parameters count, type and order");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,4 @@ private Optional<String> findExtension(String fileName) {
}
return Optional.of(fileName.substring(lastIndex + 1));
}

/**
* Sets the valid extensions for the attachment
*
* @param validExtensions the valid extensions
* @deprecated use {@link AttachmentArgument#whenInvalidExtension(Set)}
*/
@Deprecated
public void setValidExtensions(String... validExtensions) {
}

/**
* Sets the maximum allowed size for the attachment in bytes
*
* @param maxSize the max size in bytes
* @deprecated use {@link AttachmentArgument#whenAboveMaxSize(int)}
*/
@Deprecated
public void setMaxSize(Integer maxSize) {
}

/**
* Sets the maximum allowed size for the attachment in megabytes
* <br>
* The size will be converted to bytes (multiplying by 1000 two times) and explicit converted to integer,
* therefore the maximum value could be 2147.483647
*
* @param maxSizeInMB the max size in megabytes
* @deprecated use {@link AttachmentArgument#whenAboveMaxSizeInMB(double)}
*/
@Deprecated
public void setMaxSizeInMB(Double maxSizeInMB) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,4 @@ public NumberArgument(String name, String description, Class<O> resultType) {
public ArgumentValidation<Long> whenNotInRange(long min, long max) {
return getArgumentValidator().when(NumberPredicate.notInRage(min, max));
}

/**
* Sets a range for the number
* NOTE: this is an inclusive range
*
* @param min the minimum
* @param max the maximum
* @deprecated use {@link NumberArgument#whenNotInRange(long, long)}
*/
@Deprecated
public void setRange(long min, long max) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,4 @@ public StringArgument(String name, String description, Class<O> resultType) {
public ArgumentValidation<String> whenNotInRange(int min, int max) {
return getArgumentValidator().when(StringLengthPredicate.notInRage(min, max));
}

/**
* Sets the maximum length for the string
* NOTE: this is an inclusive range
*
* @param max the maximum
* @deprecated use {@link StringArgument#whenNotInRange(int, int)}
*/
@Deprecated
public void setMaxLength(int max) {
}
}
Loading

0 comments on commit eea7bad

Please sign in to comment.