Refactored Context with Event and Optional<Server>

This commit is contained in:
Aleksei 2020-02-20 15:07:16 -05:00
parent ba9ed50f11
commit ee58f10dcb
7 changed files with 53 additions and 25 deletions

View File

@ -20,6 +20,7 @@ import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
public class CreateGalleryCommand extends Command { // TODO: This command needs help. public class CreateGalleryCommand extends Command { // TODO: This command needs help.
@Override public String name() { return "Create Gallery"; } @Override public String name() { return "Create Gallery"; }
@ -34,11 +35,18 @@ public class CreateGalleryCommand extends Command { // TODO: This command needs
@Override @Override
public void onCommand(CommandContext ctx) { public void onCommand(CommandContext ctx) {
Server server = ctx.getServer(); Optional<Server> opServer = ctx.getServer();
TextChannel channel = ctx.getChannel(); TextChannel channel = ctx.getChannel();
String[] args = ctx.getArgs(); String[] args = ctx.getArgs();
String targetChannelId = channel.getIdAsString(); String targetChannelId = channel.getIdAsString();
String targetChannelName = channel.asServerChannel().get().getName(); // TODO: un-band-aid this. String targetChannelName = channel.asServerChannel().get().getName(); // TODO: un-band-aid this.
if (!opServer.isPresent()) {
ctx.reply("This command can only be used in a server");
return;
}
Server server = opServer.get();
String targetServerName = server.getName(); String targetServerName = server.getName();
String targetServerId = server.getIdAsString(); String targetServerId = server.getIdAsString();

View File

@ -36,9 +36,13 @@ public class ColorCommand extends Command {
@Override @Override
public void onCommand(CommandContext ctx) { public void onCommand(CommandContext ctx) {
if (!ctx.getServer().isPresent()) {
ctx.reply("This command must be used inside a server.");
return;
}
String[] args = ctx.getArgs(); String[] args = ctx.getArgs();
DiscordApi api = ctx.getApi(); DiscordApi api = ctx.getApi();
Server server = ctx.getServer(); Server server = ctx.getServer().get();
User user = ctx.getUser(); User user = ctx.getUser();
TextChannel channel = ctx.getChannel(); TextChannel channel = ctx.getChannel();

View File

@ -28,7 +28,11 @@ public class ColorsCommand extends Command {
@Override @Override
public void onCommand(CommandContext ctx) { public void onCommand(CommandContext ctx) {
Server server = ctx.getServer(); if (!ctx.getServer().isPresent()) {
ctx.reply("This command must be used in a server.");
return;
}
Server server = ctx.getServer().get();
TextChannel channel = ctx.getChannel(); TextChannel channel = ctx.getChannel();
if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) { if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) {

View File

@ -31,9 +31,13 @@ public class WelcomeMessageCommand extends Command {
@Override @Override
public void onCommand(CommandContext ctx) { public void onCommand(CommandContext ctx) {
if (!ctx.getServer().isPresent()) {
ctx.reply("You must use this command in a server");
return;
}
String[] args = ctx.getArgs(); String[] args = ctx.getArgs();
TextChannel channel = ctx.getChannel(); TextChannel channel = ctx.getChannel();
Server server = ctx.getServer(); Server server = ctx.getServer().get();
if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) { if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) {
return; return;

View File

@ -14,21 +14,25 @@ import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.server.Server; import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User; import org.javacord.api.entity.user.User;
import dev.salmonllama.fsbot.config.BotConfig; import dev.salmonllama.fsbot.config.BotConfig;
import org.javacord.api.event.message.MessageCreateEvent;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class CommandContext { public class CommandContext {
private MessageCreateEvent event;
private DiscordApi api; private DiscordApi api;
private Message message; private Message message;
private MessageAuthor author; private MessageAuthor author;
private TextChannel channel; private TextChannel channel;
private Server server; private Optional<Server> server;
private Command usedCommand; private Command usedCommand;
private String usedAlias; private String usedAlias;
private String[] args; private String[] args;
private CommandContext(CommandContextBuilder builder) { private CommandContext(CommandContextBuilder builder) {
this.event = builder.event;
this.api = builder.api; this.api = builder.api;
this.message = builder.message; this.message = builder.message;
this.author = builder.author; this.author = builder.author;
@ -55,7 +59,7 @@ public class CommandContext {
return channel; return channel;
} }
public Server getServer() { public Optional<Server> getServer() {
return server; return server;
} }
@ -80,15 +84,22 @@ public class CommandContext {
return null; return null;
} }
public Collection<Role> getUserRoles() { public Optional<Collection<Role>> getUserRoles() {
User user = getUser(); User user = getUser();
return user.getRoles(getServer()); if (getServer().isPresent()) {
return Optional.of(user.getRoles(getServer().get()));
}
return Optional.empty();
} }
public boolean isUserOwner() { public boolean isUserOwner() {
return getUser().getIdAsString().equals(BotConfig.BOT_OWNER); return getUser().getIdAsString().equals(BotConfig.BOT_OWNER);
} }
public boolean isPrivateMessage() {
return event.isPrivateMessage();
}
public CompletableFuture<Message> reply(String msg) { public CompletableFuture<Message> reply(String msg) {
return channel.sendMessage(msg); return channel.sendMessage(msg);
} }
@ -98,30 +109,27 @@ public class CommandContext {
} }
public static class CommandContextBuilder { public static class CommandContextBuilder {
private MessageCreateEvent event;
private DiscordApi api; private DiscordApi api;
private Message message; private Message message;
private MessageAuthor author; private MessageAuthor author;
private TextChannel channel; private TextChannel channel;
private Server server; private Optional<Server> server;
private Command usedCommand; private Command usedCommand;
private String usedAlias; private String usedAlias;
private String[] args; private String[] args;
public CommandContextBuilder( public CommandContextBuilder(
DiscordApi api, MessageCreateEvent event,
Message message,
MessageAuthor author,
TextChannel channel,
Server server,
Command usedCommand, Command usedCommand,
String usedAlias, String usedAlias,
String[] args String[] args
) { ) {
this.api = api; this.api = event.getApi();
this.message = message; this.message = event.getMessage();
this.author = author; this.author = event.getMessageAuthor();
this.channel = channel; this.channel = event.getChannel();
this.server = server; this.server = event.getServer();
this.usedCommand = usedCommand; this.usedCommand = usedCommand;
this.usedAlias = usedAlias; this.usedAlias = usedAlias;
this.args = args; this.args = args;

View File

@ -125,11 +125,7 @@ public class Guthix implements MessageCreateListener {
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 CommandContext.CommandContextBuilder( CommandContext ctx = new CommandContext.CommandContextBuilder(
api, event,
msg,
author,
channel,
server,
cmd, cmd,
cmdString, cmdString,
cmdArgs cmdArgs

View File

@ -33,7 +33,11 @@ public class PermissionManager {
} }
private boolean roleHandler(String roleId, CommandContext ctx) { private boolean roleHandler(String roleId, CommandContext ctx) {
return ctx.getUserRoles().stream().anyMatch(role -> role.getIdAsString().equals(roleId)); if (!ctx.getUserRoles().isPresent()) {
ctx.reply("This command can only be used in a server");
return false;
}
return ctx.getUserRoles().get().stream().anyMatch(role -> role.getIdAsString().equals(roleId));
} }
private boolean ownerHandler(CommandContext ctx) { private boolean ownerHandler(CommandContext ctx) {