Skip to content

Commit

Permalink
Added ResultManagers
Browse files Browse the repository at this point in the history
  • Loading branch information
BreadMoirai committed Nov 13, 2017
1 parent f60d1f2 commit f89d495
Show file tree
Hide file tree
Showing 138 changed files with 1,152 additions and 962 deletions.
2 changes: 1 addition & 1 deletion BreadBotFramework.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="BreadBotFramework" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.github.breadmoirai" external.system.module.version="0.6.0" type="JAVA_MODULE" version="4">
<module external.linked.project.id="BreadBotFramework" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.github.breadmoirai" external.system.module.version="0.7.0" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
Expand Down
39 changes: 24 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ plugins {
id 'com.jfrog.bintray' version '1.7.3'
id 'maven-publish'
id 'signing'
id 'com.github.breadmoirai.github-release' version '1.0.9'
id 'com.github.breadmoirai.github-release' version '1.1.3'
}

final author = 'breadmoirai'
final artifactId = 'breadbot-framework'
group "com.github.${author}"
version '0.6.0'
version '0.7.0'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down Expand Up @@ -63,18 +63,20 @@ task printBody {
}

githubRelease {
// token = getProperty('github.key').toString()
//if (hasProperty('github.key'))
token = getProperty('github.key').toString()
String version = project.version
it.body = body
prerelease = true
FilenameFilter filter = { dir, file -> file.contains(version) }
releaseAssets = jar.destinationDir.listFiles filter
// println releaseAssets.findAll()
}


bintray {
user = author
// key = getProperty('bintray.key').toString()
key = getProperty('bintray.key').toString()
publications = ["BintrayRelease"]
publish = true
pkg {
Expand Down Expand Up @@ -125,25 +127,32 @@ task jarMake {
dependsOn shadowJar
}

task docRepo() {
def docDir = file('../breaddocs')
def repoDir = file(docDir.getAbsolutePath() + '/breadbotframework')
if (docDir.mkdir() || (docDir.exists() && !repoDir.exists())) {
'git clone https://github.com/breadmoirai/breadbotframework.git'.execute(null, docDir).waitFor()
'git checkout --orphan gh-pages'.execute(null, repoDir).waitFor()
}
'git rm -rf .'.execute(null, repoDir)
}

task javadocDir(type: Copy) {
delete('docs')
dependsOn docRepo
dependsOn javadoc
from javadoc.destinationDir
into 'docs'
into '../breaddocs/breadbotframework'
}

task publishJavadoc(type: Exec) {
task publishJavadoc() {
//this doesn't work. Just gonna go manually for now. Ugh
group 'publishing'
dependsOn javadoc
dependsOn javadocDir
commandLine 'cmd', "cd ${javadocDir.destinationDir.absolutePath}"
commandLine 'cmd', "git checkout -t origin/gh-pages"
commandLine 'cmd', "git add ."
commandLine 'cmd', "git add -u"
commandLine 'cmd', "git commit -m \"JavadocWebsite ver ${project.version} on ${new Date()}\""
commandLine 'cmd', "git push origin gh-pages"
commandLine 'cmd', "cd .."
commandLine 'cmd', "rm -rf ${javadocDir.destinationDir.absolutePath}"
def dir = file('../breaddocs/breadbotframework')
'git add .'.execute(null, dir)
"git commit -m \"JavadocWebsite ver ${project.version} on ${new Date()}\"".execute(null, dir)
'git push origin gh-pages'.execute(null, dir)
}

task wrapper(type: Wrapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
*/
package com.github.breadmoirai.breadbot.framework;

import com.github.breadmoirai.breadbot.framework.command.parameter.ArgumentParser;
import com.github.breadmoirai.breadbot.framework.command.parameter.ArgumentTypeMapper;
import com.github.breadmoirai.breadbot.framework.command.parameter.ArgumentTypePredicate;
import com.github.breadmoirai.breadbot.framework.command.parameter.CommandArgument;
import com.github.breadmoirai.breadbot.framework.internal.ArgumentTypes;
import com.github.breadmoirai.breadbot.framework.internal.parameter.ArgumentParser;
import com.github.breadmoirai.breadbot.framework.internal.parameter.ArgumentTypeMapper;
import com.github.breadmoirai.breadbot.framework.internal.parameter.ArgumentTypePredicate;
import com.github.breadmoirai.breadbot.framework.internal.parameter.CommandArgument;

import java.util.function.Function;
import java.util.function.Predicate;
Expand All @@ -37,7 +36,7 @@ public interface ArgumentTypesBuilder<R> {
<T> R registerArgumentMapper(Class<T> type, ArgumentTypePredicate predicate, ArgumentTypeMapper<T> mapper);

/**
* This ignores flags. Use {@link ArgumentTypes#registerArgumentMapper} otherwise.
* This ignores flags. Use {@link ArgumentTypesBuilder#registerArgumentMapper} otherwise.
*
* @param type The type class
* @param isType predicate to test if the argument can be parsed to the type provided. This param can be left {@code null} if the complexity is close to {@code getAsType.apply(arg) != null}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.breadmoirai.breadbot.framework;

import com.github.breadmoirai.breadbot.framework.internal.parameter.ArgumentParser;

public interface ArgumentTypesManager {

/**
* Returns the predicate mapper pair registered if found.
*
* @param type the class of the type as it was registered or one of the default types.
* @param <T> the type
* @return an ArgumentParser if found. Else {@code null}.
*/
<T> ArgumentParser<T> getParser(Class<T> type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
*/
package com.github.breadmoirai.breadbot.framework;

import com.github.breadmoirai.breadbot.framework.command.CommandHandle;
import com.github.breadmoirai.breadbot.framework.internal.ArgumentTypes;
import com.github.breadmoirai.breadbot.framework.response.CommandResponse;
import com.github.breadmoirai.breadbot.framework.response.CommandResponseManager;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.hooks.IEventManager;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public interface BreadBotClient {

Expand All @@ -49,32 +47,13 @@ public interface BreadBotClient {

IEventManager getEventManager();

ArgumentTypes getArgumentTypes();
ArgumentTypesManager getArgumentTypes();

default void send(Response response) {
send(response.getChannelId(), response);
}
CommandResponseManager getResponseManager();

default void send(long channeId, Response response) {
TextChannel textChannel = getJDA().getTextChannelById(channeId);
if (textChannel == null) return;
send(textChannel, response);
}
CommandResultManager getResultManager();


default void send(TextChannel textChannel, Response response) {
Objects.requireNonNull(textChannel, "TextChannel");
Objects.requireNonNull(response, "Response");
response.base(0, textChannel.getIdLong(), textChannel.getGuild().getIdLong(), 0, this);
response.send(textChannel);
}

default void send(User user, Response response) {
Objects.requireNonNull(user, "User");
Objects.requireNonNull(user, "Response");
response.setClient(this);
user.openPrivateChannel().queue(response::send);
}
void sendResponse(CommandResponse response, MessageChannel targetChannel);

Map<String, CommandHandle> getCommandMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,19 @@
*/
package com.github.breadmoirai.breadbot.framework;

import com.github.breadmoirai.breadbot.framework.command.CommandPreprocessor;
import com.github.breadmoirai.breadbot.framework.command.CommandPreprocessorFunction;
import com.github.breadmoirai.breadbot.framework.command.CommandPreprocessorPredicate;
import com.github.breadmoirai.breadbot.framework.command.builder.CommandHandleBuilder;
import com.github.breadmoirai.breadbot.framework.command.builder.CommandHandleBuilderFactoryImpl;
import com.github.breadmoirai.breadbot.framework.command.builder.CommandHandleBuilderInternal;
import com.github.breadmoirai.breadbot.framework.command.builder.CommandParameterBuilder;
import com.github.breadmoirai.breadbot.framework.command.parameter.ArgumentParser;
import com.github.breadmoirai.breadbot.framework.command.parameter.ArgumentTypeMapper;
import com.github.breadmoirai.breadbot.framework.command.parameter.ArgumentTypePredicate;
import com.github.breadmoirai.breadbot.framework.command.parameter.CommandArgument;
import com.github.breadmoirai.breadbot.framework.event.CommandEvent;
import com.github.breadmoirai.breadbot.framework.event.ICommandEventFactory;
import com.github.breadmoirai.breadbot.framework.event.impl.CommandEventFactoryImpl;
import com.github.breadmoirai.breadbot.framework.internal.ArgumentTypes;
import com.github.breadmoirai.breadbot.framework.internal.ArgumentTypesImpl;
import com.github.breadmoirai.breadbot.framework.defaults.DefaultCommandResponseManager;
import com.github.breadmoirai.breadbot.framework.internal.BreadBotClientImpl;
import com.github.breadmoirai.breadbot.framework.internal.CommandPropertiesImpl;
import com.github.breadmoirai.breadbot.framework.internal.argument.ArgumentTypesManagerImpl;
import com.github.breadmoirai.breadbot.framework.internal.command.CommandPropertiesManagerImpl;
import com.github.breadmoirai.breadbot.framework.internal.command.CommandResultManagerImpl;
import com.github.breadmoirai.breadbot.framework.internal.command.builder.CommandHandleBuilderFactoryImpl;
import com.github.breadmoirai.breadbot.framework.internal.command.builder.CommandHandleBuilderInternal;
import com.github.breadmoirai.breadbot.framework.internal.event.CommandEventFactoryImpl;
import com.github.breadmoirai.breadbot.framework.internal.parameter.ArgumentParser;
import com.github.breadmoirai.breadbot.framework.internal.parameter.ArgumentTypeMapper;
import com.github.breadmoirai.breadbot.framework.internal.parameter.ArgumentTypePredicate;
import com.github.breadmoirai.breadbot.framework.internal.parameter.CommandArgument;
import com.github.breadmoirai.breadbot.framework.response.CommandResponseManager;
import com.github.breadmoirai.breadbot.modules.prefix.DefaultPrefixModule;
import com.github.breadmoirai.breadbot.modules.prefix.PrefixModule;
import net.dv8tion.jda.core.entities.Message;
Expand All @@ -48,24 +43,29 @@ public class BreadBotClientBuilder implements
CommandHandleBuilderFactory<BreadBotClientBuilder>,
CommandModuleBuilder<BreadBotClientBuilder>,
CommandPropertiesBuilder<BreadBotClientBuilder>,
ArgumentTypesBuilder<BreadBotClientBuilder> {
ArgumentTypesBuilder<BreadBotClientBuilder>,
CommandResultManagerBuilder<BreadBotClientBuilder> {

// private static final Logger LOG = LoggerFactory.getLogger(BreadBotClientBuilder.class);

private final List<CommandModule> modules;
private final CommandProperties commandProperties;
private final ArgumentTypes argumentTypes;
private final CommandPropertiesManagerImpl commandProperties;
private final ArgumentTypesManagerImpl argumentTypes;
private final CommandHandleBuilderFactoryImpl factory;
private final List<CommandHandleBuilderInternal> commands;
private final CommandResultManagerImpl resultManager;
private CommandResponseManager responseManager;
private Predicate<Message> preProcessPredicate;
private ICommandEventFactory commandEventFactory;

public BreadBotClientBuilder() {
modules = new ArrayList<>();
commandProperties = new CommandPropertiesImpl();
argumentTypes = new ArgumentTypesImpl();
commandProperties = new CommandPropertiesManagerImpl();
argumentTypes = new ArgumentTypesManagerImpl();
factory = new CommandHandleBuilderFactoryImpl(this);
commands = new ArrayList<>();
resultManager = new CommandResultManagerImpl();
responseManager = new DefaultCommandResponseManager();
}

@Override
Expand Down Expand Up @@ -314,6 +314,17 @@ public <T> ArgumentParser<T> getParser(Class<T> type) {
return argumentTypes.getParser(type);
}

@Override
public <T> BreadBotClientBuilder registerResultHandler(Class<T> resultType, CommandResultHandler<T> handler) {
resultManager.registerResultHandler(resultType, handler);
return this;
}

@Override
public <T> CommandResultHandler<? super T> getResultHandler(Class<T> resultType) {
return resultManager.getResultHandler(resultType);
}

/**
* Sets a predicate to be used on each message before processing it. This will override any existing predicates.
*
Expand Down Expand Up @@ -348,6 +359,15 @@ public BreadBotClientBuilder setEventFactory(ICommandEventFactory commandEventFa
return this;
}

/**
* Sets the manager that handles sending responses. If this field is not set, the CommandResponseManager will default to {@link com.github.breadmoirai.breadbot.framework.defaults.DefaultCommandResponseManager}
*
* @param responseManager An implementation of CommandResponseManager.
*/
public void setResponseManager(CommandResponseManager responseManager) {
this.responseManager = responseManager;
}

/**
* Builds a BreadBotClient with an {@link net.dv8tion.jda.core.hooks.AnnotatedEventManager}
* <p>
Expand Down Expand Up @@ -378,7 +398,6 @@ public BreadBotClient buildInterfaced() {

/**
* Builds the BreadBotClient with the provided EventManager.
* It is at this point that all Modules are initialized and Commands built.
* If an {@link PrefixModule} has not been provided, a {@link com.github.breadmoirai.breadbot.modules.prefix.DefaultPrefixModule new DefaultPrefixModule("!")} is provided.
*
* @param eventManager The IEventManager of which to attach all the listeners (CommandModules) to. If the module is an instanceof {@link net.dv8tion.jda.core.hooks.InterfacedEventManager} it will only use {@link IEventManager#register(Object)} on Modules that extend {@link net.dv8tion.jda.core.hooks.EventListener}. Otherwise, the BreadBotClient will register all the CommandModules as listeners.
Expand All @@ -388,8 +407,7 @@ public BreadBotClient build(IEventManager eventManager) {
if (!hasModule(PrefixModule.class)) modules.add(new DefaultPrefixModule("!"));
if (commandEventFactory == null)
commandEventFactory = new CommandEventFactoryImpl(getModule(PrefixModule.class));

BreadBotClient client = new BreadBotClientImpl(modules, commands, commandProperties, argumentTypes, eventManager, commandEventFactory, preProcessPredicate);
BreadBotClient client = new BreadBotClientImpl(modules, commands, commandProperties, resultManager, argumentTypes, eventManager, commandEventFactory, preProcessPredicate, responseManager);
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.breadmoirai.breadbot.framework.command;
package com.github.breadmoirai.breadbot.framework;

import java.lang.annotation.*;

Expand Down
Loading

0 comments on commit f89d495

Please sign in to comment.