Merge pull request #2 from Salmonllama/master

Update database-rewrite with master changes
This commit is contained in:
Alek Gryczewski 2020-02-20 15:09:54 -05:00 committed by GitHub
commit 3715209c9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 25 deletions

View File

@ -20,6 +20,7 @@ import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
public class CreateGalleryCommand extends Command { // TODO: This command needs help.
@Override public String name() { return "Create Gallery"; }
@ -34,11 +35,18 @@ public class CreateGalleryCommand extends Command { // TODO: This command needs
@Override
public void onCommand(CommandContext ctx) {
Server server = ctx.getServer();
Optional<Server> opServer = ctx.getServer();
TextChannel channel = ctx.getChannel();
String[] args = ctx.getArgs();
String targetChannelId = channel.getIdAsString();
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 targetServerId = server.getIdAsString();

View File

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

View File

@ -28,7 +28,11 @@ public class ColorsCommand extends Command {
@Override
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();
if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) {

View File

@ -31,9 +31,13 @@ public class WelcomeMessageCommand extends Command {
@Override
public void onCommand(CommandContext ctx) {
if (!ctx.getServer().isPresent()) {
ctx.reply("You must use this command in a server");
return;
}
String[] args = ctx.getArgs();
TextChannel channel = ctx.getChannel();
Server server = ctx.getServer();
Server server = ctx.getServer().get();
if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) {
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.user.User;
import dev.salmonllama.fsbot.config.BotConfig;
import org.javacord.api.event.message.MessageCreateEvent;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class CommandContext {
private MessageCreateEvent event;
private DiscordApi api;
private Message message;
private MessageAuthor author;
private TextChannel channel;
private Server server;
private Optional<Server> server;
private Command usedCommand;
private String usedAlias;
private String[] args;
private CommandContext(CommandContextBuilder builder) {
this.event = builder.event;
this.api = builder.api;
this.message = builder.message;
this.author = builder.author;
@ -55,7 +59,7 @@ public class CommandContext {
return channel;
}
public Server getServer() {
public Optional<Server> getServer() {
return server;
}
@ -80,15 +84,22 @@ public class CommandContext {
return null;
}
public Collection<Role> getUserRoles() {
public Optional<Collection<Role>> getUserRoles() {
User user = getUser();
return user.getRoles(getServer());
if (getServer().isPresent()) {
return Optional.of(user.getRoles(getServer().get()));
}
return Optional.empty();
}
public boolean isUserOwner() {
return getUser().getIdAsString().equals(BotConfig.BOT_OWNER);
}
public boolean isPrivateMessage() {
return event.isPrivateMessage();
}
public CompletableFuture<Message> reply(String msg) {
return channel.sendMessage(msg);
}
@ -98,30 +109,27 @@ public class CommandContext {
}
public static class CommandContextBuilder {
private MessageCreateEvent event;
private DiscordApi api;
private Message message;
private MessageAuthor author;
private TextChannel channel;
private Server server;
private Optional<Server> server;
private Command usedCommand;
private String usedAlias;
private String[] args;
public CommandContextBuilder(
DiscordApi api,
Message message,
MessageAuthor author,
TextChannel channel,
Server server,
MessageCreateEvent event,
Command usedCommand,
String usedAlias,
String[] args
) {
this.api = api;
this.message = message;
this.author = author;
this.channel = channel;
this.server = server;
this.api = event.getApi();
this.message = event.getMessage();
this.author = event.getMessageAuthor();
this.channel = event.getChannel();
this.server = event.getServer();
this.usedCommand = usedCommand;
this.usedAlias = usedAlias;
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
CommandContext ctx = new CommandContext.CommandContextBuilder(
api,
msg,
author,
channel,
server,
event,
cmd,
cmdString,
cmdArgs

View File

@ -33,7 +33,11 @@ public class PermissionManager {
}
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) {

View File

@ -146,6 +146,11 @@ public class ImageListener implements MessageCreateListener {
});
});
if (dbTable.equals("disaster")) {
// Add custom panda emoji: <:PandaWut:433045737245376522>
message.addReaction("PandaWut:433045737245376522");
}
message.addReaction(EmojiParser.parseToUnicode(":heartpulse:"));
}
}