Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/main/java/com/diamondfire/helpbot/bot/HelpBotInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import okhttp3.OkHttpClient;

import javax.security.auth.login.LoginException;

Expand All @@ -24,14 +25,14 @@ public class HelpBotInstance {
private static final Config config = new Config();
public static final long DF_GUILD = config.getGuild();
public static final long LOG_CHANNEL = config.getLogChannel();
private static final CommandHandler handler = new CommandHandler();
public static final OkHttpClient HTTP_CLIENT = new OkHttpClient();

private static JDA jda;
private static final TaskRegistry loop = new TaskRegistry();

public static void initialize() throws LoginException {

handler.register(
CommandHandler.getInstance().register(
// codeblock commands
new CodeCommand(),
new RankCommand(),
Expand Down Expand Up @@ -128,15 +129,15 @@ public static void initialize() throws LoginException {
.addEventListeners(new MessageEvent(), new ReadyEvent(), new GuildJoinEvent(), new ButtonEvent());

jda = builder.build();
handler.initialize();
CommandHandler.getInstance().initialize();
}

public static JDA getJda() {
return jda;
}

public static CommandHandler getHandler() {
return handler;
public static OkHttpClient getHttpClient() {
return HTTP_CLIENT;
}

public static Config getConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ public class CommandHandler {
private final HashMap<String, Command> ALIASES = new HashMap<>();
private final CommandExecutor COMMAND_EXECUTOR = new CommandExecutor();
private final DisableCommandHandler DISABLED_COMMAND_HANDLER = new DisableCommandHandler();

private static CommandHandler instance;

private CommandHandler() {
instance = this;
}

public void initialize() {
DISABLED_COMMAND_HANDLER.initialize();
}

public static Command getCommand(String name) {
Command cmd = HelpBotInstance.getHandler().getCommands().get(name.toLowerCase());
Command cmd = CommandHandler.getInstance().getCommands().get(name.toLowerCase());
if (cmd == null) {
cmd = HelpBotInstance.getHandler().getAliases().get(name.toLowerCase());
cmd = CommandHandler.getInstance().getAliases().get(name.toLowerCase());
}

return cmd;
Expand Down Expand Up @@ -53,4 +59,8 @@ public HashMap<String, Command> getAliases() {
public DisableCommandHandler getDisabledHandler() {
return DISABLED_COMMAND_HANDLER;
}
}

public static CommandHandler getInstance() {
return instance == null ? new CommandHandler() : instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

import java.util.Deque;

// Arguments simply parse a given value and can remove it from the stack if they process it correctly.
// This means an argument can actually use more than one argument if they want!
/**
* Arguments simply parse a given value and can remove it from the stack if they process it correctly.
* This means an argument can actually use more than one argument if they want!
*
* @param <T> parsed value type
*/
public interface Argument<T> {

T parseValue(@NotNull Deque<String> args) throws ArgumentException;

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.google.gson.*;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.UUID;

public class MinecraftPlayerUUIDArgument extends AbstractSimpleValueArgument<UUID> {

@Override
protected UUID parse(@NotNull String argument) throws ArgumentException {
if (argument.contains("-") || argument.length() > 16) {
return UUID.fromString(argument);
} else {
JsonObject responseObject = null;
ResponseBody res = null;
Request request = new Request.Builder().url("https://api.mojang.com/users/profiles/minecraft/" + argument).get().build();
try {
res = HelpBotInstance.getHttpClient().newCall(request).execute().body();
} catch (IOException e) {
e.printStackTrace();
}

try {
responseObject = JsonParser.parseString(res.string()).getAsJsonObject();
} catch (IOException e) {
e.printStackTrace();
}

if (responseObject.has("id")) {
return UUID.fromString(responseObject.get("id").getAsString());
} else {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.diamondfire.helpbot.bot.command.executor.checks;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.CommandHandler;
import com.diamondfire.helpbot.bot.command.reply.PresetBuilder;
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
import com.diamondfire.helpbot.bot.events.CommandEvent;

public class DisabledCheck implements CommandCheck {

@Override
public boolean check(CommandEvent event) {
return !HelpBotInstance.getHandler().getDisabledHandler().isDisabled(event.getCommand());
return !CommandHandler.getInstance().getDisabledHandler().isDisabled(event.getCommand());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.impl.other;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.CommandHandler;
import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.MultiArgumentContainer;
import com.diamondfire.helpbot.bot.command.argument.impl.types.*;
Expand Down Expand Up @@ -43,7 +44,7 @@ public boolean cacheArgumentSet() {
@Override
public ArgumentSet compileArguments() {
List<String> playerCommands = new ArrayList<>();
for (Command command : HelpBotInstance.getHandler().getCommands().values()) {
for (Command command : CommandHandler.getInstance().getCommands().values()) {
if (command instanceof AbstractPlayerUUIDCommand) {
playerCommands.add(command.getName());
}
Expand All @@ -69,7 +70,7 @@ public void run(CommandEvent event) {

for (String player : playerNames) {
try {
Command command1 = HelpBotInstance.getHandler().getCommands().get(command);
Command command1 = CommandHandler.getInstance().getCommands().get(command);
Field field = event.getClass().getDeclaredField("command");
field.trySetAccessible();
field.set(event, command1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.diamondfire.helpbot.bot.command.reply.PresetBuilder;
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import com.diamondfire.helpbot.sys.database.impl.DatabaseQuery;
import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery;
import com.diamondfire.helpbot.sys.tasks.impl.MuteExpireTask;
import com.diamondfire.helpbot.util.*;
import net.dv8tion.jda.api.entities.*;
Expand Down Expand Up @@ -65,8 +67,17 @@ public void run(CommandEvent event) {
long timeLeft = duration.toInstant().minusSeconds(Instant.now().getEpochSecond()).toEpochMilli();
Date finalDuration = duration;
event.getGuild().retrieveMemberById(user).queue((msg) -> {
//TODO Owen DBQuery

event.getGuild().retrieveMemberById(user).queue((member) -> {
new DatabaseQuery()
.query(new BasicQuery("INSERT INTO owen.muted_members (member,muted_by,muted_at,muted_till,reason) VALUES (?,?,CURRENT_TIMESTAMP(),?,?)", (statement) -> {
statement.setLong(1, user);
statement.setLong(2, event.getAuthor().getIdLong());
statement.setTimestamp(3, DateUtil.toTimeStamp(finalDuration));
statement.setString(4, event.getArgument("reason"));

}))
.compile();
});

builder.withPreset(
new InformativeReply(InformativeReplyType.SUCCESS, "Muted!", String.format("User will be muted for ``%s``.", FormatUtil.formatMilliTime(timeLeft)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void run(CommandEvent event) {
}

if (command != null) {
HelpBotInstance.getHandler().getDisabledHandler().disable(command);
CommandHandler.getInstance().getDisabledHandler().disable(command);
builder.withPreset(new InformativeReply(InformativeReplyType.SUCCESS, String.format("Command ``%s`` has been disabled.", command.getName())));
} else {
builder.withPreset(new InformativeReply(InformativeReplyType.ERROR, String.format("Command ``%s`` could not be found.", name)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Permission getPermission() {

@Override
public void run(CommandEvent event) {
DisableCommandHandler handler = HelpBotInstance.getHandler().getDisabledHandler();
DisableCommandHandler handler = CommandHandler.getInstance().getDisabledHandler();
PresetBuilder builder = new PresetBuilder();
String name = event.getArgument("cmd");
Command command = CommandHandler.getCommand(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.impl.other;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.CommandHandler;
import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.SingleArgumentContainer;
import com.diamondfire.helpbot.bot.command.argument.impl.types.DefinedObjectArgument;
Expand Down Expand Up @@ -42,7 +43,7 @@ public boolean cacheArgumentSet() {
@Override
public ArgumentSet compileArguments() {
return new ArgumentSet().addArgument("help",
new SingleArgumentContainer<>(new DefinedObjectArgument<>(HelpBotInstance.getHandler().getCommands().values().stream()
new SingleArgumentContainer<>(new DefinedObjectArgument<>(CommandHandler.getInstance().getCommands().values().stream()
.map(Command::getName)
.toArray(String[]::new))).optional(null));
}
Expand Down Expand Up @@ -75,7 +76,7 @@ public void run(CommandEvent event) {
categories.put(CommandCategory.CODE_BLOCK, new EmbedBuilder());
categories.put(CommandCategory.OTHER, new EmbedBuilder());

List<Command> commandList = new ArrayList<>(HelpBotInstance.getHandler().getCommands().values());
List<Command> commandList = new ArrayList<>(CommandHandler.getInstance().getCommands().values());
commandList.sort(Comparator.comparing(Command::getName));
for (Command command : commandList) {
HelpContext context = command.getHelpContext();
Expand All @@ -97,7 +98,7 @@ public void run(CommandEvent event) {
}
selector.build().send(event.getJDA());
} else {
Command command = HelpBotInstance.getHandler().getCommands().get(helpInfo);
Command command = CommandHandler.getInstance().getCommands().get(helpInfo);
HelpContext context = command.getHelpContext();
EmbedBuilder builder = new EmbedBuilder();
builder.setTitle("Command Information");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.command.impl.other;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.CommandHandler;
import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
import com.diamondfire.helpbot.bot.command.argument.impl.types.DefinedObjectArgument;
import com.diamondfire.helpbot.bot.command.help.*;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void run(CommandEvent event) {
);

List<String> commands = new ArrayList<>();
List<Command> commandList = new ArrayList<>(HelpBotInstance.getHandler().getCommands().values());
List<Command> commandList = new ArrayList<>(CommandHandler.getInstance().getCommands().values());
commandList.sort(Comparator.comparingInt((command) -> command.getPermission().ordinal()));

for (Command command : commandList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diamondfire.helpbot.bot.events;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.CommandHandler;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.ParsedArgumentSet;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.parser.ArgumentParser;
Expand All @@ -16,7 +17,8 @@ public class CommandEvent extends GuildMessageReceivedEvent {

private final Command command;
private final ReplyHandler replyHandler = new ReplyHandler(getChannel());
//TODO Cleanup and refactor this. I'd like to see stuff like replying be put into it's whole own section and refactored as well.
//TODO Cleanup and refactor this.
// I'd like to see stuff like replying be put into it's whole own section and refactored as well.
private ParsedArgumentSet parsedArgumentSet = null;
private String aliasedUsed = null;

Expand All @@ -26,10 +28,10 @@ public CommandEvent(Message message) {
String commandPrefix = rawArgs[0].substring(HelpBotInstance.getConfig().getPrefix().length()).toLowerCase();


Command cmd = HelpBotInstance.getHandler().getCommands().get(commandPrefix.toLowerCase());
Command cmd = CommandHandler.getInstance().getCommands().get(commandPrefix.toLowerCase());
if (cmd == null) {
this.aliasedUsed = commandPrefix.toLowerCase();
cmd = HelpBotInstance.getHandler().getAliases().get(commandPrefix.toLowerCase());
cmd = CommandHandler.getInstance().getAliases().get(commandPrefix.toLowerCase());
}

this.command = cmd;
Expand Down
Loading