Refactored CommandContextBuilder as static builder
This commit is contained in:
parent
a908dd0042
commit
ca93f18f1e
@ -28,15 +28,15 @@ public class CommandContext {
|
||||
private String usedAlias;
|
||||
private String[] args;
|
||||
|
||||
CommandContext(DiscordApi api, Message message, MessageAuthor author, TextChannel channel, Server server, String alias, Command cmd, String[] args) {
|
||||
this.api = api;
|
||||
this.message = message;
|
||||
this.author = author;
|
||||
this.channel = channel;
|
||||
this.server = server;
|
||||
this.usedCommand = cmd;
|
||||
this.usedAlias = alias;
|
||||
this.args = args;
|
||||
private CommandContext(CommandContextBuilder builder) {
|
||||
this.api = builder.api;
|
||||
this.message = builder.message;
|
||||
this.author = builder.author;
|
||||
this.channel = builder.channel;
|
||||
this.server = builder.server;
|
||||
this.usedCommand = builder.usedCommand;
|
||||
this.usedAlias = builder.usedAlias;
|
||||
this.args = builder.args;
|
||||
}
|
||||
|
||||
public DiscordApi getApi() {
|
||||
@ -96,4 +96,39 @@ public class CommandContext {
|
||||
public CompletableFuture<Message> reply(EmbedBuilder 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -123,15 +123,17 @@ public class Guthix implements MessageCreateListener {
|
||||
String[] cmdArgs = registry.getCmdArgs(content);
|
||||
|
||||
Command cmd = registry.findCommand(cmdString).orElse(null); // TODO: default command here
|
||||
CommandContext ctx = new ContextBuilder().setApi(api)
|
||||
.setServer(server)
|
||||
.setAuthor(author)
|
||||
.setChannel(channel)
|
||||
.setMessage(msg)
|
||||
.setArgs(cmdArgs)
|
||||
.setUsedAlias(cmdString)
|
||||
.setUsedCommand(cmd)
|
||||
.build();
|
||||
|
||||
CommandContext ctx = new CommandContext.CommandContextBuilder(
|
||||
api,
|
||||
msg,
|
||||
author,
|
||||
channel,
|
||||
server,
|
||||
cmd,
|
||||
cmdString,
|
||||
cmdArgs
|
||||
).build();
|
||||
|
||||
if (manager.hasPermission(cmd.permission(), ctx)) {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user