Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static void initialize() throws LoginException {
new MuteCommand(),
new MutedCommand(),
new UnmuteCommand(),
new LockCommand(),
new VerifyCommand(),
new PollCommand(),
new IdeaCommand(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static ParsedArgumentSet parseArgs(Command command, String[] args, Comman
rawArguments.pushStack();
} catch (MissingArgumentException exception) {
if (argumentContainer.isOptional()) {
parsedArgs.put(identifier, new ParsedArgument<>(identifier, argumentContainer.getDefaultValue()));
parsedArgs.put(identifier, new ParsedArgument<>(identifier, argumentContainer.getDefaultValue(event)));
} else {
exception.setContext(command, i, event);
throw exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ParsedArgument<?> parse(String identifier, ArgumentStack.RawArgumentStack

for (int i = 0; i < arguments; i++) {
try {
approvedArgumentValues.add(arg.parseValue(rawArgs, event));
approvedArgumentValues.add(arg.parsed(rawArgs, event));
} catch (Exception e) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.*;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.SingleArgumentContainer;
import com.diamondfire.helpbot.bot.command.argument.impl.types.Argument;
import com.diamondfire.helpbot.bot.command.argument.impl.types.*;
import com.diamondfire.helpbot.bot.events.CommandEvent;

import java.util.Deque;
Expand All @@ -20,10 +20,10 @@ public ParsedArgument<?> parse(String identifier, ArgumentStack.RawArgumentStack
Deque<String> rawArgs = args.popStack();
Argument<A> arg = getContainer().getArgument();

if (rawArgs.peek() == null) {
if (rawArgs.peek() == null && !(arg instanceof FallbackArgument<?>)) {
throw new MissingArgumentException("Expected an argument, but got nothing.");
}

return new ParsedArgument<>(identifier, arg.parseValue(rawArgs, event));
return new ParsedArgument<>(identifier, arg.parsed(rawArgs, event));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.diamondfire.helpbot.bot.command.argument.impl.parsing.types;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.parser.ArgumentParser;
import com.diamondfire.helpbot.bot.events.CommandEvent;

import java.util.function.Function;

// Argument containers provide essential information for the argument.
// These give a parser.

/**
* Argument containers provide essential information for the argument.
* These give a parser.
* @param <T>
*/
public abstract class ArgumentContainer<T> {

private T defaultValue;
private Function<CommandEvent, T> defaultValueFunction;
private boolean isOptional;

public abstract <P extends ArgumentParser<?, T>> P getParser();

public ArgumentContainer<T> optional(T defaultValue) {
this.defaultValue = defaultValue;
public ArgumentContainer<T> optional(Function<CommandEvent, T> defaultValueFunction) {
this.defaultValueFunction = defaultValueFunction;
this.isOptional = true;
return this;
}
Expand All @@ -22,8 +28,12 @@ public boolean isOptional() {
return isOptional;
}

public T getDefaultValue() {
return defaultValue;
public Function<CommandEvent, T> getDefaultValueFunction() {
return defaultValueFunction;
}

public T getDefaultValue(CommandEvent event) {
return getDefaultValueFunction().apply(event);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

import java.util.Deque;

public abstract class AbstractSimpleValueFallbackArgument<T> implements FallbackArgument<T> {

@Override
@SuppressWarnings("all")
public T parseValue(@NotNull Deque<String> args, CommandEvent event) throws ArgumentException {
return parse(args.peek(), event);
}

protected abstract T parse(@NotNull String argument, CommandEvent event) throws ArgumentException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
*/
public interface Argument<T> {

default T parsed(@NotNull Deque<String> args, CommandEvent event) throws ArgumentException {
return parseValue(args, event);
}

T parseValue(@NotNull Deque<String> args, CommandEvent event) throws ArgumentException;


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

import java.util.Deque;

public interface FallbackArgument<T> extends Argument<T> {

@Override
default T parsed(@NotNull Deque<String> args, CommandEvent event) throws ArgumentException {
T result;

try {
result = Argument.super.parsed(args, event);
args.pop();

} catch (Exception e) {
result = null;
}

if (result == null) {
return ifFail(event);

} else {
return result;
}
}


/**
* The value to return if the parsed value throws an exception or is null.
* @param event The Command event.
* @return The new value.
*/
T ifFail(CommandEvent event);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.diamondfire.helpbot.bot.events.CommandEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractOffsetArgument;

import java.util.Calendar;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;


import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.Argument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import com.diamondfire.helpbot.util.JaroWinkler;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.MalformedArgumentException;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import net.dv8tion.jda.api.utils.MiscUtil;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.diamondfire.helpbot.bot.command.argument.impl.types.Argument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types.Enum;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl.Enum;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
Expand All @@ -10,11 +10,10 @@

public class EnumArgument<E extends Enum<E> & EnumArgument.InputEnum> extends AbstractSimpleValueArgument<E> {

private EnumSet<E> associatedEnum;
private final EnumSet<E> associatedEnum;

public EnumArgument<E> setEnum(Class<E> anEnum) {
public EnumArgument(Class<E> anEnum) {
associatedEnum = EnumSet.allOf(anEnum);
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types.Enum;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl.Enum;

import com.diamondfire.helpbot.sys.tag.Tag;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

public class LongArgument extends AbstractSimpleValueArgument<Long>{
public class LongArgument extends AbstractSimpleValueArgument<Long> {

@Override
protected Long parse(@NotNull String argument, CommandEvent event) throws ArgumentException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.Argument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
import com.diamondfire.helpbot.bot.command.impl.*;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.*;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Collectors;

/**
* <strong>This argument extends {@link FallbackArgument}.</strong>
*
* Will return the channel this command was executed in if an invalid channel was provided by the user.
*/
public class TextChannelArgument extends AbstractSimpleValueFallbackArgument<TextChannel> {

private final Permission[] requiredChannelPermissions;

public TextChannelArgument() {
this(Permission.EMPTY_PERMISSIONS);
}

public TextChannelArgument(Permission... requiredChannelPermissions) {
this.requiredChannelPermissions = requiredChannelPermissions;
}

@Override
protected TextChannel parse(@NotNull String argument, CommandEvent event) throws ArgumentException {

List<TextChannel> mentionedChannels = event.getMessage().getMentionedChannels();

for (TextChannel channel : mentionedChannels) {
if (channel.getAsMention().equals(argument) || channel.getId().equals(argument)) {
if (!event.getGuild().getSelfMember().hasPermission(channel, requiredChannelPermissions)) {

throw new MalformedArgumentException(
"I need the following permissions in this channel: " +
getFormattedPermissionList(requiredChannelPermissions)
);
}

return channel;
}
}

throw new MalformedArgumentException("Invalid channel provided. Must be a valid mention or ID.");
}

@Override
public TextChannel ifFail(CommandEvent event) {
return event.getChannel();
}

public static @NotNull String getFormattedPermissionList(Permission... permissions) {
return "`" + Arrays.stream(permissions)
.map(Permission::getName)
.collect(Collectors.joining("` `"))
+ "`";
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl;

import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractOffsetArgument;

import java.util.Calendar;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types.minecraft;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl.minecraft;

import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.*;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types.minecraft;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl.minecraft;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types.minecraft;
package com.diamondfire.helpbot.bot.command.argument.impl.types.impl.minecraft;

import java.util.UUID;

Expand Down
Loading