Refactored CommandContextBuilder as static builder

This commit is contained in:
Aleksei 2020-02-13 23:41:18 -05:00
parent a908dd0042
commit ca93f18f1e
3 changed files with 55 additions and 91 deletions

View File

@ -28,15 +28,15 @@ public class CommandContext {
private String usedAlias; private String usedAlias;
private String[] args; private String[] args;
CommandContext(DiscordApi api, Message message, MessageAuthor author, TextChannel channel, Server server, String alias, Command cmd, String[] args) { private CommandContext(CommandContextBuilder builder) {
this.api = api; this.api = builder.api;
this.message = message; this.message = builder.message;
this.author = author; this.author = builder.author;
this.channel = channel; this.channel = builder.channel;
this.server = server; this.server = builder.server;
this.usedCommand = cmd; this.usedCommand = builder.usedCommand;
this.usedAlias = alias; this.usedAlias = builder.usedAlias;
this.args = args; this.args = builder.args;
} }
public DiscordApi getApi() { public DiscordApi getApi() {
@ -96,4 +96,39 @@ public class CommandContext {
public CompletableFuture<Message> reply(EmbedBuilder embed) { public CompletableFuture<Message> reply(EmbedBuilder embed) {
return channel.sendMessage(embed); return channel.sendMessage(embed);
} }
public static class CommandContextBuilder {
private DiscordApi api;
private Message message;
private MessageAuthor author;
private TextChannel channel;
private Server server;
private Command usedCommand;
private String usedAlias;
private String[] args;
public CommandContextBuilder(
DiscordApi api,
Message message,
MessageAuthor author,
TextChannel channel,
Server server,
Command usedCommand,
String usedAlias,
String[] args
) {
this.api = api;
this.message = message;
this.author = author;
this.channel = channel;
this.server = server;
this.usedCommand = usedCommand;
this.usedAlias = usedAlias;
this.args = args;
}
public CommandContext build() {
return new CommandContext(this);
}
}
} }

View File

@ -1,73 +0,0 @@
/*
* Copyright (c) 2020. Aleksei Gryczewski
* All rights reserved.
*/
package dev.salmonllama.fsbot.guthix;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageAuthor;
import org.javacord.api.entity.server.Server;
import java.util.Collection;
public class ContextBuilder {
private DiscordApi api;
private Message message;
private MessageAuthor author;
private TextChannel channel;
private Server server;
private Command usedCommand;
private String usedAlias;
private String[] args;
public ContextBuilder() {
}
public ContextBuilder setUsedCommand(Command commandUsed) {
this.usedCommand = commandUsed;
return this;
}
public ContextBuilder setUsedAlias(String alias) {
this.usedAlias = alias;
return this;
}
public ContextBuilder setArgs(String[] args) {
this.args = args;
return this;
}
ContextBuilder setMessage(Message message) {
this.message = message;
return this;
}
ContextBuilder setAuthor(MessageAuthor author) {
this.author = author;
return this;
}
ContextBuilder setChannel(TextChannel channel) {
this.channel = channel;
return this;
}
ContextBuilder setServer(Server server) {
this.server = server;
return this;
}
ContextBuilder setApi(DiscordApi api) {
this.api = api;
return this;
}
CommandContext build() {
return new CommandContext(api, message, author, channel, server, usedAlias, usedCommand, args);
}
}

View File

@ -123,15 +123,17 @@ public class Guthix implements MessageCreateListener {
String[] cmdArgs = registry.getCmdArgs(content); String[] cmdArgs = registry.getCmdArgs(content);
Command cmd = registry.findCommand(cmdString).orElse(null); // TODO: default command here Command cmd = registry.findCommand(cmdString).orElse(null); // TODO: default command here
CommandContext ctx = new ContextBuilder().setApi(api)
.setServer(server) CommandContext ctx = new CommandContext.CommandContextBuilder(
.setAuthor(author) api,
.setChannel(channel) msg,
.setMessage(msg) author,
.setArgs(cmdArgs) channel,
.setUsedAlias(cmdString) server,
.setUsedCommand(cmd) cmd,
.build(); cmdString,
cmdArgs
).build();
if (manager.hasPermission(cmd.permission(), ctx)) { if (manager.hasPermission(cmd.permission(), ctx)) {